日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql用in查询list_Mysql优化器对in list的处理

發布時間:2023/12/4 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql用in查询list_Mysql优化器对in list的处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

select * from table where id in (....)

這樣的查詢,是走范圍索引還是走等值索引?

select * from table where key_part1 in (....) and key_part2='XX';

這樣的查詢,第二部分還走不走索引?

測試目的,想知道,MYSQL對IN LIST是如何選擇執行計劃的;在單字段索引和復合索引中;

mysql 5.1.40

os:rhel 5.4

engine=innodb

innodb_file_per_table

# 先來創建測試環境:

create table index_test ( id int auto_increment , col1 int ,col2 varchar(200) ,content varchar(500),primary key (id) ,key col1 (col1) ) engine=innodb default charset=latin1;

# repeat insert operation 12 times

insert into index_test (col1,col2) select @rownum:=@rownum+1,column_name from information_schema.COLUMNS c , (select @rownum:=0 ) id limit 500 ;

# 測試1:先測對主鍵的IN操作;

# 測試用例:

reset query cache; --清空QUERY_CAHCE

show status like 'Innodb_buffer_pool_read_requests' ;? ?--用來查詢邏輯讀

select * from index_test where id in (2,10,1000,2000,9000);

show status like 'Innodb_buffer_pool_read_requests' ;? ?--與前面的結果相減,就得到SQL執行所帶來的邏輯讀 ;

為了邏輯讀的準確性, 對同一個SQL你應該多跑幾次,以去掉物理讀 ;

root@127.0.0.1 : test 16:02:16> explain select * from index_test where id in (2,10,1000,2000);

+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+

| id | select_type | table? ?? ?| type??| possible_keys | key? ???| key_len | ref??| rows | Extra? ?? ? |

+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+

|??1 | SIMPLE? ?? ?| index_test | range | PRIMARY? ?? ? | PRIMARY | 4? ?? ? | NULL |? ? 4 | Using where |

+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+

1 row in set (0.00 sec)

# 從執行計劃看,走的是范圍條件;但我們看看實際情況 :

# 注意,為了減少篇幅,我把各個查詢的結果給刪減了。

select * from index_test where id in (2,10);

RESULTs: 2 rows

LIO  : 4

select * from index_test where id in (2,1000);

RESULTs: 2 rows

LIO  : 4

select * from index_test where id in (2,10,100);

RESULTs: 3 rows

LIO  : 6

select * from index_test where id in (2,10,1000,2000);

RESULTs: 4 rows

LIO  : 8

select * from index_test where id in (2,10,1000,2000,9000);

RESULTs: 5 rows

LIO  : 10

### 在這里看到,邏輯讀根據IN LIST里KEY的數量成線性增加,而沒有根據KEY值的大小變化,所以我們判斷,對主鍵的IN操作,其實都轉成了OR操作。

# 測試2:對非主鍵的IN操作;

# 測試用例:

reset query cache;

show status like 'Innodb_buffer_pool_read_requests' ;

select * from index_test where col1 in (100,500,300,400);

show status like 'Innodb_buffer_pool_read_requests' ;

root@127.0.0.1 : test 16:06:33> explain select * from index_test where col1 in (100,200);

+----+-------------+------------+-------+---------------+------+---------+------+------+-------------+

| id | select_type | table? ?? ?| type??| possible_keys | key??| key_len | ref??| rows | Extra? ?? ? |

+----+-------------+------------+-------+---------------+------+---------+------+------+-------------+

|??1 | SIMPLE? ?? ?| index_test | range | col1? ?? ?? ? | col1 | 5? ?? ? | NULL |? ?24 | Using where |

+----+-------------+------------+-------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

select * from index_test where col1 in (100,101);

RESULTs: 24 rows

LIO? ? : 86

select * from index_test where col1 in (100,500);

RESULTs: 24 rows

LIO? ? : 86

select * from index_test where col1 in (100,500,300);

RESULTs: 36 rows

LIO? ? : 139

select * from index_test where col1 in (100,500,300,400);

RESULTs: 48 rows

LIO? ? : 172

分析: 這個結果與測試1的結果是一樣的;

# 測試3:對復合索引的前列IN操作;

alter table index_test drop index col1 ,add index col1col2(col1,col2) ;

update index_test set content=concat(col2,col3,col1) ;

主要是測一下,索引的第一個字段用IN后,優化器還會不會使用第二個字段來進行索引搜索;

root@127.0.0.1 : test 18:41:38> explain select content from index_test where col1 in (100,500,300,400) and col2='aaaa';

+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+

| id | select_type | table? ?? ?| type??| possible_keys | key? ?? ?| key_len | ref??| rows | Extra? ?? ? |

+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+

|??1 | SIMPLE? ?? ?| index_test | range | col1col2? ?? ?| col1col2 | 208? ???| NULL |? ? 4 | Using where |

+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+

1 row in set (0.00 sec)

select count(*) from index_test where col1 in (100,500,300,400) and col2='aaaa';

RESULTs: 0 rows

LIO? ? : 24

select content from index_test where col1 in (100,500,300,400) and col2='aaaa';

RESULTs: 0 rows

LIO? ? : 24

分析:

#我們發現,兩個查詢的邏輯讀是一樣,其實這已經表明優化器用上了索引的第二個字段,在索引搜索部分就完成了對COL2的過濾;

總結:MYSQL優化器對in list是轉成“or” 的“多個等值”查詢來處理的;并沒有轉成范圍查詢 ;

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的mysql用in查询list_Mysql优化器对in list的处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。