MySQL初心者に贈るインデックスチューニングのポイントまとめ2014 | サイバーエージェント 公式エンジニアブログ
(@strsk)AmebaAA

MySQL
使

InnoDBMyISAM使


使
DBWebCPU1CPU



long_query_time10.8
10.50.1調

log_queries_not_using_indexeslong_query_time使使

/etc/my.cnf
slow_query_log = 1
slow_query_log_file = mysql-slow.log
long_query_time = 0.1
#log_queries_not_using_indexes


mysql> set global slow_query_log = 1;
mysql> set global long_query_time=0.1;


# Time: 140829  3:34:08
# User@Host: root[root] @ localhost [] Id: 2156371
# Query_time: 23.355119 Lock_time: 0.000034 Rows_sent: 1 Rows_examined: 28057953
SET timestamp=1409250848;
select count(*) from hoge;


便1

/etc/logrotate.d/mysql
/var/lib/mysql/mysql-slow.log {
create 644 mysql mysql
notifempty
daily
rotate 30
missingok
compress
delaycompress
dateext
sharedscripts
postrotate
# just if mysqld is really running
if test -x /usr/bin/mysqladmin && \
/usr/bin/mysqladmin ping &>/dev/null
then
/usr/bin/mysqladmin flush-logs
fi
endscript
}




grep
mysqldumpslowpercona-toolkitpt-query-digest使

Percona Toolkit
$ sudo yum -y install perl-Time-HiRes perl-IO-Socket-SSL perl-DBD-MySQL
$ sudo rpm -ivh http://www.percona.com/redir/downloads/percona-toolkit/LATEST/RPM/percona-toolkit-2.2.10-1.noarch.rpm


$ pt-query-digest mysql-slow.log > digest.txt

digest.txt

# A software update is available:
# * Percona Toolkit 2.2.6 has a possible security issue (CVE-2014-2029) upgrade is recommended. The current version for Percona::Toolkit is 2.2.10.


# 5.3s user time, 80ms system time, 28.53M rss, 216.25M vsz
# Current date: Thu Aug 28 21:42:16 2014
# Hostname: hoge-db03
# Files: /var/lib/mysql/mysql-slow.log
# Overall: 10.48k total, 57 unique, 0.16 QPS, 0.05x concurrency __________
# Time range: 2014-08-28 03:40:02 to 21:42:15
# Attribute total min max avg 95% stddev median
# ============ ======= ======= ======= ======= ======= ======= =======
# Exec time 3002s 100ms 11s 286ms 777ms 284ms 180ms
# Lock time 1s 41us 378us 96us 119us 16us 98us
# Rows sent 13.66M 1 174.31k 1.33k 6.31k 5.51k 0.99
# Rows examine 825.18M 2.00k 1.46M 80.61k 211.82k 110.61k 36.57k
# Query size 5.05M 154 791 504.95 755.64 100.54 487.09

# Profile
# Rank Query ID Response time Calls R/Call V/M Item
# ==== ================== =============== ===== ====== ===== =============
# 1 0x3BEFCC5114487A23 1268.7555 42.3% 4211 0.3013 0.36 SELECT tablename
# 2 0x323595A45502EE39 315.5999 10.5% 1026 0.3076 0.19 SELECT tablename
# 3 0xD0473BC5F5324984 176.0897 5.9% 634 0.2777 0.00 SELECT tablename
# 4 0x9FA860819FCAB0B9 174.2407 5.8% 439 0.3969 0.47 SELECT tablename
# 5 0xB9B067F6AF21AD27 149.3055 5.0% 370 0.4035 0.25 SELECT tablename
# 6 0x957F7C8D78DF45F4 97.7765 3.3% 770 0.1270 0.00 SELECT tablename
# 7 0xDF95841E1D35E78C 85.7142 2.9% 483 0.1775 0.12 SELECT tablename
# 8 0xB8E3A489E461A7D7 82.2773 2.7% 244 0.3372 0.14 SELECT tablename
# 9 0x4E29A68B8903FFB3 72.5987 2.4% 323 0.2248 0.43 SELECT tablename
# 10 0x2839418CA78A9FEB 61.5735 2.1% 103 0.5978 0.00 SELECT tablename
# 11 0x91AAB488213B2825 58.6673 2.0% 126 0.4656 0.23 SELECT tablename

1001001 10
# Query 1: 0.06 QPS, 0.02x concurrency, ID 0x3BEFCC5114487A23 at byte 3388347
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.36
# Time range: 2014-08-28 03:40:02 to 21:41:41
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 40 4211
# Exec time 42 1269s 100ms 11s 301ms 777ms 327ms 180ms
# Lock time 42 426ms 50us 253us 101us 119us 12us 98us
# Rows sent 0 4.11k 1 1 1 1 0 1
# Rows examine 23 197.02M 3.36k 847.55k 47.91k 130.04k 51.64k 30.09k
# Query size 39 2.00M 492 498 497.70 487.09 0 487.09
# String:
# Databases hoge
# Hosts
# Users hoge
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms
# 100ms ################################################################
# 1s ##
# 10s+ #
# Tables
# SHOW TABLE STATUS FROM `database` LIKE 'tablename'\G
# SHOW CREATE TABLE `database`.`tablename`\G
# EXPLAIN /*!50100 PARTITIONS*/
select hoge, fuga from tablename where ( hoge = 1 and fuga = 2 and hogehoge = 3 ) order by upd_datetime DESC\G



Explain"EXPLAIN"

EXPLAIN SELECT hoge, count(fuga)  from xxxxxxxxxxx_8      where id = 4             and upd_date  <  1408647600000      group by hoge;
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+-------------+
| 1 | SIMPLE | xxxxxxxxxxx_8 | index | NULL | PRIMARY | 12 | NULL | 4857810 | Using where |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+-------------+
1 row in set (0.00 sec)



select_type
DEPENDENT SUBQUERYUNCACHEABLE SUBQUERYDEPENDENT SUBQUERYUNCACHEABLE SUBQUERY( ՞ ՞))՞ ՞)

type
indexALLindexALLindex

key
使使NULL使

key_len
使user_id(int),status(int)使key_len4user_id使

MySQL :: MySQL 5.1  :: 10.5 
MySQL5.6DATATIME
MySQL :: MySQL 5.6 Reference Manual :: 11.7 Data Type Storage Requirements

rows
使480

Extra
Using filesortUsing temporaryUsing temporaryUsing filesortUsing IndexCovering Index


mysql> SHOW INDEX FROM tbl_name;

"SHOW INDEX 
mysql_show_indexmysql_show_index

Key_name
PRIMARY

Column_name


Cardinality



mysql> CREATE INDEX index_name ON tbl_name(index_col_name, ...);

CREATE INDEXALTER TABLECREATE INDEX


DBEXPLAIN
CREATE INDEX"(index_col_name, ...)"

mysql> SELECT ... WHERE col1 = x AND col2 = y;

WHERE(col1,col2)col1col2col1WHEREcol3 = z AND col4 = rows

mysql> SELECT ... WHERE col1 = x ORDER BY col2;

ORDER BYUsing filesort(col1,col2)

mysql> SELECT ... WHERE col1 = x ORDER BY col2 LIMIT 10;

LIMITcol1(col2)WHERE10

mysql> SELECT ... WHERE col1 = x AND col2 = y ORDER BY col3;

WHERE(col1,col2,col3)col2(col1,col3)key_lenExtraUsing filesortOK

mysql> SELECT ... WHERE col1 = x AND col2 > y ORDER BY col3;

WHERE(col1,col2,col3)(col1,col2)(col1,col3)(col1,col3)

mysql> SELECT ... WHERE col1 = x AND col2 > y AND col3 > z;

(col1,col2)(col1,col3)rows

mysql> SELECT ... WHERE col1 = x AND col2 = y ORDER BY col3 DESC col4 ASC;

ORDER BY

Covering Index


Covering IndexInnoDB使Covering IndexExtraUsing index

mysql> SELECT pk_col FROM t1 WHERE col1 = x AND col2 = y;

(col1,col2)(pk_col)Covering Index

mysql> SELECT pk_col,col1 FROM t1 WHERE col1 = x ORDER BY col2;

(col1,col2)Covering Index

mysql> SELECT pk_col FROM t1 WHERE col1 = x AND col2 > y ORDER BY col3;

(col1,col2,col3)Covering IndexUsing filesort




30
rows
使FORCE INDEX使
key_len


ALTER
pt-online-schema-change使MySQL5.6Online DDL






Special Thanks


R.E.S.P.E.C.T.

(): MySQLEXPLAIN!!
(): Using filesort
(): 2008
(): InnoDB

MySQL 3/
5,184
Amazon.co.jp

MySQL[+]/
3,564
Amazon.co.jp