
mysql 删除数据时是否利用索引
问题描述:
在拥有 (sex, city) 联合索引的 user 表中,删除指定性别和城市的数据时,是否会使用该索引?
答案:
在 mysql 中,如果删除操作涉及的数据量超过 20%,将不会使用索引。否则,会使用索引。
实践验证:
假设数据总量为 1602,删除满足 sex=”女”、city=”广州” 条件的数据,涉及数据量为 604。
explain delete from test_del_idx where sex="女" and city = "广州";
执行结果:
+---------+---------+--------------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |+---------+---------+--------------+------------+------+---------------+------+---------+------+------+----------+-------------+| 1 | delete | test_del_idx | null | all | idx_sex_city | null | null | null | 1602 | 100.00 | using where |+---------+---------+--------------+------------+------+---------------+------+---------+------+------+----------+-------------+
由于删除数据量超过了 20%,因此未走索引。
再假设删除满足 sex=”女”、city=”惠州” 条件的数据,涉及数据量为 6。
explain delete from test_del_idx where sex="女" and city = "惠州";
执行结果:
+---------+---------+--------------+------------+-------+---------------+--------------+---------+-------------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+---------+---------+--------------+------------+-------+---------------+--------------+---------+-------------+------+----------+-------------+| 1 | DELETE | test_del_idx | NULL | range | idx_sex_city | idx_sex_city | 773 | const,const | 6 | 100.00 | Using where |+---------+---------+--------------+------------+-------+---------------+--------------+---------+-------------+------+----------+-------------+
由于删除数据量少于 20%,因此走索引。
以上就是MySQL 删除数据时是否会利用索引?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/42416.html
微信扫一扫
支付宝扫一扫