Hola Hellmuth, corroborando lo que comenta Alvaro sobre la consulta, un
ejemplo:
create table prueba (i int, fecha timestamp );
insert into prueba
select (random()*10000000)::int, fec from generate_series
('2007-02-01'::timestamp
, '2008-04-01'::timestamp
, '1 min'::interval) as fec;
create index idx_btree on prueba (fecha);
create index idx_brin on prueba using brin (fecha);
ANALYZE prueba ;
EXPLAIN ANALYZE
select * from prueba where fecha ='2007-02-01 00:02:00' ;
--selecciona el btree
"Index Scan using idx_btree on prueba (cost=0.43..11.43 rows=2
width=12) (actual time=0.036..0.044 rows=2 loops=1)"
" Index Cond: (fecha = '2007-02-01 00:02:00'::timestamp without time zone)"
"Planning time: 0.224 ms"
"Execution time: 0.115 ms"
EXPLAIN ANALYZE
select * from prueba where fecha >'2007-02-01 00:02:00' and
fecha <'2007-08-10 00:02:00';
--selecciona el brin
"Bitmap Heap Scan on prueba (cost=151.06..23475.16 rows=550176
width=12) (actual time=0.210..68.213 rows=547198 loops=1)"
" Recheck Cond: ((fecha > '2007-02-01 00:02:00'::timestamp without time
zone) AND (fecha < '2007-08-10 00:02:00'::timestamp without time zone))"
" Rows Removed by Index Recheck: 44802"
" Heap Blocks: lossy=3200"
" -> Bitmap Index Scan on idx_brin (cost=0.00..13.51 rows=1113807
width=0) (actual time=0.185..0.185 rows=32000 loops=1)"
" Index Cond: ((fecha > '2007-02-01 00:02:00'::timestamp without
time zone) AND (fecha < '2007-08-10 00:02:00'::timestamp without time
zone))"
"Planning time: 0.222 ms"
"Execution time: 85.913 ms"
Saludos
El 26-06-19 a las 17:07, Alvaro Herrera escribió:
Hellmuth Vargas escribió:
Hola lista
Si sobre una columna de una tabla están definidos tanto un índice btree
como un brin el optimizador siempre va a preferir el btree sobre el brin?
Depende de la consulta, pero en general sí.