mysql 索引优化法则

建表语句

CREATE TABLE staffs(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR (24) NOT NULL DEFAULT '' COMMENT '姓名',
age INT NOT NULL DEFAULT 0 COMMENT '年龄',
pos VARCHAR (20) NOT NULL DEFAULT 0 COMMENT '职位',
add_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入职时间'
) CHARSET utf8 COMMENT '员工记录表';

INSERT INTO staffs(NAME, age, pos, add_time) VALUES('ronnie', 22, 'manager', NOW());
INSERT INTO staffs(NAME, age, pos, add_time) VALUES('John', 23, 'devloper', NOW());
INSERT INTO staffs(NAME, age, pos, add_time) VALUES('2181', 27, 'devloper', NOW());

-- 建复合索引
alter table staffs add index idx_staffs_nameAgePos(name, age, pos);

mysql 优化法则

  1. 全值匹配我最爱(Kappa)
  2. 遵循最佳左前缀法则
    • 如果索引了多列, 查询从索引的最左前列开始并且不跳过索引中的列。
    • 其实索引本质是个单向链表, 你要先得到头才能逐渐往后取后面的, 中间断了就走不了后面的索引了。
    • 正确使用 Demo:
      mysql> explain select * from staffs where name = 'ronnie';
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref   | rows | filtered | Extra |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | const |    1 |   100.00 | NULL  |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name = 'ronnie' and age = 22;
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------------+------+----------+-------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref         | rows | filtered | Extra |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------------+------+----------+-------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 78      | const,const |    1 |   100.00 | NULL  |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------------+------+----------+-------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name = 'ronnie' and age = 22 and pos = 'manager';
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------------------+------+----------+-------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref               | rows | filtered | Extra |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------------------+------+----------+-------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 140     | const,const,const |    1 |   100.00 | NULL  |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------------------+------+----------+-------+
      1 row in set, 1 warning (0.00 sec)
      
    • 违反使用Demo:(完全失效)[带头大哥死了]:
      mysql> explain select * from staffs where age = 22 and pos = 'manager';
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where age = 22;
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where pos = 'manager';
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      • 完全失效, 执行了全表扫描。
    • 违反使用Demo(部分失效)[中间兄弟断了]:
      mysql> explain select * from staffs where name = 'ronnie' and pos = 'manager';
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-----------------------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref   | rows | filtered | Extra                 |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-----------------------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | const |    1 |    33.33 | Using index condition |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-----------------------+
      1 row in set, 1 warning (0.00 sec)
      
      • 使用了复合索引 idx_staffs_nameAgePos, 但 reference 只有一个常量参数, 只用了最左侧的 name, 没用到 pos。
  3. 不在索引列上做任何操作(计算、函数、(自动 or 手动) 类型转换), 会导致索引失效而转向全表扫描
    • 索引列上执行函数Demo:

      mysql> explain select * from staffs where left(name,4) = 'John';
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |   100.00 | Using where |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      • 可以看到执行了全表扫描, 索引失效。
    • 索引列上执行计算Demo:
      
      mysql> explain select * from staffs where char_length('name') > 3;
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |   100.00 | NULL  |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
      1 row in set, 1 warning (0.00 sec)
      
      • 可以看到执行了全表扫描, 索引失效。
    • 索引列上执行类型转换Demo:
      mysql> select * from staffs where name = 2181;
      +----+------+-----+----------+---------------------+
      | id | NAME | age | pos      | add_time            |
      +----+------+-----+----------+---------------------+
      |  3 | 2181 |  27 | devloper | 2020-01-10 11:38:08 |
      +----+------+-----+----------+---------------------+
      1 row in set, 2 warnings (0.00 sec)
      
      mysql> explain select * from staffs where name = 2181;
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    33.33 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 3 warnings (0.00 sec)
      
      mysql> explain select * from staffs where name = '2181';
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref   | rows | filtered | Extra |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | const |    1 |   100.00 | NULL  |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
      1 row in set, 1 warning (0.00 sec)
      • 这种就是比较坑的, 平时写sql是不注意, varChar类型请务必加上‘ ‘, 如果不加, mysql底层会进行类型的隐式转换, 造成索引失效。
  4. 存储引擎不能使用索引中范围条件右边的列
    • 范围之后全失效, 所以建索引是一门学问, 需要对常用的业务sql进行综合考量。
    • Demo:
      mysql> explain select * from staffs where name = 'John' and age > 22 and pos = 'devloper';
      +----+-------------+--------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+-----------------------+
      | id | select_type | table  | partitions | type  | possible_keys         | key                   | key_len | ref  | rows | filtered | Extra                 |
      +----+-------------+--------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+-----------------------+
      |  1 | SIMPLE      | staffs | NULL       | range | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 78      | NULL |    1 |    33.33 | Using index condition |
      +----+-------------+--------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+-----------------------+
      1 row in set, 1 warning (0.00 sec)
      • 我们的索引是这样建的: name -> age -> pos
      • 由于中间的age进行了范围查询, 会导致后续的索引全部失效。
  5. 尽量使用覆盖索引(只访问索引的查询(索引列和查询列一致)), 减少select *
    • 对比Demo:

      mysql> explain select * from staffs where name = 'John' and pos = 'devloper';
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-----------------------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref   | rows | filtered | Extra                 |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-----------------------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | const |    1 |    33.33 | Using index condition |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+-----------------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select name, age, pos from staffs where name = 'John' and pos = 'devloper';
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+--------------------------+
      | id | select_type | table  | partitions | type | possible_keys         | key                   | key_len | ref   | rows | filtered | Extra                    |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+--------------------------+
      |  1 | SIMPLE      | staffs | NULL       | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | const |    1 |    33.33 | Using where; Using index |
      +----+-------------+--------+------------+------+-----------------------+-----------------------+---------+-------+------+----------+--------------------------+
      1 row in set, 1 warning (0.00 sec)
      
      • 可以看到extra多了 Using index, 表面select操作中使用了覆盖索引(Covering Index), 避免了访问表的数据行。
  6. mysql 在使用不等于(!= 或者 <>) 的时候无法使用索引会导致全表扫描
    • Demo:

      mysql> explain select * from staffs where name != 'John';
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    66.67 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name <> 'John';
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    66.67 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
  7. is null, is not null 也无法使用索引
    • 底层原因是: null 不能转换为索引表示的数字来索引
    • Demo:
      mysql> explain select * from staffs where name is null;
      +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
      | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra            |
      +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
      |  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Impossible WHERE |
      +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name is not null;
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    66.67 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
  8. like 以通配符开头(‘%abc...‘) mysql 索引失效会变成全表扫描的操作
    • Demo:

      mysql> explain select * from staffs where name like '%ronnie';
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name like '%ronnie%';
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
      +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name like 'ronnie%';
      +----+-------------+--------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+-----------------------+
      | id | select_type | table  | partitions | type  | possible_keys         | key                   | key_len | ref  | rows | filtered | Extra                 |
      +----+-------------+--------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+-----------------------+
      |  1 | SIMPLE      | staffs | NULL       | range | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | NULL |    1 |   100.00 | Using index condition |
      +----+-------------+--------+------------+-------+-----------------------+-----------------------+---------+------+------+----------+-----------------------+
      1 row in set, 1 warning (0.00 sec)
      • 可以看到只有% 在右边的查询使用了索引
      • 这个情况就比较蛋疼, 如果你们业务上就需要百分号在左边, 需要使用覆盖索引来优化。
        mysql> explain select id, name from staffs where name like '%ronnie%';
        +----+-------------+--------+------------+-------+---------------+-----------------------+---------+------+------+----------+--------------------------+
        | id | select_type | table  | partitions | type  | possible_keys | key                   | key_len | ref  | rows | filtered | Extra                    |
        +----+-------------+--------+------------+-------+---------------+-----------------------+---------+------+------+----------+--------------------------+
        |  1 | SIMPLE      | staffs | NULL       | index | NULL          | idx_staffs_nameAgePos | 140     | NULL |    3 |    33.33 | Using where; Using index |
        +----+-------------+--------+------------+-------+---------------+-----------------------+---------+------+------+----------+--------------------------+
        1 row in set, 1 warning (0.00 sec)
      • 做查询如果数据量大的话 用 Lucene, Solr, ElasticSearch 不香吗?
  9. 字符串不加单引号索引失效(mysql会自己做隐式转换)
    • Demo 同3
  10. 少用or, 用它来连接时会索引失效
    • Demo:

      mysql> explain select * from staffs where name = 'John' or name = 'ronnie';
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    66.67 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name = 'John' or age = 23;
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    55.56 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.00 sec)
      
      mysql> explain select * from staffs where name = 'John' or pos = 'manager';
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      | id | select_type | table  | partitions | type | possible_keys         | key  | key_len | ref  | rows | filtered | Extra       |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      |  1 | SIMPLE      | staffs | NULL       | ALL  | idx_staffs_nameAgePos | NULL | NULL    | NULL |    3 |    55.56 | Using where |
      +----+-------------+--------+------------+------+-----------------------+------+---------+------+------+----------+-------------+
      1 row in set, 1 warning (0.01 sec)
      

原文地址:https://www.cnblogs.com/ronnieyuan/p/12176093.html

时间: 2024-08-14 05:53:02

mysql 索引优化法则的相关文章

mysql索引优化

mysql 索引优化 >mysql一次查询只能使用一个索引.如果要对多个字段使用索引,建立复合索引. >越小的数据类型通常更好:越小的数据类型通常在磁盘.内存和CPU缓存中都需要更少的空间,处理起来更快. >简单的数据类型更好:整型数据比起字符,处理开销更小,因为字符串的比较更复杂.在MySQL中,应该用内置的日期和时间数据类型,而不是用字符串来存储时间:以及用整型数据类型存储IP地址. >尽量避免NULL:应该指定列为NOT NULL,除非你想存储NULL.在MySQL中,含有空

MySQL索引优化-from 高性能MYSQL

Btree: 1. 尽量使用覆盖索引, 即三星索引 2. 多列索引如果带范围的话, 后续列不会作为筛选条件 3. 多列索引应选择过滤性更好的充当前缀索引 4. 尽量按主键顺序插入, 减少页分裂, 采用自增ID在高并发情况下, 可能造成明显征用, 或者更改innodb_autoinc_lock_mode配置. Hash: 1.只有精确匹配所有列的查询才有效, 对于每行数据, 引擎都会对所有索引列计算hash码 2. 只有memory才可以支持hash索引, innodb支持自适应hash索引, 但

【转载】MySQL索引优化

MySQL索引优化 原文链接 MySQL官方对索引的定义:索引是帮助MySQL高效获取数据的数据结构.索引是在存储引擎中实现的,所以每种存储引擎中的索引都不一样.如MYISAM和InnoDB存储引擎只支持BTree索引:MEMORY和HEAP储存引擎可以支持HASH和BTREE索引. 这里仅针对常用的InnoDB存储引擎所支持的BTree索引进行介绍: 一.索引类型 先创建一个新表,用于演示索引类型 CREATE TABLE index_table ( id BIGINT NOT NULL au

Mysql 索引优化分析

MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字段的意义.助你了解索引,分析索引,使用索引,从而写出更高性能的sql语句.还在等啥子?撸起袖子就是干! 案例分析 我们先简单了解一下非关系型数据库和关系型数据库的区别. MongoDB是NoSQL中的一种.NoSQL的全称是Not only SQL,非关系型数据库.它的特点是性能高,扩张性强,模式灵

mySql索引优化分析

MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字段的意义.助你了解索引,分析索引,使用索引,从而写出更高性能的sql语句.还在等啥子?撸起袖子就是干! 案例分析 我们先简单了解一下非关系型数据库和关系型数据库的区别.MongoDB是NoSQL中的一种.NoSQL的全称是Not only SQL,非关系型数据库.它的特点是性能高,扩张性强,模式灵活

MySQL索引优化步骤总结

在项目使用mysql过程中,随着系统的运行,发现一些慢查询,在这里总结一下mysql索引优化步骤 1.开发过程优化 开发过程中对业务表中查询sql分析sql执行计划(尤其是业务流水表),主要是查看sql执行计划,对sql进行优化. explain执行计划关键属性 select_type,possible_keys,key,rows (1) select_type 访问类型 system>const > eq_ref > ref > fulltext > ref_or_null

MySQL索引优化与分析(重要)

建表SQL CREATE TABLE staffs ( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR (24) NULL DEFAULT '' COMMENT '姓名', age INT NOT NULL DEFAULT 0 COMMENT '年龄', pos VARCHAR (20) NOT NULL DEFAULT '' COMMENT '职位', add_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIM

MySQL数据库优化法则总结

网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正以及补充 这篇文章我花费了大量的时间查找资料.修改希望大家阅读之后,感觉好的话推荐给更多的人,让更多的人看到.纠正以及补充. 要正确的优化SQL,我们需要快速定位能性的瓶颈点,也就是说快速找到我们SQL主要的开销在哪里?而大多数情况性能最慢的设备会是瓶颈点,如下载时网络速度可能会是瓶颈点,本地复制文件时硬盘可能会是瓶颈点,为什么这些一般的工作我们能快速确认瓶颈点呢,因为我们对

MySQL 索引优化原则

一.索引优化原则 1.最左前缀匹配原则,联合索引,mysql会从做向右匹配直到遇到范围查询(>.<.between.like)就停止匹配,比如a = 1 and b = 2 and c > 3 and d = 4 如果建立(a,b,c,d)顺序的索引,d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用到,a,b,d的顺序可以任意调整. 2.=和in可以乱序,比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mysql的查询优化器会帮你优