postgresql简单选择很慢

时间:2012-01-26 14:42:02

标签: postgresql

我有一张桌子:

CREATE TABLE my_table
(
  id integer NOT NULL DEFAULT nextval('seq_my_table_id'::regclass),
  fk_id1 integer NOT NULL,
  fk_id2 smallint NOT NULL,
  name character varying(255) NOT NULL,
  description text,
  currency_name character varying(3) NOT NULL,
  created timestamp with time zone NOT NULL DEFAULT now(),
  updated timestamp with time zone NOT NULL DEFAULT now(),
  CONSTRAINT "PK_my_table_id" PRIMARY KEY (id ),
  CONSTRAINT "FK_my_table_fk_id1" FOREIGN KEY (fk_id1)
      REFERENCES my_table2 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT "FK_my_table_fk_id2" FOREIGN KEY (fk_id2)
      REFERENCES my_table3 (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (
  OIDS=FALSE,
  autovacuum_enabled=true,
  autovacuum_vacuum_threshold=50,
  autovacuum_vacuum_scale_factor=0.2,
  autovacuum_analyze_threshold=50,
  autovacuum_analyze_scale_factor=0.1,
  autovacuum_vacuum_cost_delay=20,
  autovacuum_vacuum_cost_limit=200,
  autovacuum_freeze_min_age=50000000,
  autovacuum_freeze_max_age=200000000,
  autovacuum_freeze_table_age=150000000
);
ALTER TABLE my_table
  OWNER TO postgres;

CREATE INDEX my_table_fk_id1
  ON my_table
  USING btree
  (fk_id1 );

CREATE INDEX my_table_fk_id2
  ON my_table
  USING btree
  (fk_id2 );

表记录计数

select count(id) from my_table; --24061
select count(id) from my_table2; --24061
select count(id) from my_table3; --123

执行时间

select * from my_table -- ~17sec

vacuum/analyze - 没有效果
description - 每行长约4000个字符

postgres.conf - 标准设置

版本:9.1

选择除描述之外的所有字段将执行时间减少到~1.5秒

如何根据描述选择速度?

UPD

--explain analyze select * from my_table
"Seq Scan on my_table (cost=0.00..3425.79 rows=24079 width=1015) (actual time=0.019..17.238 rows=24079 loops=1)"
"Total runtime: 18.649 ms"

1 个答案:

答案 0 :(得分:1)

问题是如何快速做到这一点。问题不在服务器上,因为它需要18ms。简单的解决方案是选择较少的列,以便通过网络传输更少的列。我的猜测是你对一些人有很长的描述。将该列从您的选择中删除,然后重试。