
作成者
二階堂航
公開日時
2024-06-19
ハッシュタグ
#tech#MySQL

作成者
公開日時
ハッシュタグ
mysql> SHOW INDEX FROM blogs;
+-------+------------+--------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+--------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| blogs | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
| blogs | 1 | index_blogs_on_user_id | 1 | user_id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
| blogs | 1 | index_blogs_on_publish_at | 1 | publish_at | A | 15 | NULL | NULL | YES | BTREE | | | YES | NULL |
| blogs | 1 | index_blogs_on_status_and_publish_at | 1 | status | A | 2 | NULL | NULL | YES | BTREE | | | YES | NULL |
| blogs | 1 | index_blogs_on_status_and_publish_at | 2 | publish_at | A | 15 | NULL | NULL | YES | BTREE | | | YES | NULL |
| blogs | 1 | index_blogs_on_publish_at_and_status | 1 | publish_at | A | 15 | NULL | NULL | YES | BTREE | | | YES | NULL |
| blogs | 1 | index_blogs_on_publish_at_and_status | 2 | status | A | 15 | NULL | NULL | YES | BTREE | | | YES | NULL |
+-------+------------+--------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
7 rows in set (0.01 sec)SELECT *
FROM blogs
WHERE status = 1
ORDER BY publish_at DESC;mysql> EXPLAIN SELECT * FROM blogs WHERE status = 1 ORDER BY publish_at DESC;
+----+-------------+-------+------------+------+--------------------------------------+--------------------------------------+---------+-------+------+----------+---------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------------------------+--------------------------------------+---------+-------+------+----------+---------------------+
| 1 | SIMPLE | blogs | NULL | ref | index_blogs_on_status_and_publish_at | index_blogs_on_status_and_publish_at | 5 | const | 15 | 100.00 | Backward index scan |
+----+-------------+-------+------------+------+--------------------------------------+--------------------------------------+---------+-------+------+----------+---------------------+
1 row in set, 2 warnings (0.00 sec)
| id | title | status | publish_at | user_id |
| 1 | タイトル1 | 0 | 2023-12-10 | 1 |
| 2 | タイトル2 | 1 | 2023-08-16 | 2 |
| 3 | タイトル3 | 1 | 2023-11-29 | 3 |
| 4 | タイトル4 | 1 | 2024-04-21 | 4 |
| 5 | タイトル5 | 1 | 2024-01-03 | 5 |
| 6 | タイトル6 | 1 | 2023-11-27 | 6 |
| 7 | タイトル7 | 0 | 2024-03-30 | 7 |
| 8 | タイトル8 | 0 | 2023-06-24 | 8 |
| 9 | タイトル9 | 1 | 2023-09-04 | 9 |
| 10 | タイトル10 | 1 | 2023-08-21 | 10 |
| 11 | タイトル11 | 1 | 2023-06-17 | 11 |
| 12 | タイトル12 | 1 | 2024-04-20 | 12 |
| 13 | タイトル13 | 1 | 2024-04-28 | 13 |
| 14 | タイトル14 | 0 | 2023-06-25 | 14 |
| 15 | タイトル15 | 0 | 2024-12-13 | 15 |