MySQL的聯合索引和覆蓋索引

2021-07-11 04:11:43 字數 4808 閱讀 7158

關於mysql的聯合索引,覆蓋索引一直濛濛噠,所以寫點東西來熟悉一下

首先建立乙個表orders,結構如下:

create

table orders(

order_id int unsigned auto_increment,

order_status int

notnull,

total_price int unsigned,

settle_type int,

store_id int,

buyer_id int,

goods varchar(20),

create_time datetime default

current_timestamp,

primary

key(order_id)

);

然後我們建立乙個儲存過程,向這個表中插入一些資料:

drop procedure

ifexists

load_orders;

delimiter //

create

procedure

load_orders

(count int unsigned)

begin

declare

status

intdefault 0;

declare type int default

0;declare store int default

133;

declare price int default

85;declare buyer int default

1001;

declare goods varchar(20) ;

declare i int unsigned default

0;set goods= 'al';

while ido

set status=mod (status,77);

settype=mod(type,29);

if buyer>2000

then

set buyer=1001; end

if;if price>1000

then

set price=200; end

if;if length(goods)>18

then

set goods= 'al'; end

if;insert into orders(order_status,total_price,settle_type,store_id,goods,buyer_id) values (status, price, type, store, goods, buyer);

set count=count+1;

set status=status+1;

settype=type+1;

set store=store+1;

set price=price+43;

set buyer=buyer+1;

set goods=concat(goods, char(round(rand()*25)+97));

set i=i+1;

endwhile;

end//

delimiter ;

call load_orders(99999);#呼叫儲存過程

然後我們有表有資料啦,做一些查詢操作:

select

count(store_id) as cnt from orders where settle_type = 1

and order_status = 2

and buyer_id=1990;

select

count(order_id) as cnt from orders where settle_type = 1

and order_status = 2

and buyer_id=1990;

select

count(*) as cnt from orders where settle_type = 1

and order_status = 2

and buyer_id=1990;

結果如圖所示:

我們發現,此時count(普通列),count(主鍵索引列),count(*)耗時都是差不多的,需要7秒多,當然還是count(*)最快。

從查詢計畫中我們可以看到是全表掃瞄的,using where。

我們改變表的結構,給where後的三列新增乙個聯合索引:

alter

table orders add

key union_key(settle_type,order_status,buyer_id);

然後我們再做一下上面的那三個查詢:

我們發現,此時count(普通列)需要用時最久,

count(主鍵索引列),count(*)耗時都是很短。

由上圖我們看到當使用索引的第二列和第三列時根本就用不到索引。

聯合索引中是放著主鍵索引的值的。

上面的using index condition就是使用到了icp喲,where會在儲存引擎中過濾後再傳遞給server層。

desc select settle_type  from orders where settle_type = 1

and order_status = 2

and buyer_id=1990;

desc select settle_type from orders where settle_type = 1

and buyer_id=1990;

desc select settle_type from orders where order_status = 2

and buyer_id=1990;

desc select buyer_id from orders where settle_type = 1

and order_status = 2

and buyer_id=1990;

desc select buyer_id from orders where settle_type = 1

and buyer_id=1990;

desc select buyer_id from orders where order_status = 2

and buyer_id=1990;

mysql 覆蓋索引和聯合索引

我們這裡建立乙個使用者表,表中有字段name,並且在name上有索引 1 create table t user 2 id bigint 20 not null auto increment 3 name varchar 255 not null,4 primary key id 5index in...

Mysql中的聯合索引 字首索引 覆蓋索引

索引 索引是一種特殊的檔案,它們包含著對資料表裡所有記錄的引用指標。更通俗的說,資料庫索引好比是一本書前面的目錄,能加快資料庫的查詢速度。聯合索引 又名復合索引,由兩個或多個列的索引。它規定了mysql從左到右地使用索引字段,對字段的順序有一定要求。乙個查詢可以只使用索引中的一部分,更準確地說是最左...

MySQL單列索引和聯合索引

所有的mysql列型別能被索引。在相關的列上的使用索引是改進select操作效能的最好方法。乙個表最多可有16個索引。最大索引長度是256個位元組,儘管這可以在編譯mysql時被改變。對於char和varchar列,你可以索引列的字首。這更快並且比索引整個列需要較少的磁碟空間。在create tab...