d-shimizu / book-web-performance-tuning

達人が教えるWebパフォーマンスチューニング 〜ISUCONから学ぶ高速化の実践
0 stars 0 forks source link

comments テーブルの user_id にインデックスを作成する #8

Open d-shimizu opened 2 years ago

d-shimizu commented 2 years ago
mysql> explain select count(*) from comments where user_id = 123;
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | comments | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 99904 |    10.00 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
d-shimizu commented 2 years ago
mysql> alter table comments add index idx_user_id (user_id);
Query OK, 0 rows affected (0.50 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql> explain select count(*) from comments where user_id = 123;
+----+-------------+----------+------------+------+---------------+-------------+---------+-------+------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key         | key_len | ref   | rows | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+-------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | comments | NULL       | ref  | idx_user_id   | idx_user_id | 4       | const |  101 |   100.00 | Using index |
+----+-------------+----------+------------+------+---------------+-------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
isucon@ip-10-128-1-252:~/private_isu.git/benchmarker$ ./bin/benchmarker -u ./userdata -t http://10.128.1.80
{"pass":true,"score":15708,"success":14409,"fail":0,"messages":[]}