Skip to content

Instantly share code, notes, and snippets.

@vidluther
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vidluther/9370018 to your computer and use it in GitHub Desktop.
Save vidluther/9370018 to your computer and use it in GitHub Desktop.
with and without hints
SELECT count(DISTINCT a.id) FROM wp_bp_activity a USE INDEX (type) WHERE a.is_spam = 0 AND a.id IN (957935) AND a.type != 'activity_comment' ORDER BY a.date_recorded DESC
-> ;
+----------------------+
| count(DISTINCT a.id) |
+----------------------+
| 1 |
+----------------------+
1 row in set (2.31 sec)
mysql> SELECT count(DISTINCT a.id) FROM wp_bp_activity a WHERE a.is_spam = 0 AND a.id IN (957935) AND a.type != 'activity_comment' ORDER BY a.date_recorded DESC;
+----------------------+
| count(DISTINCT a.id) |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)
# To prove it's not a query cache..
SELECT count(DISTINCT a.id) FROM wp_bp_activity a WHERE a.is_spam = 0 AND a.id IN (957925) AND a.type != 'activity_comment' ORDER BY a.date_recorded DESC;
+----------------------+
| count(DISTINCT a.id) |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT count(DISTINCT a.id) FROM wp_bp_activity a USE INDEX (type) WHERE a.is_spam = 0 AND a.id IN (957925) AND a.type != 'activity_comment' ORDER BY a.date_recorded DESC;
+----------------------+
| count(DISTINCT a.id) |
+----------------------+
| 0 |
+----------------------+
1 row in set (3.86 sec)
### Explain with the HINT
EXPLAIN SELECT count(DISTINCT a.id) FROM wp_bp_activity a USE INDEX (type) WHERE a.is_spam = 0 AND a.id IN (957935) AND a.type != 'activity_comment' ORDER BY a.date_recorded DESC
-> ;
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | a | range | type | type | 227 | NULL | 620546 | Using where |
+----+-------------+-------+-------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)
### Explain without the Hint
EXPLAIN SELECT count(DISTINCT a.id) FROM wp_bp_activity a WHERE a.is_spam = 0 AND a.id IN (957935) AND a.type != 'activity_comment' ORDER BY a.date_recorded DESC;
+----+-------------+-------+-------+----------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+----------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | a | const | PRIMARY,type,is_spam | PRIMARY | 8 | const | 1 | |
+----+-------------+-------+-------+----------------------+---------+---------+-------+------+-------+
1 row in set (0.01 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment