mysql 删除数据的时候,会不会走索引?
一个用户表,字段 id,name,age,sex,work,city 联合索引为 (sex,city)我要删除sex=男,city=北京的数据,会不会走联合索引
回复
1个回答

test
2024-07-17
在 mysql 中,如果涉及到的数据超过 20%,将不会使用索引,反之则会。@zxdposter 你这个应该是翻译错了吧
简单实践
数据总条数是: 1602
符合条件sex,city条数: 女, 广州, 604
mysql> 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 |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set (0.00 sec)
符合条件sex,city条数: 女, 惠州, 6
mysql> 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 |
+----+-------------+--------------+------------+-------+---------------+--------------+---------+-------------+------+----------+-------------+
1 row in set (0.00 sec)
实践环境和数据
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容