长时间运行的查询PostgreSQL

长时间运行的查询PostgreSQL,sql,postgresql,query-performance,Sql,Postgresql,Query Performance,我需要一些关于PostgreSQL的帮助。我有一个32列200多万行的表。我有两个外键和一个索引。当我查询表时,需要8秒以上的时间才能返回60000多行。我已经尝试了我在网上找到的所有东西,包括关于堆栈溢出的提示,但在查询方面没有任何改进。顺便说一句,该查询是对一半列的简单选择,没有GROUPBY或ORDERBY语句。我运行了EXPLAIN-ANALYZE命令,结果如下。有人能告诉我这里缺少什么吗?事先非常感谢 "Seq Scan on mydatatable a (cost=0.00..33

我需要一些关于PostgreSQL的帮助。我有一个32列200多万行的表。我有两个外键和一个索引。当我查询表时,需要8秒以上的时间才能返回60000多行。我已经尝试了我在网上找到的所有东西,包括关于堆栈溢出的提示,但在查询方面没有任何改进。顺便说一句,该查询是对一半列的简单选择,没有GROUPBY或ORDERBY语句。我运行了EXPLAIN-ANALYZE命令,结果如下。有人能告诉我这里缺少什么吗?事先非常感谢

"Seq Scan on mydatatable a  (cost=0.00..336085.02 rows=40944 width=156) (actual time=1244.135..7139.903 rows=60576 loops=1)"
"  Filter: ((customer_id = 123) AND (vip_period_id = 555))"
"  Rows Removed by Filter: 2070509"
"  SubPlan 1"
"    ->  Limit  (cost=0.00..5.05 rows=1 width=11) (actual time=0.065..0.068 rows=1 loops=60576)"
"          ->  Seq Scan on countries b  (cost=0.00..5.05 rows=1 width=11) (actual time=0.056..0.056 rows=1 loops=60576)"
"                Filter: ((a.endcustomercountry)::text = (iso3)::text)"
"                Rows Removed by Filter: 231"
"Total runtime: 7326.915 ms"

抱歉,我忘记添加查询:

select a.id, a.color, a.grade, round((a.allocationpercentage)::numeric, 2) AS percent, 
    a.product, a.customer, a.actualshipmentdate, 
    round((a.booknet)::numeric, 2) AS allocated, 
    round((a.cost)::numeric, 2) AS cost, 
    round((a.rebate)::numeric, 2) AS rebate, 
    COALESCE(round((a.forecast)::numeric, 2), 0.00) AS forecast, 
    round((a.estimatedforecast)::numeric, 2) AS estforecast, 
    c.countryname, a.transactiondate, a.sellerpo, a.sopo, a.quantity 
    from mydatatable as a left outer join countries as c on a.endcustomercountry = c.iso3
    where a.customer_id = 123 AND a.vip_period_id = 555;

CREATE TABLE mydatatable
(
  id serial NOT NULL,
  sopo integer,
  transactiondate date,
  partner character varying(110),
  color character varying(100),
  grade character varying(100),
  allocationpercentage double precision,
  technologytier character varying(250),
  product character varying(100),
  promotionname character varying(100),
  dealiddartid integer,
  dartdescriptor character varying(250),
  quantity integer,
  routetomarket character varying(100),
  endcustomername character varying(200),
  endcustomerparentname character varying(200),
  endcustomercountry character varying(20),
  shipmentflag character varying(20),
  actualshipmentdate date,
  oipsipflag character varying(250),
  reasondescription character varying(200),
  totalbooknet double precision,
  booknet double precision,
  rebate double precision,
  customer_id integer,
  entry_datetime timestamp without time zone,
  forecast double precision,
  sellerpo character varying(111),
  vip_period_id integer DEFAULT 1,
  pollersessionid integer,
  estimatedcost double precision DEFAULT 0,
  estimatedforecast double precision DEFAULT 0,
  CONSTRAINT mydatatable_pkey PRIMARY KEY (id),
  CONSTRAINT mydatatable_customer_id_fkey FOREIGN KEY (customer_id)
      REFERENCES customer (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT mydatatable_vip_period_id_fkey FOREIGN KEY (vip_period_id)
      REFERENCES vipperiod (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mydatatable
  OWNER TO myowner;

CREATE INDEX idx_mydatatable
  ON mydatatable
  USING btree
  (customer_id, vip_period_id);

请重新格式化问题中的查询,因为它几乎无法阅读,因此我们可以帮助您。我甚至找不到原始的SQL查询。对不起,我忘记添加查询:显示数据库架构。也就是说,向我们显示用于设置数据库的CREATE TABLE和CREATE INDEX语句。如果删除国家/地区匹配的子查询,请从国家/地区中选择b.countryname作为b,其中a.endcustomercountry=b.iso3 limit 1,因为国家/地区的查询时间缩短了?确定。只要我能看到你们有一个包含两个字段的复合索引。尝试为每个字段创建两个索引。 { "Hash Left Join (cost=11101.47..81592.05 rows=39533 width=164) (actual time=73.286..434.876 rows=60576 loops=1)" " Hash Cond: ((a.endcustomercountry)::text = (c.iso3)::text)" " -> Bitmap Heap Scan on mydatatable a (cost=11093.98..79266.39 rows=39299 width=157) (actual time=73.174..104.951 rows=60576 loops=1)" " Recheck Cond: ((customer_id = 123) AND (vip_period_id = 555))" " -> BitmapAnd (cost=11093.98..11093.98 rows=39299 width=0) (actual time=72.700..72.700 rows=0 loops=1)" " -> Bitmap Index Scan on idx_cid_mydatatable (cost=0.00..4038.51 rows=218411 width=0) (actual time=41.377..41.377 rows=221793 loops=1)" " Index Cond: (customer_id = 123)" " -> Bitmap Index Scan on idx_vip_mydatatable (cost=0.00..7035.57 rows=380685 width=0) (actual time=28.829..28.829 rows=372877 loops=1)" " Index Cond: (vip_period_id = 555)" " -> Hash (cost=4.44..4.44 rows=244 width=15) (actual time=0.075..0.075 rows=235 loops=1)" " Buckets: 1024 Batches: 1 Memory Usage: 11kB" " -> Seq Scan on countries c (cost=0.00..4.44 rows=244 width=15) (actual time=0.011..0.037 rows=244 loops=1)" "Total runtime: 436.599 ms" }