チューニングのためにはexplainとshow profileして、まずは情報取得。
下の結果を見る限り、SELECTで抽出するレコード数を減らす & (テーブルのレコード数を減らす or パーティショニング)が効きそうかな。
explain
explain.sql
explain select xxx from yyy ...;
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
---|---|---|---|---|---|---|---|---|---|
1 | PRIMARY | ALL | NULL | NULL | NULL | NULL | 500 | NULL | |
2 | DERIVED | yyy | range | IDX_1,IDX_2 | IDX_2 | 3 | NULL | 52104 | Using where |
2 | DERIVED | zzz | ref | IDX_Z_1 | IDX_Z_1 | 4 | zzz.id | 9 | Using where; Using index |
show profile
profile.sql
-- プロファイリングを有効にする
set profiling = 1;
-- プロファイル情報を取得する対象となるSQLを実行する
select xxx from yyy ...;
-- プロファイル情報を表示する
show profile;
Status | Duration |
---|---|
starting | 0.000120 |
checking permissions | 0.000017 |
checking permissions | 0.000013 |
checking permissions | 0.000014 |
Opening tables | 0.000143 |
init | 0.000036 |
System lock | 0.000036 |
optimizing | 0.000014 |
optimizing | 0.000033 |
statistics | 0.000111 |
preparing | 0.000070 |
Sorting result | 0.000019 |
statistics | 0.000019 |
preparing | 0.000017 |
executing | 0.000027 |
Sending data | 0.000032 |
executing | 0.000013 |
Sending data | 11.752481 |
end | 0.000031 |
query end | 0.000018 |
closing tables | 0.000014 |
removing tmp table | 0.000025 |
closing tables | 0.000049 |
freeing items | 0.000376 |
logging slow query | 0.000021 |
Opening tables | 0.000024 |
System lock | 0.000087 |
cleaning up | 0.000030 |