相关疑难解决方法(0)

优化对一系列时间戳的查询(两列)

我在 Ubuntu 12.04 上使用 PostgreSQL 9.1。

我需要在一个时间范围内选择记录:我的表time_limits有两个timestamp字段和一个integer属性。我的实际表中还有其他列与此查询无关。

create table (
   start_date_time timestamp,
   end_date_time timestamp, 
   id_phi integer, 
   primary key(start_date_time, end_date_time,id_phi);
Run Code Online (Sandbox Code Playgroud)

该表包含大约 200 万条记录。

像下面这样的查询花费了大量的时间:

select * from time_limits as t 
where t.id_phi=0 
and t.start_date_time <= timestamp'2010-08-08 00:00:00'
and t.end_date_time   >= timestamp'2010-08-08 00:05:00';
Run Code Online (Sandbox Code Playgroud)

所以我尝试添加另一个索引 - PK的倒数:

create index idx_inversed on time_limits(id_phi, start_date_time, end_date_time);
Run Code Online (Sandbox Code Playgroud)

我的印象是性能有所提高:访问表中间记录的时间似乎更合理:介于 40 到 90 秒之间。

但是对于时间范围中间的值,它仍然是几十秒。在针对表格末尾时(按时间顺序),还有两次。

explain analyze第一次尝试得到这个查询计划:

 Bitmap Heap Scan on time_limits  (cost=4730.38..22465.32 rows=62682 width=36) (actual time=44.446..44.446 rows=0 loops=1)
   Recheck …
Run Code Online (Sandbox Code Playgroud)

postgresql index optimization explain postgresql-9.1

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

复合索引是否也适用于第一个字段的查询?

假设我有一个包含字段A和的表B。我在A+上进行常规查询B,所以我在 上创建了一个复合索引(A,B)A复合索引是否也会对查询进行全面优化?

此外,我在 上创建了一个索引A,但 Postgres 仍然只使用复合索引来查询A。如果前面的答案是肯定的,我想这并不重要,但是为什么它默认选择复合索引,如果单个A索引可用?

postgresql performance index database-design index-tuning

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

日期索引优化

我在 PostgreSQL 9.0.8 中有一个很大的对象表(15M+ 行),我想查询过时的字段。

出于可扩展性和并发性的目的,我想将查询除以数百万,并且我想获取具有几天前日期的 updated_at 字段的所有数据。

我已经在 100 万个 ID 上尝试了许多索引和查询,但使用 Heroku 的 Ronin 硬件似乎无法在 100 秒内获得性能。

我正在寻找我尚未尝试使其尽可能高效的建议。

尝试 #1

 EXPLAIN ANALYZE SELECT count(*) FROM objects
 WHERE (date(updated_at)) < (date(now())-7) AND id >= 5000001 AND id < 6000001;
 INDEX USED: (date(updated_at),id)
 268578.934 ms
Run Code Online (Sandbox Code Playgroud)

尝试 #2

 EXPLAIN ANALYZE SELECT count(*) FROM objects
 WHERE ((date(now()) - (date(updated_at)) > 7)) AND id >= 5000001 AND id < 6000001;
 INDEX USED: primary key
 335555.144 ms
Run Code Online (Sandbox Code Playgroud)

尝试 #3

 EXPLAIN ANALYZE SELECT count(*) FROM …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index partitioning postgresql-performance

32
推荐指数
1
解决办法
5万
查看次数

没有表锁的 CLUSTER 的替代方案

由于频繁的新记录和更新记录导致索引和存储碎片,我面临性能下降和存储使用量增加的问题。

VACUUM 没有多大帮助。

不幸的是,CLUSTER 不是一个选项,因为它会导致停机并且 pg_repack 不适用于 AWS RDS。

我正在寻找 CLUSTER 的 hacky 替代品。在我的本地测试中似乎可以正常工作的一个是:

begin;

create temp table tmp_target as select * from target;

delete from target;
insert into target select * from tmp_target order by field1 asc, field2 desc;

drop table tmp_target;

commit;
Run Code Online (Sandbox Code Playgroud)

ctid看起来的顺序是正确的:

select ctid, field1, field2 from target order by ctid;
Run Code Online (Sandbox Code Playgroud)

问题是:这看起来好吗?是否会锁定target表以SELECT查找导致应用程序停机的查询?有没有办法列出事务中涉及的锁?

集群命令锁定只读查询相关的副本

postgresql clustering locking

3
推荐指数
1
解决办法
1063
查看次数