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

SQL:
  1. SELECT e.id,
  2. e.STATUS,
  3. e.category_id,
  4. e.user_type,
  5. e.name,
  6. e.favorites,
  7. e.comments,
  8. e.trackbacks,
  9. e.favorite_permission,
  10. e.comment_permission,
  11. e.comment_captcha_permission,
  12. e.trackback_permission,
  13. e.edited,
  14. e.image,
  15. e.caption,
  16. e.user_name,
  17. e.created,
  18. e.modified,
  19. e.removed,
  20. e.title,
  21. e.body
  22. FROM entry AS e LEFT JOIN entry_comment AS c ON e.id = c.entry_id
  23. WHERE e.name = %s
  24. AND ( e.STATUS = 1 OR e.STATUS = 2)
  25. AND e.removed IS NULL
  26. AND c.removed IS NULL ORDER BY e.edited DESC

※%sはランダムに変化します。

InnoDBの実行計画(explainの結果)

SQL:
  1. *************************** 1. row ***************************
  2. id: 1
  3. select_type: SIMPLE
  4. TABLE: e
  5. type: ref
  6. possible_keys: entry_unique,entry_idx2
  7. KEY: entry_unique
  8. key_len: 194
  9. ref: const
  10. rows: 1
  11. Extra: USING WHERE; USING filesort
  12. *************************** 2. row ***************************
  13. id: 1
  14. select_type: SIMPLE
  15. TABLE: c
  16. type: ref
  17. possible_keys: entry_comment_idx1
  18. KEY: entry_comment_idx1
  19. key_len: 5
  20. ref: testsuite.e.id
  21. rows: 1
  22. Extra: USING WHERE; USING INDEX

MyISAMの実行計画(explainの結果)

SQL:
  1. *************************** 1. row ***************************
  2. id: 1
  3. select_type: SIMPLE
  4. TABLE: e
  5. type: ref
  6. possible_keys: entry_unique,entry_idx2
  7. KEY: entry_unique
  8. key_len: 194
  9. ref: const
  10. rows: 1
  11. Extra: USING WHERE; USING filesort
  12. *************************** 2. row ***************************
  13. id: 1
  14. select_type: SIMPLE
  15. TABLE: c
  16. type: ref
  17. possible_keys: entry_comment_idx1
  18. KEY: entry_comment_idx1
  19. key_len: 5
  20. ref: testsuite.e.id
  21. rows: 1
  22. Extra: USING WHERE; USING INDEX

実行計画は同じです。

計測結果

スレッド数 MyISAM InnoDB

LEFT JOIN


8同時スレッドのピーク時性能で50%程度、InnoDBのほうが高速です。
ただし64スレッド以上だと、MySQLのほうが高速になります。
それと、InnerJoinに比べると、処理性能が1/3程度になってしまっています。