通过增加内存或查询重写来提高查询速度

时间:2019-05-11 06:44:35

标签: postgresql

问题是如何将查询速度从0.4毫秒提高到约0.1毫秒? 我应该添加到PostgreSQL服务器什么额外的内存,或者如何重写查询? 以下有关查询和服务器的所有信息。

PostgreSQL 11

2Gb RAM
2xCPU
40Gb SSD

postgresql.conf

max_connections = 200
shared_buffers = 512MB
effective_cache_size = 1536MB
maintenance_work_mem = 128MB
checkpoint_completion_target = 0.7
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 1.1
effective_io_concurrency = 200
work_mem = 2621kB
min_wal_size = 1GB
max_wal_size = 2GB
max_worker_processes = 2
max_parallel_workers_per_gather = 1
max_parallel_workers = 2

查询一张具有30万行和14列的表

select
SUM(revenue) as m0,
SUM(revenue) / Count(distinct transactions) as m1,
extract(ISODOW from datetime_transactions::date) as d0
from test
where shop='New York'
group by GROUPING SETS((d0))
order by d0 ASC, m0 ASC, m1 ASC

解释分析

Sort  (cost=29831.74..29972.90 rows=56464 width=72) (actual time=371.042..371.043 rows=5 loops=1)
Sort Key: (date_part('isodow'::text, ((datetime_transactions)::date)::timestamp without time zone)), (sum(revenue)), ((sum(revenue) / (count(DISTINCT transactions))::numeric))
  Sort Method: quicksort  Memory: 25kB
  ->  GroupAggregate  (cost=21703.94..24018.21 rows=56464 width=72) (actual time=156.002..371.019 rows=5 loops=1)
        Group Key: (date_part('isodow'::text, ((datetime_transactions)::date)::timestamp without time zone))
        ->  Sort  (cost=21703.94..21856.24 rows=60921 width=60) (actual time=102.089..117.352 rows=60609 loops=1)
              Sort Key: (date_part('isodow'::text, ((datetime_transactions)::date)::timestamp without time zone))
              Sort Method: external merge  Disk: 4216kB
              ->  Seq Scan on test (cost=0.00..15519.59 rows=60921 width=60) (actual time=0.015..75.789 rows=60609 loops=1)
                    Filter: (shop = 'New York'::text)
                    Rows Removed by Filter: 239446
Planning Time: 0.123 ms
Execution Time: 371.846 ms

0 个答案:

没有答案