INNER JOINに続いて、今度はLEFT JOINのパフォーマンス比較です。
測定環境についてはこちらを参照して下さい。
・データ件数は100万件
・こちらの構造のテーブルをInnoDBとMyISAMで作成して処理速度を比較
・entryとentry2は記事を格納するテーブルで100万件登録されています。
・entry_commentとentry_comment2はコメントを格納するテーブルで、こちらも100万件登録されています。
・PRIMARY KEYであるidには1~1000000の値がセットされています。
・1記事に対して、1コメントが存在しています。・以下の構造のテーブルをInnoDBとMyISAMで作成して処理速度を比較
以下SQL中の「テーブル名 entryとentry_comment」にはMyISAMなら entry2とentry_comment2 がセットされます。
LeftJoinのパフォーマンス比較用のSQL
-
SELECT e.id,
-
e.STATUS,
-
e.category_id,
-
e.user_type,
-
e.name,
-
e.favorites,
-
e.comments,
-
e.trackbacks,
-
e.favorite_permission,
-
e.comment_permission,
-
e.comment_captcha_permission,
-
e.trackback_permission,
-
e.edited,
-
e.image,
-
e.caption,
-
e.user_name,
-
e.created,
-
e.modified,
-
e.removed,
-
e.title,
-
e.body
-
FROM entry AS e LEFT JOIN entry_comment AS c ON e.id = c.entry_id
-
WHERE e.name = %s
-
AND ( e.STATUS = 1 OR e.STATUS = 2)
-
AND e.removed IS NULL
-
AND c.removed IS NULL ORDER BY e.edited DESC
※%sはランダムに変化します。
InnoDBの実行計画(explainの結果)
-
*************************** 1. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: e
-
type: ref
-
possible_keys: entry_unique,entry_idx2
-
KEY: entry_unique
-
key_len: 194
-
ref: const
-
rows: 1
-
Extra: USING WHERE; USING filesort
-
*************************** 2. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: c
-
type: ref
-
possible_keys: entry_comment_idx1
-
KEY: entry_comment_idx1
-
key_len: 5
-
ref: testsuite.e.id
-
rows: 1
-
Extra: USING WHERE; USING INDEX
MyISAMの実行計画(explainの結果)
-
*************************** 1. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: e
-
type: ref
-
possible_keys: entry_unique,entry_idx2
-
KEY: entry_unique
-
key_len: 194
-
ref: const
-
rows: 1
-
Extra: USING WHERE; USING filesort
-
*************************** 2. row ***************************
-
id: 1
-
select_type: SIMPLE
-
TABLE: c
-
type: ref
-
possible_keys: entry_comment_idx1
-
KEY: entry_comment_idx1
-
key_len: 5
-
ref: testsuite.e.id
-
rows: 1
-
Extra: USING WHERE; USING INDEX
実行計画は同じです。
計測結果
| スレッド数 | MyISAM | InnoDB |
8同時スレッドのピーク時性能で50%程度、InnoDBのほうが高速です。
ただし64スレッド以上だと、MySQLのほうが高速になります。
それと、InnerJoinに比べると、処理性能が1/3程度になってしまっています。




