MySQL索引優化策略練手

2021-10-10 18:11:38 字數 4748 閱讀 4771

create table t2(

c1 char(1) not null default 0,

c2 char(1) not null default 0,

c3 char(1) not null default 0,

c4 char(1) not null default 0,

c5 char(1) not null default 0,

index c1234(c1,c2,c3,c4)

)engine innodb charset utf8;

insert into t2 values (1,3,5,6,7),(2,3,9,8,3),(4,3,2,7,5);
假設某個表有乙個聯合索引(c1,c2,c3,c4

a where c1=1 and c2=2 and c4>3 and c3=4

b where c1=1 and c2=2 and c4=3 order by c3

c where c1=1 and c4=2 group by c3,c2

d where c1=1 and c5=2 order by c2,c3

e where c1=1 and c2=2 and c5=4 order by c2,c3

驗證a
explain select * from t2 where c1=『a』 and c2=『b』 and c4>『a』 and c3=『b』\g;

mysql> explain select * from t2 where c1='a' and c2='b' and c4>'a' and c3='b'\g; 

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: range

possible_keys: c1234

key: c1234

key_len: 12

ref: null

rows: 1

extra: using index condition

1 row in set (0.00 sec)

error:

no query specified

結果 4個都用了

驗證b

explain select * from t2 where c1=『a』 and c2=『b』 and c4=『a』 order by c3\g;

mysql> explain select * from t2 where c1='a' and c2='b' and c4='a' order by c3\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: ref

possible_keys: c1234

key: c1234

key_len: 6

ref: const,const

rows: 2

extra: using index condition; using where

1 row in set (0.00 sec)

error:

no query specified

結果 c1 c2 用了 c3用在排序上

驗證c

explain select * from t2 where c1=『a』 and c4=『b』 group by c3,c2\g;

mysql> explain select * from t2 where c1='a' and c4='b' group by c3,c2\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: ref

possible_keys: c1234

key: c1234

key_len: 3

ref: const

rows: 2

extra: using index condition; using where; using temporary; using filesort

1 row in set (0.00 sec)

error:

no query specified

結果 用了1個c1

驗證d

explain select * from t2 where c1=『a』 and c5=『b』 order by c2,c3\g;

mysql> explain select * from t2 where c1='a' and c5='b' order by c2,c3\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: ref

possible_keys: c1234

key: c1234

key_len: 3

ref: const

rows: 2

extra: using index condition; using where

1 row in set (0.00 sec)

error:

no query specified

結果 用了1個c1, 排序用到了c2和c3

d變種

explain select * from t2 where c1=『a』 and c5=『b』 order by c3,c2\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: ref

possible_keys: c1234

key: c1234

key_len: 3

ref: const

rows: 2

extra: using index condition; using where; using filesort

1 row in set (0.00 sec)

error:

no query specified

結果 用了1個c1 c3和c2順序使用錯誤 導致索引失效 產生filesort

驗證e

explain select * from t2 where c1=『a』 and c2=『c』 and c5=『b』 order by c2,c3\g;

mysql> explain select * from t2 where c1='a' and c2='c' and c5='b' order by c2,c3\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: ref

possible_keys: c1234

key: c1234

key_len: 6

ref: const,const

rows: 1

extra: using index condition; using where

1 row in set (0.00 sec)

error:

no query specified

結果 用了2個 c1 c2 排序用到c2和c3

e變種

explain select * from t2 where c1=『a』 and c2=『c』 and c5=『b』 order by c3,c2\g;

*************************** 1. row ***************************

id: 1

select_type: ******

table: t2

type: ref

possible_keys: c1234

key: c1234

key_len: 6

ref: const,const

rows: 1

extra: using index condition; using where

1 row in set (0.00 sec)

error:

no query specified

結果 用了2個 此時c2是確定值 相當於常量

mysql索引優化策略練手

create table t2 c1 char 1 not null default 0,c2 char 1 not null default 0,c3 char 1 not null default 0,c4 char 1 not null default 0,c5 char 1 not null...

MySQL索引優化策略分析

explain的用法 執行計畫 explain select from pms product where id 1 組合索引一定是最左匹配原則 如果你在表上建立了很多組合索引,索引檔案膨脹,修改 刪除 更新會比較慢expalin的作用 type 查詢的效果從上到下越來越差 優勢 提高查詢速度 表連...

mysql索引使用策略和優化

1.1 索引選擇原則 較頻繁的作為查詢條件的字段應該建立索引 唯一性太差的字段不適合單獨建立索引,即使頻繁作為查詢條件 更新非常頻繁的字段不適合建立索引 不會出現在 where 子句中的字段不該建立索引 1.2 索引選擇原則描述 1.3 索引選擇注意事項 既然索引可以加快查詢速度,那麼是不是只要是查...