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

作者:stpeace 时间:2024-01-27 11:50:44 

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>

来源:https://blog.csdn.net/stpeace/article/details/79143987

标签:mysql,in,索引失效
0
投稿

猜你喜欢

  • 仿dw8代码折叠功能的HTML编辑器

    2008-04-29 21:19:00
  • 查看python下OpenCV版本的方法

    2022-12-24 18:15:32
  • vue路由history模式页面刷新404解决方法Koa Express

    2024-06-07 15:22:33
  • sqlserver 支持定位当前页,自定义排序的分页SQL(拒绝动态SQL)

    2024-01-16 03:58:06
  • Server.HTMLEncode让代码在页面里显示为源代码

    2023-11-24 13:40:28
  • go日志库logrus的安装及快速使用

    2024-04-26 17:31:40
  • Python+OCR实现文档解析的示例代码

    2023-11-22 02:34:19
  • python+Selenium自动化测试——输入,点击操作

    2023-08-09 07:20:50
  • 如何用OpenCV -python3实现视频物体追踪

    2022-04-02 23:15:58
  • 详解Python 多线程 Timer定时器/延迟执行、Event事件

    2022-09-04 11:12:52
  • Python实现随机生成迷宫并自动寻路

    2023-11-18 11:12:41
  • MySQL SELECT同时UPDATE同一张表问题发生及解决

    2024-01-25 05:32:31
  • MySQL性能诊断与调优工具

    2010-11-02 11:41:00
  • 讲解无法打开用户默认数据库的解决方法

    2008-12-05 15:55:00
  • pytorch深度神经网络入门准备自己的图片数据

    2023-12-07 13:55:58
  • vue项目中在可编辑div光标位置插入内容的实现代码

    2024-05-28 15:55:45
  • Python解压 rar、zip、tar文件的方法

    2023-07-22 10:51:38
  • MySQL存储引擎InnoDB与Myisam的区别分析

    2024-01-18 13:34:50
  • 如何使用Python的Requests包实现模拟登陆

    2022-10-07 03:12:26
  • 解决python 未发现数据源名称并且未指定默认驱动程序的问题

    2022-07-18 14:34:48
  • asp之家 网络编程 m.aspxhome.com