zoukankan      html  css  js  c++  java
  • mysql use index、ignore index、force index用法


    原创你去了哪里 最后发布于2019-10-18 14:05:48 阅读数 121  收藏
    展开
    1:use index:在你查询语句表名的后面,添加use index来提供你希望mysql去参考的索引列表,就可以让mysql不再考虑其他可用的索引。如:select * from table use index(name,age);

    2:IGNORE INDEX 提示会禁止查询优化器使用指定的索引。在具有多个索引的查询时,可以用来指定不需要优化器使用的那个索引,还可以在删除不必要的索引之前在查询中禁止使用该索引。如:select * from table ignore index(name,age);

    3:force index:强制mysql使用一个特定的索引。一般情况下mysql会根据统计信息选择正确的索引,但是当查询优化器选择了错误的索引或根本没有使用索引的时候,这个提示将非常有用。

    需要注意的是use/ignore/force index(index)这里括号里的index是索引名,而不是列名。而且后面必须要加上where条件。

    先查看表索引信息:
    show index from student2G
    *************************** 1. row ***************************
            Table: student2
       Non_unique: 0
         Key_name: PRIMARY
     Seq_in_index: 1
      Column_name: id
        Collation: A
      Cardinality: 4
         Sub_part: NULL
           Packed: NULL
             Null:
       Index_type: BTREE
          Comment:
    Index_comment:
          Visible: YES
       Expression: NULL
    *************************** 2. row ***************************
            Table: student2
       Non_unique: 1
         Key_name: se
     Seq_in_index: 1
      Column_name: sex
        Collation: A
      Cardinality: 4
         Sub_part: NULL
           Packed: NULL
             Null: YES
       Index_type: BTREE
          Comment:
    Index_comment:
          Visible: YES
       Expression: NULL
    2 rows in set (0.07 sec)
    可以看出列sex上有个普通索引,索引名是se。

    explain select * from student2 where sex='a';
    +----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
    | id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref   | rows | filtered | Extra |
    +----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
    |  1 | SIMPLE      | student2 | NULL       | ref  | se            | se   | 2       | const |    1 |   100.00 | NULL  |
    +----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
    1 row in set, 1 warning (0.00 sec)

    使用到了sex列的索引,如果我们不想它sql语句使用sex列的索引,可以这样写:
    explain select * from student2 ignore index(se) where sex='a';
    +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
    | id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
    +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
    |  1 | SIMPLE      | student2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |    33.33 | Using where |
    +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
    1 row in set, 1 warning (0.00 sec)
    可以看出这次并没有使用到索引。

    如果你这样写sql就会报错:explain select * from student2 ignore index(sex) where sex='a';
    ERROR 1176 (42000): Key 'sex' doesn't exist in table 'student2'  
    因为ignore index括号里要写索引名,而不是列明。
    ————————————————
    版权声明:本文为CSDN博主「你去了哪里」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_43740552/article/details/102623930

  • 相关阅读:
    oracle 导入数据语句
    移动上去换样式代码
    google suggest 代码例子
    删除一个表的字段的sql语句命令
    将json从前台传到后台注意问题
    eclipse 自动 getter setter 注释
    jsp界面获取地址栏参数
    常见的正则表达式验证
    JSTL 核心标签库
    javascript中的call和apply两个方法的区别
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/12690281.html
Copyright © 2011-2022 走看看