性能配置似乎是PostgreSQL无效的

时间:2018-02-13 19:46:27

标签: sql postgresql database-performance

我在数据库方面不是很有经验,我想通过使用配置来提高PostgreSQL查询的性能。大多数查询需要大约3.5秒才能在日志中完全搜索。然后我检查了配置文件并将设置设置为相对较低的值,因此我计算了最佳配置并在psql中输入:

postgres=# ALTER SYSTEM SET
postgres-#  max_connections = '200';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  shared_buffers = '4GB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  effective_cache_size = '12GB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  work_mem = '20971kB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  maintenance_work_mem = '1GB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  min_wal_size = '1GB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  max_wal_size = '2GB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  checkpoint_completion_target = '0.7';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  wal_buffers = '16MB';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  default_statistics_target = '100';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET
postgres-#  random_page_cost = '4';
ALTER SYSTEM

这一开始没有用,我认为重启数据库是必要的,所以我使用了:

SELECT pg_reload_conf();

但仍然没有进展。在配置work_mem4 MBmaintenance_work_mem64 MB之前,即使在这些数字增加后,数据库查询所用的时间也非常相似。

表格

表是由Django(我的Web框架)自动创建的,因此像这样:

CREATE TABLE tablename (
    "id" serial NOT NULL PRIMARY KEY,
    "some_char" varchar(30) NOT NULL,
);

创建索引

CREATE INDEX trgm_idx ON table USING gist (column gist_trgm_ops);

执行计划(解释和分析)

 Bitmap Heap Scan on main_question  (cost=1721.05..20990.16 rows=1786 width=436) (actual time=2634.604..2634.634 rows=9 loops=1)
   Recheck Cond: (attr1 % 'querytext'::text)
   Filter: (char_length(attr2) > 300)
   Rows Removed by Filter: 5
   Heap Blocks: exact=13
   Buffers: shared hit=419319
   ->  Bitmap Index Scan on trgm_idx  (cost=0.00..1720.60 rows=5358 width=0) (actual time=2634.578..2634.578 rows=14 loops=1)
         Index Cond: (attr1 % 'querytext'::text)
         Buffers: shared hit=419306
 Planning time: 0.131 ms
 Execution time: 2634.739 ms
(11 rows)

查询日志

配置前

(0.000) SELECT typarray FROM pg_type WHERE typname = 'citext'; args=None
(3.568) SELECT "main_model"."id", "main_model"."attribute1", "main_model"."attribute2" FROM "main_model" WHERE ((CHAR_LENGTH(attribute2) > 350) AND "main_model"."attribute1" % 'querytext'); args=(u'querytext',)
配置后

(0.000) SELECT typarray FROM pg_type WHERE typname = 'citext'; args=None
(3.555) SELECT "main_model"."id", "main_model"."attribute1", "main_model"."attribute2" FROM "main_model" WHERE ((CHAR_LENGTH(attribute2) > 350) AND "main_model"."attribute1" % 'querytext'); args=(u'querytext',)

如您所见,由于某种原因,查询时间非常相似。

这些设置不应该增加最大内存使用量,从而提高性能吗?我做错了吗?

1 个答案:

答案 0 :(得分:0)

将最大连接数设置为200表明您是Postgres性能调整的新手。查看wiki on performance或部分introductions on YouTube。我可以推荐Christophe Pettus: PostgreSQL When It's Not Your Job