Created
June 17, 2012 12:43
-
-
Save georgestephanis/2944457 to your computer and use it in GitHub Desktop.
MySQL Optimization -- which is faster, or does it matter?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Option 1 | |
| SELECT p.ID | |
| FROM wp_posts p | |
| INNER JOIN wp_postmeta pm ON p.ID = pm.post_id | |
| WHERE pm.key = 'key' | |
| --- Option 2 | |
| SELECT p.ID | |
| FROM wp_posts p | |
| INNER JOIN wp_postmeta pm ON ( p.ID = pm.post_id AND pm.key = 'key' ) | |
| --- EXPLAIN results for each: | |
| mysql> EXPLAIN EXTENDED SELECT p.ID | |
| FROM wp_posts p | |
| INNER JOIN wp_postmeta pm ON ( p.ID = pm.post_id AND pm.meta_key = 'key' ); | |
| +----+-------------+-------+--------+------------------+----------+---------+----------------+------+----------+-------------+ | |
| | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | | |
| +----+-------------+-------+--------+------------------+----------+---------+----------------+------+----------+-------------+ | |
| | 1 | SIMPLE | pm | ref | post_id,meta_key | meta_key | 768 | const | 1 | 100.00 | Using where | | |
| | 1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 8 | wpb.pm.post_id | 1 | 100.00 | Using index | | |
| +----+-------------+-------+--------+------------------+----------+---------+----------------+------+----------+-------------+ | |
| 2 rows in set (0.06 sec) | |
| mysql> EXPLAIN EXTENDED SELECT p.ID | |
| FROM wp_posts p | |
| INNER JOIN wp_postmeta pm ON p.ID = pm.post_id | |
| WHERE pm.meta_key = 'key'; | |
| +----+-------------+-------+--------+------------------+----------+---------+----------------+------+----------+-------------+ | |
| | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | | |
| +----+-------------+-------+--------+------------------+----------+---------+----------------+------+----------+-------------+ | |
| | 1 | SIMPLE | pm | ref | post_id,meta_key | meta_key | 768 | const | 1 | 100.00 | Using where | | |
| | 1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 8 | wpb.pm.post_id | 1 | 100.00 | Using index | | |
| +----+-------------+-------+--------+------------------+----------+---------+----------------+------+----------+-------------+ | |
| 2 rows in set (0.03 sec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment