Commit 5ae62e5
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
1 file changed
+8
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
| |||
94 | 96 | | |
95 | 97 | | |
96 | 98 | | |
| 99 | + | |
| 100 | + | |
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
| |||
102 | 106 | | |
103 | 107 | | |
104 | 108 | | |
| 109 | + | |
| 110 | + | |
105 | 111 | | |
106 | 112 | | |
107 | 113 | | |
| |||
124 | 130 | | |
125 | 131 | | |
126 | 132 | | |
| 133 | + | |
| 134 | + | |
127 | 135 | | |
128 | 136 | | |
129 | 137 | | |
| |||
0 commit comments