Sql 如何使用大数据优化此查询

Sql 如何使用大数据优化此查询,sql,postgresql,optimization,greatest-n-per-group,Sql,Postgresql,Optimization,Greatest N Per Group,-=============================== CREATE TABLE ticket ( id serial NOT NULL, source integer NOT NULL, status integer NOT NULL, ticket_type integer NOT NULL, remaining_uses integer NOT NULL, expiry_date timestamp with time zone NOT NULL, p

-===============================

CREATE TABLE ticket
(
  id serial NOT NULL,
  source integer NOT NULL,
  status integer NOT NULL,
  ticket_type integer NOT NULL,
  remaining_uses integer NOT NULL,
  expiry_date timestamp with time zone NOT NULL,
  price numeric(20,2) NOT NULL,
  created_date timestamp with time zone NOT NULL,
  pax_type integer NOT NULL,
  last_updated timestamp with time zone NOT NULL,
  service integer,
  client_id character varying(50),
  CONSTRAINT skybus_ticket_pkey PRIMARY KEY (id),
  CONSTRAINT skybus_ticket_sale_id_fkey FOREIGN KEY (sale_id)
      REFERENCES skybus_sale (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (
  OIDS=FALSE
);
ALTER TABLE ticket
  OWNER TO umd;

-- Index: ticket_client_id_idx

-- DROP INDEX ticket_client_id_idx;

CREATE INDEX ticket_client_id_idx
  ON ticket
  USING btree
  (client_id COLLATE pg_catalog."default");

-- Index: ticket_profile_id_idx

-- DROP INDEX ticket_profile_id_idx;

CREATE INDEX ticket_profile_id_idx
  ON ticket
  USING btree
  (profile_id);

-- Index: ticket_sale_id

-- DROP INDEX ticket_sale_id;

CREATE INDEX skybus_ticket_sale_id
  ON ticket
  USING btree
  (sale_id);

-- Index: ticket_ticket_number

-- DROP INDEX ticket_ticket_number;

CREATE INDEX ticket_ticket_number
  ON ticket
  USING btree
  (ticket_number COLLATE pg_catalog."default");

-- Index: ticket_ticket_number_like

-- DROP INDEX ticket_ticket_number_like;

CREATE INDEX ticket_ticket_number_like
  ON ticket
  USING btree
  (ticket_number COLLATE pg_catalog."default" varchar_pattern_ops);

-- Index: ticket_topup_for_idx

-- DROP INDEX ticket_topup_for_idx;

CREATE INDEX ticket_topup_for_idx
  ON ticket
  USING btree
  (topup_for COLLATE pg_catalog."default");
-====执行计划-这是行号更改

CREATE TABLE tickethistory
(
  id serial NOT NULL,
  ticket_id integer,
  action integer NOT NULL,
  action_result integer NOT NULL,
  initial_status integer NOT NULL,
  final_status integer NOT NULL,
  final_remaining_uses integer NOT NULL,
  ticket_type integer NOT NULL,
  action_when timestamp with time zone NOT NULL,
  last_updated timestamp with time zone NOT NULL,
  service integer,
  CONSTRAINT tickethistory_pkey PRIMARY KEY (id),
  CONSTRAINT tickethistory_ticket_id_fkey FOREIGN KEY (ticket_id)
      REFERENCES ticket (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (
  OIDS=FALSE
);
ALTER TABLE tickethistory
  OWNER TO umd;

-- Index: tickethistory_ticket_id

-- DROP INDEX tickethistory_ticket_id;

CREATE INDEX tickethistory_ticket_id
  ON tickethistory
  USING btree
  (ticket_id);
您可以使用row_number在一次通行中获取每张票的最新一行:

    "HashAggregate  (cost=4526158.63..4526158.64 rows=1 width=16) (actual time=382849.323..382849.376 rows=41 loops=1)"
"  ->  Nested Loop  (cost=3880592.94..4526158.62 rows=1 width=16) (actual time=380338.613..382825.688 rows=11745 loops=1)"
"        ->  Subquery Scan on sub  (cost=3880592.94..4463424.47 rows=6563 width=8) (actual time=126346.043..258837.523 rows=293717 loops=1)"
"              Filter: ((sub.remaining_uses > 0) AND (sub.rn = 1) AND (sub.status = 1))"
"              Rows Removed by Filter: 15244064"
"              ->  WindowAgg  (cost=3880592.94..4191436.42 rows=15542174 width=203) (actual time=126345.775..237172.180 rows=15537781 loops=1)"
"                    ->  Sort  (cost=3880592.94..3919448.38 rows=15542174 width=203) (actual time=126345.757..180461.191 rows=15537781 loops=1)"
"                          Sort Key: th.ticket_id, th.*"
"                          Sort Method: external merge  Disk: 3050616kB"
"                          ->  Seq Scan on skybus_tickethistory th  (cost=0.00..483544.74 rows=15542174 width=203) (actual time=14.091..53312.782 rows=15537781 loops=1)"
"        ->  Index Scan using skybus_ticket_pkey on skybus_ticket t  (cost=0.00..9.55 rows=1 width=12) (actual time=0.418..0.418 rows=0 loops=293717)"
"              Index Cond: (id = sub.ticket_id)"
"              Filter: ((source = ANY ('{0,1,2,6,7,8}'::integer[])) AND (created_date < ('now'::cstring)::date) AND (expiry_date >= (('now'::cstring)::date - 1)) AND (created_date > (('now'::cstring)::date - 30)) AND (ticket_type = ANY ('{2,3,4,5,6,7,16,17, (...)"
"              Rows Removed by Filter: 1"
"Total runtime: 383045.381 ms"
distinct on通常是解决博士后问题的最快方法:

with    last_history as
        (
        select  *
        from    (
                select  row_number() over (partition by ticket_id
                                           order by th desc) rn
                ,       *
                from    TicketHistory
                ) sub
        where   rn = 1 -- Latest history row only
        )
select  *
from    ticket t
join    th
on      t.id = th.ticket_id
where   remaining_uses > 0
        and <... other conditions ...>
distinct on与order by一起返回每个票证id的tickethistory.id值最高的行

在tickethistory ticket\u id,id desc上建立索引可能会有所帮助。甚至可能还有一个关于tickethistory ticket\u id、id desc、final\u剩余使用、final\u状态、action\u何时启用仅索引扫描


但是,存储创建时刻的时间戳列可能更准确。如果tickethistory.id是通过序列生成的id,因为它是串行的,那么这些值可能不会反映实际的插入顺序

这当然会缩短查询时间。那么我需要使用distinct还是这种方法?我的印象是,我现在做的方式更快捷。我会选择一个有没有名字的马的答案,这比行号更可读:@a有没有名字的马,我编辑了这个问题,这不是你查询的执行计划-执行计划有一个WindowAgg步骤,但你的查询没有窗口函数。原始查询的计划可能更有用,也可能是针对distinct on解决方案的计划
with    last_history as
        (
        select  *
        from    (
                select  row_number() over (partition by ticket_id
                                           order by th desc) rn
                ,       *
                from    TicketHistory
                ) sub
        where   rn = 1 -- Latest history row only
        )
select  *
from    ticket t
join    th
on      t.id = th.ticket_id
where   remaining_uses > 0
        and <... other conditions ...>
select ticket_type,f_rows.remaining_uses,t.source,count(t.id) as total
FROM (
    -- Filter rows to get those where remaining_uses > 0 and status = 1 
    SELECT * 
    FROM (
        --Get all the latest rows for each ticket
        SELECT distinct on (ticket_id) 
               ticket_id, 
               final_remaining_uses AS remaining_uses, 
               final_status AS status, action_when 
        FROM TicketHistory th
        order by ticket_id, id desc
    ) latest_rows
    WHERE remaining_uses > 0 
      AND status = 1 --and (action_when  current_date -30)
) f_rows 
  JOIN Ticket t ON f_rows.ticket_id = t.id
WHERE t.expiry_date >= current_date -1 
  and source in (0,1,2,6,7,8) 
  and created_date  current_date - 30
GROUP BY ticket_type, f_rows.remaining_uses, t.source
order by source, ticket_type, remaining_uses;