동일한 두개의 쿼리가 있습니다.
다만 차이가 있다면 Limt 100 과 Limit 200 으로 조회되는 row를 다르게 준거 밖에 없습니다.
그런데 두개의 실행 계획이 아래와 같이 다르고 100으로 했을 경우 무지하게 실행이 오래 걸리다가 timeout이 발생합니다..
200으로 하면 1초 미만으로 나옵니다.
도대체 무슨 차이일까요???
그리고 100로 했을때 빠르게 수행되게 하려면 어떻게 해야 할 까요?
쿼리
SELECT _id, _created_at, _modified_at, archetype, attributes, charges, is_shared, is_enabled, owner,
attachments, slots, expires_at
FROM "objects._items"
WHERE archetype = 'a1234567-0000-0000-0000-b7a282e1f064'
AND (owner = '00000000-0000-0000-0000-000000000000' OR is_shared OR True)
ORDER BY _id
LIMIT 100; -- or LIMIT 200;
실행계획
-- 100로 했을때
"Limit (cost=0.56..203664.55 rows=100 width=514)"
" -> Index Scan using "objects._items_pkey" on "objects._items" (cost=0.56..148910956.97 rows=73116 width=514)"
" Filter: (archetype = 'a1234567-0000-0000-0000-b7a282e1f064'::uuid)"
--200 으로 했을때
"Limit (cost=257678.75..257679.25 rows=200 width=514)"
" -> Sort (cost=257678.75..257861.54 rows=73117 width=514)"
" Sort Key: _id"
" -> Index Scan using object_concurrent_index_c197cfe95227b8fed06c3621fe4d035a on "objects._items" (cost=0.56..254518.69 rows=73117 width=514)"
" Index Cond: (archetype = 'a1234567-0000-0000-0000-b7a282e1f064'::uuid)"
|