Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
MySQL 删除数据时何时会走联合索引?_创想鸟

MySQL 删除数据时何时会走联合索引?

mysql 删除数据时何时会走联合索引?

%ign%ignore_a_1%re_a_1% 删除数据时走索引的条件

在一个用户表中有字段 id、name、age、sex、work 和 city,联合索引为 (sex, city)。现在要删除 sex=男、city=北京 的数据,会不会使用联合索引?

回答:

在 mysql 中,当涉及的数据量超过 20% 时,不会使用索引。因此,如果符合条件的数据量超过表总数据量的 20%,则不会走索引。

实践验证:

表总数据量:1602 条

符合条件(sex, city)条数:

智谱AI开放平台 智谱AI开放平台

智谱AI大模型开放平台-新一代国产自主通用AI开放平台

智谱AI开放平台 85 查看详情 智谱AI开放平台 女,广州:604 条女,惠州:6 条

执行删除语句的 explain 结果:

女,广州:符合条件数据量超过 20%,不会使用索引。

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%,会使用索引。

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 |+----+-------------+--------------+------------+-------+---------------+--------------+---------+-------------+------+----------+-------------+

以上就是MySQL 删除数据时何时会走联合索引?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/738863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
悟空浏览器怎么恢复默认的搜索引擎_悟空浏览器恢复默认搜索引擎设置步骤
上一篇 2025年11月25日 14:07:55
Java中LinkedList基本使用方法
下一篇 2025年11月25日 14:07:57

相关推荐

发表回复

登录后才能评论
关注微信