Mysql索引讀書筆記(待續)
##################################
高效能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索引筆記
- 劍指Offer讀書筆記(持續更新中)筆記
- GDI+學習筆記--未完待續筆記
- 小程式學習筆記(未完待續)筆記
- 《Mysql必知必會》讀書筆記MySql筆記
- 讀書筆記...筆記
- 讀書筆記筆記
- 《讀書與做人》讀書筆記筆記
- 《Python 簡明教程》讀書筆記(持續更新)Python筆記
- MySQL 讀書筆記 (一) 體系結構MySql筆記
- mysql--平日點滴知識隨筆(待續)MySql
- Mysql技術內幕InnoDB儲存引擎讀書筆記--《五》索引與演算法MySql儲存引擎筆記索引演算法
- 讀書筆記|Windows 除錯原理學習|持續更新筆記Windows除錯
- Cucumber讀書筆記筆記
- 散文讀書筆記筆記
- HTTP 讀書筆記HTTP筆記
- CoreJava讀書筆記-------Java筆記
- flask讀書筆記Flask筆記
- Vue讀書筆記Vue筆記
- MONGODB 讀書筆記MongoDB筆記
- Qt讀書筆記QT筆記
- Node讀書筆記筆記
- SAP讀書筆記筆記
- YII讀書筆記筆記
- iptables 讀書筆記筆記
- Makefile 讀書筆記筆記
- 鎖讀書筆記筆記
- dataguard讀書筆記筆記
- 讀書筆記3筆記
- 讀書筆記2筆記
- MySQL 筆記 - 索引優化MySql筆記索引優化
- MySQL 8.0 Reference Manual(讀書筆記90節--Replication)MySql筆記
- s3-api閱讀筆記(待整理)S3API筆記