长时间运行查询PostgreSQL

时间:2014-11-21 13:39:22

标签: sql postgresql query-performance

我需要一些PostgreSQL的帮助。我有一个包含32列的表,以及200多万行。我有两个外键和一个索引。当我查询表时,返回60,000多行需要8秒以上。我已经尝试了我在网上找到的所有内容,包括有关Stack Overflow的提示,并且对查询没有任何改进。顺便说一下,查询是一半列的简单选择,没有group by或order by语句。我运行了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);
{
"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"
}

0 个答案:

没有答案