mysql數據類型與索引調優

varcha比固定長度佔用更少存儲空間,只佔用需要空間,但是需要1字節保存長度

更小通常更好

簡單就好,避免null

確定類型

使用enum代替字符串類型


innnodb存儲引擎

事務性

外鍵

行級鎖

多版本

按照主鍵聚集

所有索引包含主鍵列

優化的緩存:innodb把數據和內存緩存緩衝到緩存池 自動構建哈希索引

未壓縮索引:索引沒有使用前綴壓縮,阻塞auto_increment;innodb使用表級鎖產生新的auto_increment

沒有緩存的count():myisam會把行數保存在表中 innodb中的count會全表掃描或索引掃描


可以適當的使用前綴索引

select count(disinct name) / count(*) from area

select count (disinct left(name, 3)) / count(*) from area


聚集索引

innodb索引保存了b-tree索引和數據行


覆蓋索引

explain解釋器的extra列看到using index

滿足條件:

select 查詢字段必須有索引全覆蓋,不能在索引執行like操作


不要在選擇性很差的列添加索引


check table

repair table

myisamchk


更新索引統計

1.analyze table test

show index


減少索引和數據碎片

myisam引擎: optimize table

innodb引擎:alter table engine =


表週期性創建

週期創建可以得到沒有碎片和全排序索引高效表


drop table if exists area_new,area_old

create table area_new like area;

rename table area to area_old.area_new to area;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章