【问题标题】:Slow Postgres 9.3 queries缓慢的 Postgres 9.3 查询
【发布时间】:2016-12-09 22:08:31
【问题描述】:

我试图弄清楚是否可以加快对存储电子邮件消息的数据库的两个查询。这是表格:

\d messages;
                             Table "public.messages"
     Column     |  Type   |                       Modifiers
----------------+---------+-------------------------------------------------------
 id             | bigint  | not null default nextval('messages_id_seq'::regclass)
 created        | bigint  |
 updated        | bigint  |
 version        | bigint  |
 threadid       | bigint  |
 userid         | bigint  |
 groupid        | bigint  |
 messageid      | text    |
 date           | bigint  |
 num            | bigint  |
 hasattachments | boolean |
 placeholder    | boolean |
 compressedmsg  | bytea   |
 revcount       | bigint  |
 subject        | text    |
 isreply        | boolean |
 likes          | bytea   |
 isspecial      | boolean |
 pollid         | bigint  |
 username       | text    |
 fullname       | text    |
Indexes:
    "messages_pkey" PRIMARY KEY, btree (id)
    "idx_unique_message_messageid" UNIQUE, btree (groupid, messageid)
    "idx_unique_message_num" UNIQUE, btree (groupid, num)
    "idx_group_id" btree (groupid)
    "idx_message_id" btree (messageid)
    "idx_thread_id" btree (threadid)
    "idx_user_id" btree (userid)

来自SELECT relname, relpages, reltuples::numeric, pg_size_pretty(pg_table_size(oid)) FROM pg_class WHERE oid='messages'::regclass;的输出

 relname  | relpages | reltuples | pg_size_pretty
----------+----------+-----------+----------------
 messages |  1584913 |   7337880 | 32 GB

一些可能相关的 postgres 配置值:

shared_buffers = 1536MB
effective_cache_size = 4608MB
work_mem = 7864kB
maintenance_work_mem = 384MB

以下是解释分析输出:

explain analyze SELECT * FROM messages WHERE groupid=1886 ORDER BY id ASC LIMIT 20 offset 4440;
                                                                      QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=479243.63..481402.39 rows=20 width=747) (actual time=14167.374..14167.408 rows=20 loops=1)
   ->  Index Scan using messages_pkey on messages  (cost=0.43..19589605.98 rows=181490 width=747) (actual time=14105.172..14167.188 rows=4460 loops=1)
         Filter: (groupid = 1886)
         Rows Removed by Filter: 2364949
 Total runtime: 14167.455 ms
(5 rows)

第二个查询:

explain analyze SELECT * FROM messages WHERE groupid=1886 ORDER BY created ASC LIMIT 20 offset 4440;
                                                                        QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=538650.72..538650.77 rows=20 width=747) (actual time=671.983..671.992 rows=20 loops=1)
   ->  Sort  (cost=538639.62..539093.34 rows=181490 width=747) (actual time=670.680..671.829 rows=4460 loops=1)
         Sort Key: created
         Sort Method: top-N heapsort  Memory: 7078kB
         ->  Bitmap Heap Scan on messages  (cost=7299.11..526731.31 rows=181490 width=747) (actual time=84.975..512.969 rows=200561 loops=1)
               Recheck Cond: (groupid = 1886)
               ->  Bitmap Index Scan on idx_unique_message_num  (cost=0.00..7253.73 rows=181490 width=0) (actual time=57.239..57.239 rows=203423 loops=1)
                     Index Cond: (groupid = 1886)
 Total runtime: 672.787 ms
(9 rows)

这是在 SSD、8GB Ram 实例上,平均负载通常在 0.15 左右。

我绝对不是专家。这是数据只是散布在整个磁盘中的情况吗?是我使用 CLUSTER 的唯一解决方案吗?

我不明白的一件事是为什么它使用idx_unique_message_num 作为第二个查询的索引。为什么按 ID 排序这么慢?

【问题讨论】:

  • groupid=1886 有多少条记录?也许在(groupid,id)(groupid,created) 上添加索引会有所帮助
  • groupid=1886 有 200,563 行。
  • 尝试在评论中添加索引,因为现在 postgres 必须(某种程度上)对这 200,000 条记录进行排序,这很慢。没有OFFSET 的查询会更快,但是有了它,索引应该对问题进行排序
  • 做到了。现在运行时间在 5ms-10ms 之间。谢谢!

标签: postgresql postgresql-9.3


【解决方案1】:

如果有很多带有groupid=1886 的记录(来自评论:有 200,563 条),要在已排序的行子集的偏移处获取记录,则需要排序(或等效的堆算法),这很慢。

这可以通过添加索引来解决。在这种情况下,一个在(groupid,id) 上,另一个在(groupid,created) 上。

来自评论:这确实有帮助,将运行时间缩短到 5ms-10ms。

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多