Skip to content

Instantly share code, notes, and snippets.

@lexdene
Last active January 3, 2016 20:19
Show Gist options
  • Save lexdene/8514456 to your computer and use it in GitHub Desktop.
Save lexdene/8514456 to your computer and use it in GitHub Desktop.
add an index in mysql
优化前:
+----+-------------+------------+--------+---------------+---------+---------+------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+------------------------+------+----------------------------------------------+
| 1 | SIMPLE | ttb | ALL | NULL | NULL | NULL | NULL | 10 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | dt | ALL | PRIMARY | NULL | NULL | NULL | 5 | Using where; Using join buffer |
| 1 | SIMPLE | tc | eq_ref | PRIMARY | PRIMARY | 4 | tp_db.ttb.topic_id | 1 | |
| 1 | SIMPLE | last_reply | eq_ref | PRIMARY | PRIMARY | 4 | tp_db.tc.last_reply_id | 1 | |
+----+-------------+------------+--------+---------------+---------+---------+------------------------+------+----------------------------------------------+
加索引:
alter table ttb add index tag_id tag_id;
优化后:
+----+-------------+------------+--------+---------------+---------+---------+------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+------------------------+------+----------------------------------------------+
| 1 | SIMPLE | ttb | ref | tag_id | tag_id | 4 | const | 4 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | dt | eq_ref | PRIMARY | PRIMARY | 4 | tp_db.ttb.topic_id | 1 | |
| 1 | SIMPLE | tc | eq_ref | PRIMARY | PRIMARY | 4 | tp_db.ttb.topic_id | 1 | |
| 1 | SIMPLE | last_reply | eq_ref | PRIMARY | PRIMARY | 4 | tp_db.tc.last_reply_id | 1 | |
+----+-------------+------------+--------+---------------+---------+---------+------------------------+------+----------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment