微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 在postgres上使用索引选择慢sql

我有一个生产数据库,使用londist复制到另一个主机.表格看起来像

# \d+ usermessage
               Table "public.usermessage"
  Column       |       Type        | Modifiers | Description
-------------------+-------------------+-----------+-------------
 id                | bigint            | not null  |
 subject           | character varying |           |
 message           | character varying |           |
 read              | boolean           |           |
 timestamp         | bigint            |           |
 owner             | bigint            |           |
 sender            | bigint            |           |
 recipient         | bigint            |           |
 dao_created       | bigint            |           |
 dao_updated       | bigint            |           |
 type              | integer           |           |
 replymessageid    | character varying |           |
 originalmessageid | character varying |           |
 replied           | boolean           |           |
 mheader           | boolean           |           |
 mbody             | boolean           |           |
Indexes:
"usermessage_pkey" PRIMARY KEY,btree (id)
"usermessage_owner_key" btree (owner)
"usermessage_recipient_key" btree (recipient)
"usermessage_timestamp_key" btree ("timestamp")
"usermessage_type_key" btree (type)
Has OIDs: no

如果在复制的数据库上执行,则select正如预期的那样快,如果在生产主机上执行它会非常慢.为了让事情变得更奇怪,并非所有时间戳都很慢,其中一些时间戳在两台主机上都很快.生产主机后面的文件系统和存储很好,而且没有大量使用.有任何想法吗?

replication# explain analyse SELECT COUNT(id) FROM usermessage WHERE owner = 1234567 AND timestamp > 1362077127010;
                                                                 QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=263.37..263.38 rows=1 width=8) (actual time=0.059..0.060 rows=1 loops=1)
   ->  Bitmap Heap Scan on usermessage  (cost=259.35..263.36 rows=1 width=8) (actual time=0.055..0.055 rows=0 loops=1)
         Recheck Cond: ((owner = 1234567) AND ("timestamp" > 1362077127010::bigint))
         ->  BitmapAnd  (cost=259.35..259.35 rows=1 width=0) (actual time=0.054..0.054 rows=0 loops=1)
               ->  Bitmap Index Scan on usermessage_owner_key  (cost=0.00..19.27 rows=241 width=0) (actual time=0.032..0.032 rows=33 loops=1)
                     Index Cond: (owner = 1234567)
               ->  Bitmap Index Scan on usermessage_timestamp_key  (cost=0.00..239.82 rows=12048 width=0) (actual time=0.013..0.013 rows=0 loops=1)
                     Index Cond: ("timestamp" > 1362077127010::bigint)
 Total runtime: 0.103 ms
(9 rows)

production#  explain analyse SELECT COUNT(id) FROM usermessage WHERE owner = 1234567 AND timestamp > 1362077127010;
                                                                        QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=267.39..267.40 rows=1 width=8) (actual time=47536.590..47536.590 rows=1 loops=1)
   ->  Bitmap Heap Scan on usermessage  (cost=263.37..267.38 rows=1 width=8) (actual time=47532.520..47536.579 rows=3 loops=1)
         Recheck Cond: ((owner = 1234567) AND ("timestamp" > 1362077127010::bigint))
         ->  BitmapAnd  (cost=263.37..263.37 rows=1 width=0) (actual time=47532.334..47532.334 rows=0 loops=1)
               ->  Bitmap Index Scan on usermessage_owner_key  (cost=0.00..21.90 rows=168 width=0) (actual time=0.123..0.123 rows=46 loops=1)
                     Index Cond: (owner = 1234567)
               ->  Bitmap Index Scan on usermessage_timestamp_key  (cost=0.00..241.22 rows=12209 width=0) (actual time=47530.255..47530.255 rows=5255617 loops=1)
                     Index Cond: ("timestamp" > 1362077127010::bigint)
 Total runtime: 47536.668 ms
(9 rows)

解决方法

我不太熟悉postgresql而不是MysqL但是

(实际时间= 0.013..0.013行= 0循环= 1)

(实际时间= 47530.255..47530.255行= 5255617循环= 1)

建议我的生产数据库有更多的数据,因为行数差别很大.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐