小编col*_*rco的帖子

Postgresql计数极慢(带索引,简单查询)

我需要在包含数百万行的表上运行这些简单的查询:

SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123;
Run Code Online (Sandbox Code Playgroud)
SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123 AND "subscriptions"."trashed_at" IS NULL;
Run Code Online (Sandbox Code Playgroud)

对于项目 123,两个查询的计数结果约为 5M。

我在 上有一个索引project_id,在 上还有另一个索引(project_id, trashed_at)

"index_subscriptions_on_project_id_and_created_at" btree (project_id, created_at DESC)
"index_subscriptions_on_project_id_and_trashed_at" btree (project_id, trashed_at DESC)
Run Code Online (Sandbox Code Playgroud)

问题是这两个查询都非常慢,每个查询大约需要 17 秒。

这些是以下结果EXPLAIN ANALIZE

      QUERY PLAN                                                                                      
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=2068127.29..2068127.30 rows=1 width=0) (actual time=17342.420..17342.420 rows=1 loops=1)
   ->  Bitmap Heap Scan on subscriptions  (cost=199573.94..2055635.23 rows=4996823 width=0) (actual time=1666.409..16855.610 rows=4994254 loops=1)
         Recheck Cond: (project_id = 123)
         Rows Removed …
Run Code Online (Sandbox Code Playgroud)

postgresql performance count postgresql-9.5 query-performance

8
推荐指数
2
解决办法
2万
查看次数

使用 ALTER SYSTEM 从命令行设置 PostgreSQL 配置

我需要编写一个 shell/bash 脚本来自动更改一些 PostgreSQL 配置。

这是我写的命令:

sudo -u postgres psql -U postgres -d postgres -c "
ALTER SYSTEM SET listen_addresses = '127.0.0.1';
ALTER SYSTEM SET max_connections = '200';
ALTER SYSTEM SET shared_buffers = '24GB';
ALTER SYSTEM SET work_mem = '128MB';
...
"
Run Code Online (Sandbox Code Playgroud)

但是我收到这个错误:

ERROR:  ALTER SYSTEM cannot run inside a transaction block
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个错误?

postgresql psql postgresql-14

4
推荐指数
1
解决办法
1296
查看次数