PostgreSQL中联接表中的列对查询的排序缓慢

时间:2019-01-23 10:55:12

标签: postgresql

如何优化此查询?

我尝试增加work_mem值并创建名称索引。但这不起作用。

EXPLAIN ANALYZE 
SELECT b.* FROM book b
JOIN category c ON c.id = b.categoryid
ORDER BY c.name, b.name 
LIMIT 20 OFFSET 1
"Limit  (cost=328.82..328.87 rows=20 width=207) (actual time=11.942..11.955 rows=20 loops=1)"
"  ->  Sort  (cost=328.81..341.64 rows=5132 width=207) (actual time=11.940..11.944 rows=21 loops=1)"
"        Sort Key: c.name, b.name"
"        Sort Method: top-N heapsort  Memory: 34kB"
"        ->  Hash Join  (cost=10.37..190.45 rows=5132 width=207) (actual time=0.143..4.963 rows=5132 loops=1)"
"              Hash Cond: (b.categoryid = c.id)"
"              ->  Seq Scan on book b  (cost=0.00..166.32 rows=5132 width=196) (actual time=0.007..2.070 rows=5132 loops=1)"
"              ->  Hash  (cost=7.94..7.94 rows=194 width=27) (actual time=0.129..0.129 rows=194 loops=1)"
"                    Buckets: 1024  Batches: 1  Memory Usage: 20kB"
"                    ->  Seq Scan on category c  (cost=0.00..7.94 rows=194 width=27) (actual time=0.002..0.061 rows=194 loops=1)"
"Planning time: 0.283 ms"
"Execution time: 11.999 ms"

0 个答案:

没有答案