zoukankan      html  css  js  c++  java
  • MySQL没有选对正确的索引怎么办

    1.optimize table
    如果MySQL没有选中正确的索引,有可能是因为表经常被更改。这会影响统计数据。如果时间允许(表在此期间是锁定的),我们可以通过重新构建表来帮助解决这个问题。

    具体可以参考之前的文章:mysql optimize table

    2.analyze table
    analyze table需要的时间更少,尤其是在InnoDB中。分析将更新索引统计信息并帮助生成更好的查询计划。

    3.使用hint

    比如使用关键字use index(index-name)、force index(index-name)

    select c1 from abce use index(idx_c1)  where ... ;
    select c1 from abce force index(idx_c1)  where ... ;
    

      

    4.忽略索引

    如果使用了错误的索引,可以尝试使用关键字来忽略使用被选中的索引。比如,让sql忽略掉主键:

    select id from abce ignore index(primary)  where ... ;
    

      

    5.修改业务的逻辑结构,从而修改sql语句

    6.否定索引的使用

    select id, type, age from abce
    where type=12345 and age > 3
    order by id+0;#这样id+0是函数运算,就不使用id上的索引了

    7.修改sql的结构,使得优化器认为选择该索引的代价会更高

    select id, type, age from abce
    where type=12345 and age > 3
    order by id;
    #修改成
    select id, type, age from abce
    where type=12345 and age > 3
    order by id,type, age;

    这样,可能机会导致优化器认为选择使用id上的索引的代价会更高。从而选择其他索引。

    (以上id只是用作示例,并不一定是主键的列)

    8.有时候可能是bug,报bug,等待官方修复。

  • 相关阅读:
    POJ 3268 Silver Cow Party (Dijkstra)
    怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
    CF Amr and Music (贪心)
    CF Amr and Pins (数学)
    POJ 3253 Fence Repair (贪心)
    POJ 3069 Saruman's Army(贪心)
    POJ 3617 Best Cow Line (贪心)
    CF Anya and Ghosts (贪心)
    CF Fox And Names (拓扑排序)
    mysql8.0的新特性
  • 原文地址:https://www.cnblogs.com/abclife/p/12728005.html
Copyright © 2011-2022 走看看