欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

in会让mysql索引失效吗_mysql的in会不会让索引失效?

发布时间:2025/3/15 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 in会让mysql索引失效吗_mysql的in会不会让索引失效? 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

mysql的in会让索引失效吗?不会! 看结果:

mysql> desc select * from tb_province where name in ('lily3', 'lily2', 'lily1');

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

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

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

| 1 | SIMPLE | tb_province | NULL | ALL | NULL | NULL | NULL | NULL | 108780 | 30.00 | Using where |

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

1 row in set, 1 warning (0.00 sec)

mysql> alter table tb_province add index g(name);

Query OK, 0 rows affected (0.29 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc select * from tb_province where name in ('lily3', 'lily2', 'lily1');

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

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

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

| 1 | SIMPLE | tb_province | NULL | range | g | g | 34 | NULL | 3 | 100.00 | Using index condition |

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

1 row in set, 1 warning (0.00 sec)

mysql>

顺便说下,in查出的结果,不一定按in排序, 如下:

mysql> select * from tb_province where name in ('lily3', 'lily2', 'lily1');

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

| id | name | score | x | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 |

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

| 1 | lily1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

| 2 | lily2 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

| 3 | lily3 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

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

3 rows in set (0.00 sec)

mysql>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:解决MySQL中IN子查询会导致无法使用索引问题

MySQL查询条件中in会用到索引吗

总结

以上是生活随笔为你收集整理的in会让mysql索引失效吗_mysql的in会不会让索引失效?的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。