MySQL倒序如何避免filesort_MySQL:使用IN和ORDER BY时避免使用filesort

让's suppose I have the following table (let' s称之为 my_table ):

CREATE TABLE `my_table` (

`table_id` int(10) unsigned NOT NULL auto_increment,

`my_field` int(10) unsigned NOT NULL default '0'

PRIMARY KEY (`table_id`),

KEY `my_field` (`my_field`,`table_id`)

) ENGINE=MyISAM

my_table 的主键是 table_id (auto_increment),我还有一个带有 my_field 和 table_id 的键 .

如果我测试这个查询...

EXPLAIN SELECT * FROM my_table

WHERE my_field = 28

ORDER BY table_id DESC;

......我明白了:

id select_type table type possible_keys key key_len ref rows Extra

--- ----------- -------- ---- ------------- -------- ------- ----- ---- -----

1 SIMPLE my_table ref my_field my_field 8 const 36

您可以看到它正在使用正确的密钥( my_field ) .

但如果我试试这个......

EXPLAIN SELECT * FROM my_table

WHERE my_field IN (1, 28, 20)

ORDER BY table_id DESC;

......我明白了:

id select_type table type possible_keys key key_len ref rows Extra

--- ----------- -------- ---- ------------- ------ ------- ------ ---- ---------------------------

1 SIMPLE my_table ALL my_field (NULL) (NULL) (NULL) 406 Using where; Using filesort

您可以看到它根本没有使用任何密钥,更糟糕的是,使用 filesort .

即使我做“ FORCE INDEX (my_field) ”,它仍然执行filesort .

有没有办法避免filesort?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值