MySQL優化筆記02 列型別的選擇

2021-09-11 15:40:22 字數 3358 閱讀 4901

字段型別優先順序排序:

整型 > date,time > enum,char > varchar > blob

列的特點分析:

整型: 定長,沒有國家/地區之分,沒有字符集的差異

time: 定長,運算快,節省空間. 考慮時區,寫sql時不方便 where > 『2005-10-12』;

enum: 能起來約束值的目的, 內部用整型來儲存,但與char聯查時,內部要經歷串與值的轉化

char: 定長, 考慮字符集和(排序)校對集

varchar: 不定長, 要考慮字符集的轉換與排序時的校對集,速度慢.

text/blob: 無法使用記憶體臨時表

附: 關於date/time的選擇,大師的明確意見

性別:  以utf8為例

char(1) , 3個字長位元組

enum(『男』,』女』);  // 內部轉成數字來存,多了乙個轉換過程

tinyint() ,  // 0 1 2 // 定長1個位元組.

原因:大的字段浪費記憶體,影響速度,

以年齡為例 tinyint unsigned not null, 可以儲存255歲, 足夠. 用int浪費了3個位元組

以varchar(10) ,varchar(300)儲存的內容相同, 但在表聯查時,varchar(300)要花更多記憶體

原因:

null不利於索引,要用特殊的位元組來標註.

在磁碟上佔據空間其實更大.

實驗:

可以建立2張欄位相同的表, 乙個允許為null, 乙個不允許為null, 各加入1萬條, 檢視索引檔案的大小.

可以發現, 為null的索引要大些.

(mysql5.5裡,關於null已經做了優化,大小區別已不明顯)

另外:null也不便於查詢,

where 列名=null;  

where 列名!=null; 都查不到值,

where 列名 is null, 或is not null 才可以查詢.

create table dictnn (

id int,

word varchar(14) not null default '',

key(word)

)engine myisam charset utf8;

create table dictyn (

id int,

word varchar(14),

key(word)

)engine myisam charset utf8;

alter table dictnn disable keys;

alter table dictyn disable keys;

insert into dictnn select id,if(id%2,word,'') from dict limit 10000;

insert into dictyn select id,if(id%2,word,null) from dict limit 10000;

alter table dictnn enable keys;

alter table dictyn enable keys;

enum列的說明

1: enum列在內部是用整型來儲存的

2: enum列與enum列相關聯速度最快

3: enum列比(var)char 的弱勢---在碰到與char關聯時, 要轉化. 要花時間.

4: 優勢在於,當char非常長時,enum依然是整型固定長度.

當查詢的資料量越大時,enum的優勢越明顯.

5: enum與char/varchar關聯 ,因為要轉化,速度要比enum->enum,char->char要慢,

但有時也這樣用-----就是在資料量特別大時,可以節省io.

實驗:

create table t2 (

id int,

gender enum('man','woman'),

key(gender)

)engine myisam charset utf8;

create table t3 (

id int,

gender char(5) not null default '',

key(gender)

)engine myisam charset utf8;

alter table t2 disable keys;

alter table t3 disable keys;

insert into t2 select id,if(id%2,'man','woman') from dict limit 10000;

insert into t3 select id,if(id%2,'man','woman') from dict limit 10000;

alter table t2 enable keys;

alter table t3 enable keys;

select count(*) from t2 as ta,t2 as tb where ta.gender=tb.gender

select count(*) from t3 as ta,t3 as tb where ta.gender=tb.gender

列<---->列

時間enum<--->enum

10.53

char<---->char

24.65

enum<---->char

18.22

如果t2表的優勢不明顯, 加大t3的gender列, char(15), char(20)...

隨著t3 gender列的變大, t2表優勢逐漸明顯.

原因:

無論enum(『manmaman』,』womanwomanwoman』) 列舉的字元多長,內部都是用整型表示, 在記憶體中產生的資料大小不變,

而char型,卻在記憶體中產生的資料越來越多.

總結: enum 和enum型別關聯速度比較快

enum 型別 節省了io

mysql優化 表的優化與列型別的選擇

定長與變長分離 常用字段和不常用字段分離 合理增加冗餘字段 字段型別優先順序 整型 date,time enum,char varchar blob 列的特點分析 整型 定長,沒有國家 地區之分,沒有字符集的差異 time 定長,運算快,節省空間.考慮時區 enum 能起來約束值的目的,內部用整型來...

MySQL的列型別

tinyint 迷你整型,系統採用乙個位元組來儲存的整型,乙個位元組 8位,最大的表示值是0 255 smallint 小整型,系統採用兩個位元組來儲存的整型,能表示0 65535之間mediumint 中整型,系統採用3個位元組來儲存的整型int 標準整型,系統採用4個位元組來儲存資料bigint...

mysql的列型別是 mysql 三大列型別

數值型 1.整型 tinyint 佔據空間 1個位元組,儲存範圍 128 127,0 255 乙個位元組 8個位 0000 0000 1111 1111 0 2的8次方 1 255 0 000000 0 1111111 0 127 1 000000 1 1111111 0 127 127 127 二...