Mysql索引读书笔记

作者: 飞翔的Tallgeese | 来源:发表于2018-03-05 17:12 被阅读1次

##################################

高性能Mysql第5章:索引基础

##################################

eg.

不加索引,走全表

mysql> explain select ifnull(sum(if(a.trade_type in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back'), 1, 0)), 0) as fundacount, ifnull(sum(if(a.trade_type in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back'), a.amount, 0)), 0) as fundamount, 0 as withdrawacount, 0 as withdrawamount, ifnull(sum(if(a.trade_type not in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back', 'withdraw'), 1, 0)), 0) as tradeacount , ifnull(sum(if(a.trade_type not in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back', 'withdraw'), a.amount, 0)), 0) as tradeamount from act_account_change a where 1 = 1 and a.user_id = '18020514102900300019' and a.change_direction = 'in' and a.create_time >= '2018-03-01 00:00:00' and a.create_time <= '2018-03-02 00:00:00';

+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows  | Extra      |

+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+

|  1 | SIMPLE      | a    | ALL  | NULL          | NULL | NULL    | NULL | 753975 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+

1 row in set (0.00 sec)

mysql> explain select id, create_time, update_time, user_id, user_name , order_type, trade_type, account_type, account_no, change_type , change_direction, amount, balance, status, ref_user_id , ref_account_no, memo, comments, gid, merchant_no , order_no, partner_id, transfer_type from act_account_change where user_id = '18011009184100300278' and create_time >= '2018-02-27 00:00:00' order by id desc;

+----+-------------+--------------------+-------+---------------+---------+---------+------+--------+-------------+

| id | select_type | table              | type  | possible_keys | key    | key_len | ref  | rows  | Extra      |

+----+-------------+--------------------+-------+---------------+---------+---------+------+--------+-------------+

|  1 | SIMPLE      | act_account_change | index | NULL          | PRIMARY | 8      | NULL | 753975 | Using where |

+----+-------------+--------------------+-------+---------------+---------+---------+------+--------+-------------+

1 row in set (0.01 sec)

对时间字段做索引

mysql> create index idx_test on act_account_change(create_time);

mysql> explain select id, create_time, update_time, user_id, user_name , order_type, trade_type, account_type, account_no, change_type , change_direction, amount, balance, status, ref_user_id , ref_account_no, memo, comments, gid, merchant_no , order_no, partner_id, transfer_type from act_account_change where user_id = '18011009184100300278' and create_time >= '2018-02-27 00:00:00' order by id desc;

+----+-------------+--------------------+-------+---------------+----------+---------+------+-------+---------------------------------------------------------------+

| id | select_type | table              | type  | possible_keys | key      | key_len | ref  | rows  | Extra                                                        |

+----+-------------+--------------------+-------+---------------+----------+---------+------+-------+---------------------------------------------------------------+

|  1 | SIMPLE      | act_account_change | range | idx_test      | idx_test | 6      | NULL | 93422 | Using index condition; Using where; Using MRR; Using filesort |

+----+-------------+--------------------+-------+---------------+----------+---------+------+-------+---------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> explain select ifnull(sum(if(a.trade_type in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back'), 1, 0)), 0) as fundacount, ifnull(sum(if(a.trade_type in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back'), a.amount, 0)), 0) as fundamount, 0 as withdrawacount, 0 as withdrawamount, ifnull(sum(if(a.trade_type not in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back', 'withdraw'), 1, 0)), 0) as tradeacount , ifnull(sum(if(a.trade_type not in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back', 'withdraw'), a.amount, 0)), 0) as tradeamount from act_account_change a where 1 = 1 and a.user_id = '18020514102900300019' and a.change_direction = 'in' and a.create_time >= '2018-03-01 00:00:00' and a.create_time <= '2018-03-02 00:00:00';

+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------------------------------+

| id | select_type | table | type  | possible_keys | key      | key_len | ref  | rows  | Extra                                        |

+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------------------------------+

|  1 | SIMPLE      | a    | range | idx_test      | idx_test | 6      | NULL | 17412 | Using index condition; Using where; Using MRR |

+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------------------------------+

1 row in set (0.00 sec)

把排序字段和索引字段一起写入聚簇索引

mysql> create index idx_uid_time_id on act_account_change(user_id,create_time,id);

mysql> explain select id, create_time, update_time, user_id, user_name , order_type, trade_type, account_type, account_no, change_type , change_direction, amount, balance, status, ref_user_id , ref_account_no, memo, comments, gid, merchant_no , order_no, partner_id, transfer_type from act_account_change where user_id = '18011009184100300278' and create_time >= '2018-02-27 00:00:00' order by id desc;                          +----+-------------+--------------------+-------+-----------------+-----------------+---------+------+------+---------------------------------------+

| id | select_type | table              | type  | possible_keys  | key            | key_len | ref  | rows | Extra                                |

+----+-------------+--------------------+-------+-----------------+-----------------+---------+------+------+---------------------------------------+

|  1 | SIMPLE      | act_account_change | range | idx_uid_time_id | idx_uid_time_id | 105    | NULL |    8 | Using index condition; Using filesort |

+----+-------------+--------------------+-------+-----------------+-----------------+---------+------+------+---------------------------------------+

1 row in set (0.00 sec)

mysql> explain select ifnull(sum(if(a.trade_type in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back'), 1, 0)), 0) as fundacount, ifnull(sum(if(a.trade_type in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back'), a.amount, 0)), 0) as fundamount, 0 as withdrawacount, 0 as withdrawamount, ifnull(sum(if(a.trade_type not in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back', 'withdraw'), 1, 0)), 0) as tradeacount , ifnull(sum(if(a.trade_type not in ('deduct_deposit', 'net_deposit', 'offline_pay', 'deposit_back', 'withdraw'), a.amount, 0)), 0) as tradeamount from act_account_change a where 1 = 1 and a.user_id = '18020514102900300019' and a.change_direction = 'in' and a.create_time >= '2018-03-01 00:00:00' and a.create_time <= '2018-03-02 00:00:00';

+----+-------------+-------+-------+-----------------+-----------------+---------+------+------+------------------------------------+

| id | select_type | table | type  | possible_keys  | key            | key_len | ref  | rows | Extra                              |

+----+-------------+-------+-------+-----------------+-----------------+---------+------+------+------------------------------------+

|  1 | SIMPLE      | a    | range | idx_uid_time_id | idx_uid_time_id | 105    | NULL |    3 | Using index condition; Using where |

+----+-------------+-------+-------+-----------------+-----------------+---------+------+------+------------------------------------+

1 row in set (0.00 sec)

扫描行数从75W锐减到3和8,效果明显;

查询变化为:1.29s和0.68s→0.05s和0.12s→0.03s和0.04s

目标达成

相关文章

  • MySQL索引及查询优化书目录

    MySQL索引的原理之索引目的 MySQL索引的原理之索引原理 MySQL索引的原理之索引的类型 MySQL索引的...

  • 高性能的索引策略

    MySQL查询基础-查询执行过程 MySQL聚簇索引 MySQL覆盖索引 MySQL索引扫描排序 MySQL冗余和...

  • MySQL索引的使用

    MySQL索引 MySQL索引可以快速提高MySQL的检索速度。索引分单列索引和组合索引单列索引:即一个索引只包含...

  • Mysql索引与锁

    本文以Mysql5.7为例测试。 1:mysql索引方法 Mysql的索引方法分为btree索引和hash索引。 ...

  • 索引(二)

    mysql索引的新手入门详解mysql索引之三:索引使用注意规则 索引(Index)是帮助 MySQL 高效获取数...

  • MySQL 索引分类

    MySQL索引的分类(根据数据结构) 索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL...

  • MySQL--索引

    MySQL索引 查看索引 创建索引 创建唯一索引 创建主键索引 删除索引 删除主键 MySQL视图 创建视图 删除...

  • mysql索引

    索引 mysql索引的建立对于mysql的高效运行是很重要的,索引可以大大提高mysql的检索速度。索引分单列索引...

  • 5.2MySQL创建高性能索引考察点

    MySQL索引的基础和类型延伸:MySQL索引的创建原则延伸:MySQL索引的注意事项 索引的基础索引类似于书籍的...

  • MySql 数据查询优化

    1. MySQL索引类型: mysql的索引有5种:主键索引、普通索引、唯一索引、全文索引、聚合索引(多列索引)。...

网友评论

    本文标题:Mysql索引读书笔记

    本文链接:https://www.haomeiwen.com/subject/zdihfftx.html