我有这个查询
SELECT "items".*
FROM
"items"
INNER JOIN
item_mods ON item_mods.item_id = items.id AND item_mods.mod_id = 15
WHERE (items.league_id = 1) AND (items.item_type_id = 11) AND (num_sockets >= 2)
ORDER BY item_mods.total_value DESC
LIMIT 25
这是解释http://explain.depesz.com/s/dbf
"Limit (cost=55739.84..55739.90 rows=25 width=554) (actual time=18065.470..18065.478 rows=25 loops=1)"
" -> Sort (cost=55739.84..55741.90 rows=824 width=554) (actual time=18065.468..18065.471 rows=25 loops=1)"
" Sort Key: item_mods.total_value"
" Sort Method: top-N heapsort Memory: 37kB"
" -> Nested Loop (cost=5871.95..55716.59 rows=824 width=554) (actual time=285.806..18055.589 rows=610 loops=1)"
" -> Bitmap Heap Scan on items (cost=5871.52..20356.70 rows=4339 width=550) (actual time=201.543..10028.684 rows=9945 loops=1)"
" Recheck Cond: ((item_type_id = 11) AND (num_sockets >= 2))"
" Rows Removed by Index Recheck: 4120"
" Filter: (league_id = 1)"
" Rows Removed by Filter: 1125"
" -> BitmapAnd (cost=5871.52..5871.52 rows=4808 width=0) (actual time=199.322..199.322 rows=0 loops=1)"
" -> Bitmap Index Scan on index_items_on_item_type_id (cost=0.00..289.61 rows=15625 width=0) (actual time=38.699..38.699 rows=16018 loops=1)"
" Index Cond: (item_type_id = 11)"
" -> Bitmap Index Scan on index_items_on_num_sockets (cost=0.00..5579.49 rows=301742 width=0) (actual time=158.441..158.441 rows=301342 loops=1)"
" Index Cond: (num_sockets >= 2)"
" -> Index Scan using index_item_mods_on_item_id on item_mods (cost=0.43..8.14 rows=1 width=8) (actual time=0.803..0.803 rows=0 loops=9945)"
" Index Cond: (item_id = items.id)"
" Filter: (mod_id = 15)"
" Rows Removed by Filter: 9"
"Total runtime: 18065.773 ms"
如何提高此查询的速度?我注意到索引扫描中有一个循环 > 9000 次