PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Perché usare lo stesso campo durante il filtraggio causa tempi di esecuzione diversi? (diverso utilizzo dell'indice)

Come soluzione temporanea al problema possiamo aggiungere una SELECT di un alias per la colonna usata nell'espressione PARTITION BY. Allora PG applica l'ottimizzazione e usa l'indice.

La risposta alla domanda potrebbe essere: PG non applica l'ottimizzazione se viene usato un tipo composite. Nota come funziona:

PARTITION | FILTER  | IS USED?
------------------------------
ALIAS     | ORIG    | NO
NO ALIAS  | ALIAS   | YES
ORIG      | ALIAS   | NO
ORIG      | ORIG    | NO

Vedi questo dbfiddle

create table agreement (
  ag_id int,
  name text,
  cost numeric(10,2)
);
create index ag_idx on agreement (ag_id);
insert into agreement (ag_id, name, cost) values
  ( 1, '333', 22 )
, (1, '333', 33)
, (1, '333', 7)
, (2, '555', 18 )
, (2, '555', 2)
, (3, '777', 4);

select * from agreement;

create function initial ()
  returns table( agreement_id int, ag agreement )
language sql stable AS $$
  select ag_id, t from agreement t;
$$;

select * from initial() t;

explain( analyze, costs, buffers, verbose )
with totals_by_ag as (
  select *, sum( (t.ag).cost ) over ( partition by agreement_id ) as total
  from initial() t
)
select * from totals_by_ag t
where (t.ag).ag_id = 1; -- l'indice NON è USATO

explain( analyze, costs, buffers, verbose )
with totals_by_ag as (
  select *, sum( (t.ag).cost ) over ( partition by agreement_id ) as total
  from initial() t
)
select * from totals_by_ag t
where agreement_id = 1; -- l'indice è usato quando si usa l'alias per la colonna

explain( analyze, costs, buffers, verbose )
with totals_by_ag as (
  select *, sum( (t.ag).cost ) over ( partition by (t.ag).ag_id ) as total --renamed from initial() t
  from initial() t
)
select * from totals_by_ag t
where agreement_id = 1; -- l'indice NON è USATO perché si raggruppa per la colonna originale

explain( analyze, costs, buffers, verbose )
with totals_by_ag as (
  select *, sum( (t.ag).cost ) over ( partition by (t.ag).ag_id ) as total --renamed from initial() t
  from initial() t
)
select * from totals_by_ag t
where (t.ag).ag_id = 1; -- l'indice NON è USATO nemmeno se in entrambi i casi la colonna originale