设为首页 收藏本站
查看: 824|回复: 0

[经验分享] MySQL之 index merge 走错索引案例

[复制链接]

尚未签到

发表于 2018-10-1 11:53:19 | 显示全部楼层 |阅读模式
  条件:MySQL 版本:percona server 5.5.18

  •   sql优化案例一:

  xxx@xx 5.5.18-log cayenne 11:30:37>desc select>  *************************** 1. row ***************************

  >  select_type: SIMPLE
  table: credit_amount
  type: index_merge
  possible_keys: memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx
  key: memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx
  key_len: 758,399
  ref: NULL
  rows: 2
  Extra: Using union(memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx); Using where
  1 row in set (0.00 sec)
  #

  xxx@xx 5.5.18-log cayenne 11:30:06>desc select>  *************************** 1. row ***************************

  >  select_type: SIMPLE
  table: credit_amount
  type: index_merge
  possible_keys: grant_task_id_index,memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx
  key: memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx,grant_task_id_index
  key_len: 758,399,9
  ref: NULL
  rows: 3
  Extra: Using sort_union(memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx,grant_task_id_index); Using where
  1 row in set (0.01 sec)
  #此时range 和 index merge 一起使用了,但是mysql 不是这样的 http://dev.mysql.com/doc/refman/5.6/en/index-merge-optimization.html
  Before MySQL 5.6.6, if a range scan is possible on some key, the optimizer will not consider using Index Merge Union or  Index Merge Sort-Union algorithms. For example, consider this query:
SELECT * FROM t1 WHERE (goodkey1 < 10 OR goodkey2 < 20) AND badkey < 30;  3.index merge的bug:
  -----------------------------------------------------------------------------------------
  KEY `topic_id` (`topic_id`),
  KEY `forum_id` (`forum_id`)
  KEY `tid_fid` (`topic_id`,`forum_id`)
  SELECT count(*) FROM phpbb_posts WHERE topic_id = 110126 AND forum_id = 19;
mysql> EXPLAIN SELECT post_id, topic_id, forum_id FROM phpbb_posts WHERE topic_id = 110126 AND forum_id = 19 ORDER BY post_time ASC LIMIT 1 \G  
*************************** 1. row ***************************
  
           id: 1
  
  select_type: SIMPLE
  
        table: phpbb_posts
  
         type: index_merge
  
possible_keys: topic_id,tid_post_time,tid_fid,fid_tid_post_time,forum_id
  
          key: topic_id,forum_id
  
      key_len: 3,3
  
          ref: NULL
  
         rows: 1592
  
        Extra: Using intersect(topic_id,forum_id); Using where; Using filesort
  
1 row in set (0.29 sec)
#bug详见: http://bugs.mysql.com/bug.php?id=65274 在5.6.7 已经修复了。sql优化案例二:  
二.因为其他索引的影响导致优化器不走index_merge:
  
xxx@xx 5.5.18-log cayenne 05:37:47>desc select id, grant_credit_task_id, product_id, product_code, user_id, member_id, user_credit_money, product_credit_money, real_product_credit_money, credit_start_date, credit_end_date, credit_expire, repayment_way_id, repayment_way, rate_type_id, rate_type, float_way_id, float_way, float_percent, do_credit_rate, free_rate_day, installment_fees, delay_payment_fine_float, installment_fess_float, delay_payment_fine, task_type_code, task_type, status_code, status, bulid_way, create_user_id, create_user, create_time, start_time, finish_time, import_batch_id, credit_status_code, credit_status, check_type, check_type_code, fail_type, fail_type_code, effective_reason_code, effective_reason, amount_type_code, amount_type, product_name, product_type, product_type_code, user_type, certificate_type, certificate_no, approver_id, approver_name, update_time, is_activated, activated_time from credit_amount WHERE ( product_code = '1' and member_id = 'chgq2008' and credit_status_code = 'SXZT001' ) or( certificate_no = '152822198003100051' and product_code = '1' and credit_status_code = 'SXZT001' )\G
  
*************************** 1. row ***************************
  
           id: 1
  
  select_type: SIMPLE
  
        table: credit_amount
  
         type: ref
  
possible_keys: memberid_productcode_credit_code_idx,certno_productcode_credit_code_idx,certificateno_idx,creditstatuscode_creditenddate_idx,productcode_updatetime_idx
  
          key: creditstatuscode_creditenddate_idx
  
      key_len: 153
  
          ref: const
  
         rows: 5960990
  
        Extra: Using where
  
1 row in set (0.00 sec)
此时并没有index_merge,因为有索引creditstatuscode_creditenddate_idx,而or的前后都有列 credit_status_code,索引如下:| credit_amount |          1 | memberid_productcode_credit_code_idx |            1 | member_id            | A         |    11921980 |     NULL | NULL   |      | BTREE      |         |               |  
| credit_amount |          1 | memberid_productcode_credit_code_idx |            2 | product_code         | A         |    11921980 |     NULL | NULL   | YES  | BTREE      |         |               |
  
| credit_amount |          1 | memberid_productcode_credit_code_idx |            3 | credit_status_code   | A         |    11921980 |     NULL | NULL   | YES  | BTREE      |         |               |
  
| credit_amount |          1 | certno_productcode_credit_code_idx   |            1 | certificate_no       | A         |     1490247 |     NULL | NULL   | YES  | BTREE      |         |               |
  
| credit_amount |          1 | certno_productcode_credit_code_idx   |            2 | product_code         | A         |     1490247 |     NULL | NULL   | YES  | BTREE      |         |               |
  
| credit_amount |          1 | certno_productcode_credit_code_idx   |            3 | credit_status_code   | A         |     1490247 |
  
| credit_amount |          1 | creditstatuscode_creditenddate_idx   |            1 | credit_status_code   | A         |          18 |     NULL | NULL   | YES  | BTREE      |         |               |
  
| credit_amount |          1 | creditstatuscode_creditenddate_idx   |            2 | credit_end_date      | A         |          18 |
  改成union all 后是可以走union all索引的:

  xxx@xx 5.5.18-log cayenne 05:40:47>desc select>union all select >  *************************** 1. row ***************************

  >  select_type: PRIMARY
  table: credit_amount
  type: ref
  possible_keys: memberid_productcode_credit_code_idx,creditstatuscode_creditenddate_idx,productcode_updatetime_idx
  key: memberid_productcode_credit_code_idx
  key_len: 758
  ref: const,const,const
  rows: 1
  Extra: Using where
  *************************** 2. row ***************************

  >  select_type: UNION
  table: credit_amount
  type: ref
  possible_keys: certno_productcode_credit_code_idx,certificateno_idx,creditstatuscode_creditenddate_idx,productcode_updatetime_idx
  key: certno_productcode_credit_code_idx
  key_len: 399
  ref: const,const,const
  rows: 1
  Extra: Using where
  *************************** 3. row ***************************

  >  select_type: UNION RESULT
  table:
  type: ALL
  possible_keys: NULL
  key: NULL
  key_len: NULL
  ref: NULL
  rows: NULL
  Extra:
  3 rows in set (0.00 sec)
  总结:以后同样的这种sql 要用union all,防止优化器走错索引。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-607099-1-1.html 上篇帖子: Using Sqoop2 to import mysql data to HDFS 下篇帖子: Mysql InnoDB 引发 Waiting for query cache lock-Web运维
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表