Skip to content

Commit 5ae62e5

Browse files
committed
ランキングのテーブルにインデックスを追加した
Seq Scanを避けられたらうれしい。 Before after ```SQL -- ユーザーのRated Point Sumを取得する EXPLAIN ANALYSE SELECT point_sum FROM rated_point_sum WHERE LOWER(user_id) = 'hotate29'; ``` 張る前 ```text Gather (cost=1000.00..6442.09 rows=1278 width=17) (actual time=42.603..43.865 rows=1 loops=1) Workers Planned: 1 Workers Launched: 1 -> Parallel Seq Scan on rated_point_sum (cost=0.00..5314.29 rows=752 width=17) (actual time=31.409..41.356 rows=0 loops=2) Filter: (lower((user_id)::text) = 'hotate29'::text) Rows Removed by Filter: 127800 Planning Time: 0.102 ms Execution Time: 43.943 ms ``` 張った後 ```text Bitmap Heap Scan on rated_point_sum (cost=30.32..2414.86 rows=1278 width=17) (actual time=0.024..0.024 rows=1 loops=1) Recheck Cond: (lower((user_id)::text) = 'hotate29'::text) Heap Blocks: exact=1 -> Bitmap Index Scan on rated_point_sum_lower_idx (cost=0.00..30.01 rows=1278 width=0) (actual time=0.021..0.021 rows=1 loops=1) Index Cond: (lower((user_id)::text) = 'hotate29'::text) Planning Time: 0.168 ms Execution Time: 0.039 ms ``` --- ```SQL -- C++でのAC数上位3人を取得 EXPLAIN ANALYSE SELECT user_id, problem_count FROM language_count WHERE simplified_language = 'C++' ORDER BY problem_count DESC, user_id ASC OFFSET 0 LIMIT 3; ``` 張る前 ```text Limit (cost=6877.32..6877.33 rows=3 width=12) (actual time=5.972..5.973 rows=3 loops=1) -> Sort (cost=6877.32..6879.82 rows=1000 width=12) (actual time=5.971..5.971 rows=3 loops=1) Sort Key: problem_count DESC, user_id Sort Method: top-N heapsort Memory: 25kB -> Seq Scan on language_count (cost=0.00..6864.40 rows=1000 width=12) (actual time=0.013..5.854 rows=1000 loops=1) Filter: ((simplified_language)::text = 'C++'::text) Rows Removed by Filter: 25432 Planning Time: 0.197 ms Execution Time: 5.988 ms ``` 張った後 ```text Limit (cost=0.29..0.41 rows=3 width=12) (actual time=0.031..0.032 rows=3 loops=1) -> Index Only Scan using language_count_simplified_language_problem_count_user_id_idx on language_count (cost=0.29..41.79 rows=1000 width=12) (actual time=0.030..0.031 rows=3 loops=1) Index Cond: (simplified_language = 'C++'::text) Heap Fetches: 0 Planning Time: 0.280 ms Execution Time: 0.045 ms ``` データはそれぞれ <https://kenkoooo.com/atcoder/resources/sums.json> と<https://kenkoooo.com/atcoder/resources/lang.json> を使用。
1 parent 26a0188 commit 5ae62e5

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

config/database-definition.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ CREATE TABLE accepted_count (
7979
problem_count INT NOT NULL,
8080
PRIMARY KEY (user_id)
8181
);
82+
CREATE INDEX ON accepted_count (LOWER(user_id));
83+
CREATE INDEX ON accepted_count (problem_count DESC, user_id);
8284

8385
DROP TABLE IF EXISTS points;
8486
CREATE TABLE points (
@@ -94,6 +96,8 @@ CREATE TABLE rated_point_sum (
9496
point_sum BIGINT NOT NULL,
9597
PRIMARY KEY (user_id)
9698
);
99+
CREATE INDEX ON rated_point_sum (LOWER(user_id));
100+
CREATE INDEX ON rated_point_sum (point_sum DESC, user_id);
97101

98102
DROP TABLE IF EXISTS language_count;
99103
CREATE TABLE language_count (
@@ -102,6 +106,8 @@ CREATE TABLE language_count (
102106
problem_count INT NOT NULL,
103107
PRIMARY KEY (user_id, simplified_language)
104108
);
109+
CREATE INDEX ON language_count (LOWER(user_id));
110+
CREATE INDEX ON language_count (simplified_language, problem_count DESC, user_id);
105111

106112
DROP TABLE IF EXISTS predicted_rating;
107113
CREATE TABLE predicted_rating (
@@ -124,6 +130,8 @@ CREATE TABLE max_streaks (
124130
streak BIGINT NOT NULL,
125131
PRIMARY KEY (user_id)
126132
);
133+
CREATE INDEX ON max_streaks (LOWER(user_id));
134+
CREATE INDEX ON max_streaks (streak DESC, user_id);
127135

128136
-- For internal services:
129137
DROP TABLE IF EXISTS internal_problem_list_items;

0 commit comments

Comments
 (0)