在有些文章中也稱之為【索引的最佳左字首特性】
叫什麼不重要,重要的是要理解他,會去運用他----柳峰老師
建立乙個復合索引
create
index idx_name_salary_dept on employee(name,salary,dept)
;查詢資料
mysql>
explain
select
*from employee where name=
'墨菲'\g;**
****
****
****
****
****
****
*1.row***
****
****
****
****
****
****
id: 1
select_type: ******
table: employee
partitions: null
type: ref
possible_keys: idx_name_salary_dept
key: idx_name_salary_dept
key_len: 123
ref: const
rows: 1
filtered: 100.00
extra: null
1row
inset
,1 warning (
0.13 sec)
error:
no query specified
mysql>
explain
select
*from employee where name=
'墨菲'
and salary=
'8800'\g;**
****
****
****
****
****
****
*1.row***
****
****
****
****
****
****
id: 1
select_type: ******
table: employee
partitions: null
type: ref
possible_keys: idx_name_salary_dept
key: idx_name_salary_dept
key_len: 128
ref: const,const
rows: 1
filtered: 100.00
extra: null
1row
inset
,1 warning (
0.30 sec)
error:
no query specified
檢視name和dept是否有用到索引
explain
select
*from employee where name=
'墨菲'
and dept=
'部門a'\g;
mysql>
explain
select
*from employee where name=
'墨菲'
and dept=
'部門a'\g;**
****
****
****
****
****
****
*1.row***
****
****
****
****
****
****
id: 1
select_type: ******
table: employee
partitions: null
type: ref
possible_keys: idx_name_salary_dept
key: idx_name_salary_dept
key_len: 123
ref: const
rows: 1
filtered: 10.00
extra: using
index condition
1row
inset
,1 warning (
0.05 sec)
error:
no query specified
結果是一樣的,也有用到索引
如果僅僅是部門a的情況,我們發現並沒有用到索引
mysql>
explain
select
*from employee where dept=
'部門a'\g;**
****
****
****
****
****
****
*1.row***
****
****
****
****
****
****
id: 1
select_type: ******
table: employee
partitions: null
type: all
possible_keys: null
key: null
key_len: null
ref: null
rows: 13
filtered: 10.00
extra: using
where
1row
inset
,1 warning (
0.00 sec)
error:
no query specified
mysql學習筆記 復合索引前導列特性
在mysql中,如果建立了復合索引 name,salary,dept 就相當於建立了 name,salary,dept name,salary 和 name 三個索引,這被稱為復合索弓 前導列特性,因此在建立復合索引時應該將最常用作查詢條件的列放在最左邊,依次遞減。未使用索引 select from...
第五章 索引的使用 復合索引前導列特性
直接學習 復合索引前導列特性 1 在mysql中如果建立了復合索引 name,salary,dept 就相當於建立了 name,salary,dept name,salary name 三個索引,這被稱為復合索引前導特性。2 示例 如下 show index from employee g expl...
mysql 復合索引
聯合索引又叫復合索引。對於復合索引 mysql從左到右的使用索引中的字段,乙個查詢可以只使用索引中的一部份,但只能是最左側部分。例如索引是key index a,b,c 可以支援a a,b a,b,c 3種組合進行查詢,但不支援 b,c進行查詢 當最左側欄位是常量引用時,索引就十分有效。兩個或更多個...