taiwork / isucon13

0 stars 0 forks source link

feat: add index to livestream_tags.livestream_id #21

Closed taiwork closed 11 months ago

taiwork commented 11 months ago

こちらのissue https://github.com/taiwork/isucon13/issues/13

Count: 5937  Time=0.04s (244s)  Lock=0.00s (0s)  Rows=3.6 (21203), isucon[isucon]@localhost
  SELECT * FROM livestream_tags WHERE livestream_id = 'S'

before

mysql> explain SELECT * FROM livestream_tags WHERE livestream_id = 'S';
+----+-------------+-----------------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table           | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+-----------------+------------+------+---------------+------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | livestream_tags | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 10640 |    10.00 | Using where |
+----+-------------+-----------------+------------+------+---------------+------+---------+------+-------+----------+-------------+
1 row in set, 2 warnings (0.01 sec)

実行

ALTER TABLE `livestream_tags` ADD INDEX `idx_livestream_id` (`livestream_id`);
mysql> show index from livestream_tags;
+-----------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table           | Non_unique | Key_name          | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-----------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| livestream_tags |          0 | PRIMARY           |            1 | id            | A         |       10640 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| livestream_tags |          1 | idx_livestream_id |            1 | livestream_id | A         |        7305 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-----------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

after

mysql> explain SELECT * FROM livestream_tags WHERE livestream_id = 'S';
+----+-------------+-----------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-----------------------+
| id | select_type | table           | partitions | type | possible_keys     | key               | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-----------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | livestream_tags | NULL       | ref  | idx_livestream_id | idx_livestream_id | 8       | const |    1 |   100.00 | Using index condition |
+----+-------------+-----------------+------------+------+-------------------+-------------------+---------+-------+------+----------+-----------------------+
1 row in set, 2 warnings (0.00 sec)