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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL倒序如何避免filesort_MySQL Using filesort 疑问?

發(fā)布時間:2024/8/1 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL倒序如何避免filesort_MySQL Using filesort 疑问? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

表及相關(guān)數(shù)據(jù)

create table book(

bid int auto_increment primary key,

`name` varchar(20) not null,

authorid int not null,

publicid int not null,

typeid int not null

);

insert into book values(1,'tjava',1,1,2);

insert into book values(2,'tc',2,1,2);

insert into book values(3,'wx',3,2,1);

insert into book values(4,'math',4,2,3);

需求

查詢 authorid =1 且 typeid為2或3的 bid

優(yōu)化方案

方法1:

索引的順序是 bid,typeid,authorid

alter table book add index idx_bta (bid,typeid,authorid);

分析結(jié)果

explain select bid from book where typeid in(2,3) and authorid=1 order by typeid desc;

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

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

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

| 1 | SIMPLE | book | NULL | index | NULL | idx_bta | 12 | NULL | 4 | 25.00 | Using where; Using index; Using filesort |

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

方法2:

索引的順序是 typeid,authorid,bid

alter table book add index idx_tab (typeid,authorid,bid);

分析結(jié)果

explain select bid from book where typeid in(2,3) and authorid=1 order by typeid desc;

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

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

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

| 1 | SIMPLE | book | NULL | range | idx_tab | idx_tab | 8 | NULL | 2 | 100.00 | Using where; Using index |

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

疑問

為啥索引的順序是 bid,typeid,authorid 的出現(xiàn) Using filesort, 而 typeid,authorid,bid卻沒有出現(xiàn) Using filesort?

總結(jié)

以上是生活随笔為你收集整理的MySQL倒序如何避免filesort_MySQL Using filesort 疑问?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。