PostgreSQL 11的order by子句的性能问题

时间:2019-03-26 13:48:01

标签: postgresql plpgsql database-performance

我想解决仅在使用order by子句的情况下出现的问题。

详细信息: 我试图将一个临时表插入到一个临时表中,该表由按当前时间插入的修改时间列排序的联接表排成50行。

每个表在以下列上都有一个多列索引:PRIMARY KEY(SystemID, ObjectID, ElementID, ModificationTime)

声明:

sqlString := 'INSERT INTO ResultTable ('
             'SELECT * '
             'FROM "TABLE" a '
             '   LEFT OUTER JOIN "TABLE_Text" l1031 '
             '      ON a.ModificationTime = l1031.ModificationTime '
             '         AND a.SystemID = l1031.SystemID '
             '         AND a.ObjectID = l1031.ObjectID '
             '         AND a.ElementID = l1031.ElementID '
             '         AND l1031.LCID = 1031 '
             '   LEFT OUTER JOIN ( '
             '      SELECT * AS CommentNumber '
             '      FROM "TABLE_Comment" v1 '
             '      GROUP BY v1.ModificationTime, v1.SystemID, v1.ObjectID, v1.ElementID '
             '   ) c '
             '      ON a.ModificationTime = c.ModificationTime '
             '         AND a.SystemID = c.SystemID '
             '         AND a.ObjectID = c.ObjectID '
             '         AND a.ElementID = c.ElementID '
             'WHERE a.ModificationTime BETWEEN $1 AND $2 '
             '  AND ( a.Enabled = 1 ) '
             'ORDER BY a.ModificationTime DESC '
             'LIMIT 50)';

EXECUTE sqlString USING StartTime,EndTime;

enter image description here

执行计划:https://explain.depesz.com/s/S96g(已混淆)

如果删除order by子句,则会得到以下结果:

enter image description here

哪个指出我的排序问题。有什么方法可以通过order by子句改善性能吗?

为了使问题更加透明,我进行了一项长期测试,您可以看到,使用order by子句的性能呈线性下降:

enter image description here

PostgreSQL版本:“ PostgreSQL 11.1,由Visual C ++ build 1914编译,为64位”

由...安装:使用EnterpriseDB从EDB官方站点进行的一键式安装程序。

Postgresql.conf更改:使用了pgtune建议:

# DB Version: 11 
# OS Type: windows 
# DB Type: desktop 
# Total Memory (RAM): 8 GB 
# CPUs num: 4 
# Connections num: 25 
# Data Storage: hdd 
max_connections = 25 
shared_buffers = 512MB 
effective_cache_size = 2GB 
maintenance_work_mem = 512MB 
checkpoint_completion_target = 0.5 
wal_buffers = 16MB 
default_statistics_target = 100 
random_page_cost = 4 
work_mem = 8738kB 
min_wal_size = 100MB 
max_wal_size = 1GB 
max_worker_processes = 4 
max_parallel_workers_per_gather = 2 
max_parallel_workers = 4

操作系统:Windows 10 x64,版本:1607

0 个答案:

没有答案