Re: Postgresql siempre prefiere indices btree sobre brin

2019-06-26 Thread Anthony Sotolongo
Hola nuevamente, desconozco los % exactamente pero te dejo algunos enlaces
que me ayudaron en su momento a comprender un poco mas los BRIN, y puede
que te ayude a ti también  a conocer a los índices BRIN y determinar si te
ajustan a tu escenario y tal vez comprender el motivo del optimizador de
escoger uno u otro.

https://www.postgresql.org/docs/10/brin-intro.html

https://en.m.wikipedia.org/wiki/Block_Range_Index

https://info.crunchydata.com/blog/postgresql-brin-indexes-big-data-performance-with-minimal-storage


https://m.habr.com/en/company/postgrespro/blog/452900/

https://www.postgresql.fastware.com/blog/brin-indexes-what-are-they-and-how-do-you-use-them


Saludos

El mié., 26 de jun. de 2019 10:05 p. m., Hellmuth Vargas 
escribió:

> Hola
>
> Muchas gracias por la oportuna respuesta, quiere decir que  cuando el
> intervalo o rango del filtro sea amplio y/o  la cantidad de registros  que
> cumplan la condición sea mayor que el 20% de los registros es preferido el
> BRIN, osea esta mas enforcado en traer volumen... se podría deducir...
>
> El mié., 26 de jun. de 2019 a la(s) 16:25, Anthony Sotolongo (
> asotolo...@gmail.com) escribió:
>
>> Hola Hellmuth, corroborando lo que comenta Alvaro sobre la consulta, un
>> ejemplo:
>>
>>
>> createtable  prueba (i int, fecha timestamp );
>>
>> insert into prueba
>> select (random()*1000)::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í.
>> >
>>
>
>
> --
> Cordialmente,
>
> Ing. Hellmuth I. Vargas S.
> Esp. Telemática y Negocios por Internet
> Oracle Database 10g Administrator Certified Associate
> EnterpriseDB Certified PostgreSQL 9.3 Associate
>
>


Re: Postgresql siempre prefiere indices btree sobre brin

2019-06-26 Thread Hellmuth Vargas
Hola

Muchas gracias por la oportuna respuesta, quiere decir que  cuando el
intervalo o rango del filtro sea amplio y/o  la cantidad de registros  que
cumplan la condición sea mayor que el 20% de los registros es preferido el
BRIN, osea esta mas enforcado en traer volumen... se podría deducir...

El mié., 26 de jun. de 2019 a la(s) 16:25, Anthony Sotolongo (
asotolo...@gmail.com) escribió:

> Hola Hellmuth, corroborando lo que comenta Alvaro sobre la consulta, un
> ejemplo:
>
>
> createtable  prueba (i int, fecha timestamp );
>
> insert into prueba
> select (random()*1000)::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í.
> >
>


-- 
Cordialmente,

Ing. Hellmuth I. Vargas S.
Esp. Telemática y Negocios por Internet
Oracle Database 10g Administrator Certified Associate
EnterpriseDB Certified PostgreSQL 9.3 Associate


Re: Postgresql siempre prefiere indices btree sobre brin

2019-06-26 Thread Anthony Sotolongo
Hola Hellmuth, corroborando lo que comenta Alvaro sobre la consulta, un 
ejemplo:



create    table  prueba (i int, fecha timestamp );

insert into prueba
select (random()*1000)::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í.






Postgresql siempre prefiere indices btree sobre brin

2019-06-26 Thread Hellmuth Vargas
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?