Slide 66
Slide 66 text
② ORDER BY + LIMITの罠
66
INSERT INTO orders
SELECT generate_series(5000001, 5100000), -- 新しく100kのオーダー
floor(random() * 5 + 1)::int AS product_id,
floor(random() * 10 + 1)::int AS user_id, -- ユーザーid 1-10のみでオーダー
date '2024-01-01' + (random() * 365)::int AS created_at;
ちょっとuser_idのselectivityを悪くしてみる
SELECT n_distinct, most_common_vals,
most_common_freqs
FROM pg_stats WHERE attname = 'user_id';
-[ RECORD 1 ]-----+---------
n_distinct | 97702
most_common_vals | {53246}
most_common_freqs | {0.0002}
-[ RECORD 1 ]-----+---------------------------
n_distinct | 92373
most_common_vals | {6,1,3,2,7,8,4,9,10,5}
most_common_freqs |
{0.0022333334,0.0021,0.0020333333,0.002,0.002,0
.002,0.0018666667,0.0018,0.0017666667,0.0017}