在postgresql中查询速度慢

时间:2015-03-11 17:55:58

标签: performance postgresql postgresql-9.1

我对一个表进行简单查询,问题是性能下降。 这是表定义:

-- Table: variable_logs

CREATE TABLE variable_logs
(
  id serial NOT NULL,
  variable_id integer,
  created_at timestamp without time zone,
  updated_at timestamp without time zone,
  "timestamp" timestamp without time zone,
  value character varying(255)[] DEFAULT '{}'::character varying[],
  CONSTRAINT variable_logs_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE variable_logs
  OWNER TO postgres;
ALTER TABLE variable_logs ALTER COLUMN created_at SET STATISTICS 1000;


-- Index: idx_time_limits_inversed

-- DROP INDEX idx_time_limits_inversed;

CREATE INDEX idx_time_limits_inversed
  ON variable_logs
  USING btree
  (variable_id, created_at DESC);

基本上是一个每隔X次保存数据的表(例如:10秒),并且它正在快速增长。 "值"列中每行只有1个元素。

Postgresql版本是:"在x86_64-unknown-linux-gnu上的PostgreSQL 9.1.14,由gcc编译(Ubuntu / Linaro 4.8.1-10ubuntu8)4.8.1,64位"

现在该表有大约653941行,并且对表的查询(没有任何连接)很慢。

SELECT   variable_logs.created_at, variable_logs.variable_id, variable_logs.value
FROM variable_logs 
WHERE variable_logs.variable_id = 2
AND (variable_logs.created_at BETWEEN '2015-01-01 00:00:00.000000'
                                  AND '2015-03-09 23:59:00.000000')
ORDER BY created_at asc

Total query runtime: 13979 ms.
184369 rows retrieved.

如果我执行相同的查询而没有订单并过滤日期:

SELECT variable_logs.created_at, variable_logs.variable_id, variable_logs.value 
FROM variable_logs 
WHERE variable_logs.variable_id = 2

Total query runtime: 14035 ms.
184369 rows retrieved.

查询总是变慢,我也尝试使用相同的慢速结果进行VACUUM和ANALYZE。

EXPLAIN (ANALYZE on, VERBOSE off, COSTS on, BUFFERS on)
SELECT variable_logs.created_at, variable_logs.variable_id, variable_logs.value 
FROM variable_logs 
WHERE variable_logs.variable_id = 2 
  AND (variable_logs.created_at BETWEEN '2015-01-01 00:00:00.000000'  
                                    AND '2015-03-09 23:59:00.000000')  
ORDER BY created_at asc;

"Sort  (cost=32374.66..32835.00 rows=184137 width=53) (actual time=150.983..160.467 rows=184369 loops=1)"
"  Sort Key: created_at"
"  Sort Method: quicksort  Memory: 27833kB"
"  Buffers: shared hit=8570"
"  ->  Bitmap Heap Scan on variable_logs  (cost=5188.10..16271.50 rows=184137 width=53) (actual time=33.239..70.201 rows=184369 loops=1)"
"        Recheck Cond: ((variable_id = 2) AND (created_at >= '2015-01-01 00:00:00'::timestamp without time zone) AND (created_at <= '2015-03-09 23:59:00'::timestamp without time zone))"
"        Buffers: shared hit=8570"
"        ->  Bitmap Index Scan on idx_time_limits_inversed  (cost=0.00..5142.06 rows=184137 width=0) (actual time=31.935..31.935 rows=184369 loops=1)"
"              Index Cond: ((variable_id = 2) AND (created_at >= '2015-01-01 00:00:00'::timestamp without time zone) AND (created_at <= '2015-03-09 23:59:00'::timestamp without time zone))"
"              Buffers: shared hit=709"
"Total runtime: 170.038 ms"

我尝试按照指南更改postgres.conf的配置&#34;发布前的事情&#34;在https://wiki.postgresql.org/wiki/Slow_Query_Questions没有成功;(

1 个答案:

答案 0 :(得分:0)

您正在检索表中23%的行。我不希望使用索引会有所帮助,除非它涵盖了查询所需的所有列并提供了所需的排序顺序。

不带索引,然后使用索引(variable_id,created_at,value)