zoukankan      html  css  js  c++  java
  • mysql优化查询

    使用索引查询

    MariaDB [test]> explain select * from te where id=22;                   #在没有增加索引情况下,rows为7,即查询行数
    +------+-------------+-------+------+---------------+------+---------+------+------+-------------+
    | id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +------+-------------+-------+------+---------------+------+---------+------+------+-------------+
    |    1 | SIMPLE      | te    | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using where |
    +------+-------------+-------+------+---------------+------+---------+------+------+-------------+
    1 row in set (0.01 sec)
    
    MariaDB [test]> alter table te add index tindex(id);                    #增加索引
    Query OK, 0 rows affected (0.09 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    MariaDB [test]> explain select * from te where id=22;                   #rows变为1
    +------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
    | id   | select_type | table | type | possible_keys | key    | key_len | ref   | rows | Extra |
    +------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
    |    1 | SIMPLE      | te    | ref  | tindex        | tindex | 5       | const |    1 |       |
    +------+-------------+-------+------+---------------+--------+---------+-------+------+-------+
    1 row in set (0.00 sec)
    
    MariaDB [test]> 
    

    索引使用注意事项
    使用like关键字查询时,%号位于首位会导致无法使用索引查询,否则可以正常使用索引查询,如下
    这里写图片描述
    多列索引问题

    MariaDB [test]> alter table te add index indextwo(id,name);         #创建索引
    Query OK, 0 rows affected (0.06 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    MariaDB [test]> show create table teG;
    *************************** 1. row ***************************
           Table: te
    Create Table: CREATE TABLE `te` (
      `id` int(11) DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL,
      `se` varchar(3) DEFAULT 'man',
      `city` varchar(3) DEFAULT 'gx',
      KEY `indextwo` (`id`,`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)
    
    ERROR: No query specified
    
    MariaDB [test]> explain select * from te where name='ds';                       #rows为7
    +------+-------------+-------+------+---------------+------+---------+------+------+-------------+
    | id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +------+-------------+-------+------+---------------+------+---------+------+------+-------------+
    |    1 | SIMPLE      | te    | ALL  | NULL          | NULL | NULL    | NULL |    7 | Using where |
    +------+-------------+-------+------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    MariaDB [test]> explain select * from te where id=3 and name='ds';              #增加多列索引的首列后,可以正常使用索引
    +------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
    | id   | select_type | table | type | possible_keys | key      | key_len | ref         | rows | Extra                 |
    +------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
    |    1 | SIMPLE      | te    | ref  | indextwo      | indextwo | 28      | const,const |    1 | Using index condition |
    +------+-------------+-------+------+---------------+----------+---------+-------------+------+-----------------------+
    1 row in set (0.00 sec)
    
    MariaDB [test]> 
    
  • 相关阅读:
    Thread中带参方法无法使用之解决方案
    项目相关的风险要素及分类
    AspNetPager分页示例之DataGrid(PostBack分页)
    Substitution 类 (asp.net 2.0 )
    自定义HTTP处理程序显示图片(asp.net 2.0)
    常见文件扩展名和它们的说明
    基于.NET的开源GIS项目(转)
    项目开发流程标准
    AOP(Aspect Oriented Programming) 面向方面编程
    项目实施及管理标准
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730497.html
Copyright © 2011-2022 走看看