免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: yuanxb1985
打印 上一主题 下一主题

MySQL 选择索引疑惑 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2012-03-30 10:10 |只看该作者
小版主杀手 发表于 2012-03-29 11:00
记得手册上有关于优化器的选择介绍

每个表的索引被查询,并且使用最好的索引,除非优化器认为使用表 ...


你好,你想说明什么,没有看懂。能否说的通俗点。

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
12 [报告]
发表于 2012-04-11 13:52 |只看该作者
请注意 key_len: 4,在你所有的explain里面都是这个

论坛徽章:
0
13 [报告]
发表于 2012-04-11 14:12 |只看该作者
楼上版主能不能说的更清楚一点,希望指教。

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
14 [报告]
发表于 2012-04-11 14:49 |只看该作者
本帖最后由 ruochen 于 2012-04-11 15:29 编辑

从LZ的测试结果上看到有些疑惑,回头我测试一下


LZ的mysql版本多少?

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
15 [报告]
发表于 2012-04-11 15:34 |只看该作者
下面是我的测试过程,始终是使用s_u上的索引
方便的话,LZ将这个表倒出来上传到cu供大家测试下
  1. mysql> show create table book\G
  2. *************************** 1. row ***************************
  3.        Table: book
  4. Create Table: CREATE TABLE `book` (
  5.   `book_id` int(11) NOT NULL AUTO_INCREMENT,
  6.   `status` int(11) NOT NULL,
  7.   `level` int(11) NOT NULL,
  8.   `update_time` datetime DEFAULT NULL,
  9.   `price` int(11) DEFAULT NULL,
  10.   `actor` varchar(20) DEFAULT NULL,
  11.   `pub` varchar(20) DEFAULT NULL,
  12.   PRIMARY KEY (`book_id`),
  13.   KEY `s_l_u` (`status`,`level`,`update_time`),
  14.   KEY `s_u` (`status`,`update_time`)
  15. ) ENGINE=MyISAM AUTO_INCREMENT=72551 DEFAULT CHARSET=utf8
  16. 1 row in set (0.00 sec)

  17. mysql> select distinct status,count(*) from book group by status;
  18. +--------+----------+
  19. | status | count(*) |
  20. +--------+----------+
  21. |      1 |     9000 |
  22. |      2 |    28550 |
  23. |      3 |    10500 |
  24. |      4 |    12000 |
  25. |      5 |     1500 |
  26. |      6 |    11000 |
  27. +--------+----------+
  28. 6 rows in set (0.00 sec)

  29. mysql> select distinct level,count(*) from book group by level;
  30. +-------+----------+
  31. | level | count(*) |
  32. +-------+----------+
  33. |     0 |    72330 |
  34. |     1 |      220 |
  35. +-------+----------+
  36. 2 rows in set (0.10 sec)

  37. mysql> select distinct update_time,count(*) from book group by update_time;
  38. +---------------------+----------+
  39. | update_time         | count(*) |
  40. +---------------------+----------+
  41. | 2012-04-11 15:14:25 |    11533 |
  42. | 2012-04-11 15:14:26 |    12853 |
  43. | 2012-04-11 15:14:27 |    12340 |
  44. | 2012-04-11 15:14:28 |    12019 |
  45. | 2012-04-11 15:14:29 |    12046 |
  46. | 2012-04-11 15:14:30 |    11588 |
  47. | 2012-04-11 15:14:31 |      171 |
  48. +---------------------+----------+
  49. 7 rows in set (0.11 sec)


  50. mysql> explain  select update_time from book where status=2 order by update_time desc;
  51. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  52. | id | select_type | table | type | possible_keys | key  | key_len | ref   | rows  | Extra                    |
  53. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  54. |  1 | SIMPLE      | book  | ref  | s_l_u,s_u     | s_u  | 4       | const | 20798 | Using where; Using index |
  55. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  56. 1 row in set (0.00 sec)

  57. mysql> explain  select update_time from book where status=2 order by update_time desc limit 100,50;
  58. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  59. | id | select_type | table | type | possible_keys | key  | key_len | ref   | rows  | Extra                    |
  60. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  61. |  1 | SIMPLE      | book  | ref  | s_l_u,s_u     | s_u  | 4       | const | 20798 | Using where; Using index |
  62. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  63. 1 row in set (0.00 sec)

  64. mysql> explain  select update_time from book where status=2 order by update_time desc limit 28000,50;
  65. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  66. | id | select_type | table | type | possible_keys | key  | key_len | ref   | rows  | Extra                    |
  67. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  68. |  1 | SIMPLE      | book  | ref  | s_l_u,s_u     | s_u  | 4       | const | 20798 | Using where; Using index |
  69. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  70. 1 row in set (0.00 sec)

  71. mysql> explain  select update_time from book where status=2 order by update_time desc limit 20000,50;
  72. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  73. | id | select_type | table | type | possible_keys | key  | key_len | ref   | rows  | Extra                    |
  74. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  75. |  1 | SIMPLE      | book  | ref  | s_l_u,s_u     | s_u  | 4       | const | 20798 | Using where; Using index |
  76. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  77. 1 row in set (0.00 sec)

  78. mysql> explain  select update_time from book where status=2 order by update_time desc limit 10000,50;
  79. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  80. | id | select_type | table | type | possible_keys | key  | key_len | ref   | rows  | Extra                    |
  81. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  82. |  1 | SIMPLE      | book  | ref  | s_l_u,s_u     | s_u  | 4       | const | 20798 | Using where; Using index |
  83. +----+-------------+-------+------+---------------+------+---------+-------+-------+--------------------------+
  84. 1 row in set (0.00 sec)
复制代码

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
16 [报告]
发表于 2012-04-11 15:46 |只看该作者
本帖最后由 ruochen 于 2012-04-11 15:55 编辑

从LZ的explain看到,下面这个sql的type是range,ref是NULL,很疑惑
楼主analyze local table book;后再explain看看呢?

可能和数据的碎片(update/delete)有关
  1. mysql> explain select  update_time from book  where status = 2  order
  2. by update_time desc limit 100,50\G
  3. *************************** 1. row ***************************
  4.          id: 1
  5. select_type: SIMPLE
  6.       table: book
  7.        type: range---------------
  8. possible_keys: S_L_U,S_U
  9.         key: S_U
  10.     key_len: 4------------------
  11.         ref: NULL----------------
  12.        rows: 27893
  13.       Extra: Using where; Using index
  14. 1 row in set (0.00 sec)
复制代码

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
17 [报告]
发表于 2012-04-11 15:53 |只看该作者
yuanxb1985 发表于 2012-03-28 13:40
希望大家指点

表结构如下所示:


两个sql的explain输出key_len: 4;说明只使用了两个索引的status部分(索引的最左原则)
using index说明select update_time这个字段能在使用到的复合索引中直接取到(覆盖索引,也即dba建议为啥不用select *的主要原因)

论坛徽章:
0
18 [报告]
发表于 2012-04-11 16:19 |只看该作者
学习了 ,今天的气氛很好啊,
一直这样,我等菜鸟怎么能不提高啊

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
19 [报告]
发表于 2012-04-11 16:43 |只看该作者
ooooldman 发表于 2012-04-11 16:19
学习了 ,今天的气氛很好啊,
一直这样,我等菜鸟怎么能不提高啊



多来反馈问题和测试就是最快的提高
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP