pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re-2: BUG #7495: chosen wrong index
6+ messages / 2 participants
[nested] [flat]

* Re-2: BUG #7495: chosen wrong index
@ 2012-08-16 10:12  psql@elbrief.de
  4 siblings, 0 replies; 6+ messages in thread

From: psql@elbrief.de @ 2012-08-16 10:12 UTC (permalink / raw)
  To: pgsql-bugs

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> wrote:
> insert into bla ( a , b )
>   select a , floor(random() * 1000000)   1
>   from generate_series( 1 , 1000000 ) as a ( a ) ;
>  
> On my machine, with that data, all of the queries run fast.

Yes, this runs by me fast too. But here is no relation
between a and b. By my data psql must scan mostly all data
to find the rows.

This is only an example. In my live environment i have a table with
a boolean where the boolean is usually true. The boolean is
false on new or changed entrys and if i select the false-rows
order by primary key i get slow querys. The table has a lot of
million rows and a very small amount of rows with false. The
boolean indicates that this row has an entry in a second table
so i can select the rows without a join which is expensive too.

BTW: it would be nice to have an index wich works on
select * from a left join b on b.id = a.id where b.id is null. 

explain select * from bla where b > 990000 order by a limit 10 ;
                                  QUERY PLAN
-------------------------------------------------------------------------------
 Limit  (cost=0.00..30.75 rows=10 width=8)
   ->  Index Scan using bla_a on bla  (cost=0.00..30747.29 rows=10000 width=8)
         Filter: (b > 990000)

drop index bla_a ;

explain select * from bla where b > 990000 order by a limit 10 ;
                                    QUERY PLAN
-----------------------------------------------------------------------------------
 Limit  (cost=633.50..633.52 rows=10 width=8)
   ->  Sort  (cost=633.50..658.50 rows=10000 width=8)
         Sort Key: a
         ->  Index Scan using bla_b on bla  (cost=0.00..417.40 rows=10000 width=8)
               Index Cond: (b > 990000)

The first explain reduce by the limit the cost by the faktor 10/10000.
This is good for data with no relation between a and b. But in my
example it is about 1000 times higher.

BTW: in the first explain this is not an Index Scan, it is an
sequential  scan on an index. It would be nice to show this in
the explain.

Perhaps it would be good to reduce the cost for the limit on
an sequential scan on an index not linear.


Best regards,
Andreas




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re-2: BUG #7495: chosen wrong index
@ 2012-08-16 10:13  psql@elbrief.de
  4 siblings, 0 replies; 6+ messages in thread

From: psql@elbrief.de @ 2012-08-16 10:13 UTC (permalink / raw)
  To: pgsql-bugs

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> wrote:
> insert into bla ( a , b )
>   select a , floor(random() * 1000000)   1
>   from generate_series( 1 , 1000000 ) as a ( a ) ;
>  
> On my machine, with that data, all of the queries run fast.

Yes, this runs by me fast too. But here is no relation
between a and b. By my data psql must scan mostly all data
to find the rows.

This is only an example. In my live environment i have a table with
a boolean where the boolean is usually true. The boolean is
false on new or changed entrys and if i select the false-rows
order by primary key i get slow querys. The table has a lot of
million rows and a very small amount of rows with false. The
boolean indicates that this row has an entry in a second table
so i can select the rows without a join which is expensive too.

BTW: it would be nice to have an index wich works on
select * from a left join b on b.id = a.id where b.id is null. 

explain select * from bla where b > 990000 order by a limit 10 ;
                                  QUERY PLAN
-------------------------------------------------------------------------------
 Limit  (cost=0.00..30.75 rows=10 width=8)
   ->  Index Scan using bla_a on bla  (cost=0.00..30747.29 rows=10000 width=8)
         Filter: (b > 990000)

drop index bla_a ;

explain select * from bla where b > 990000 order by a limit 10 ;
                                    QUERY PLAN
-----------------------------------------------------------------------------------
 Limit  (cost=633.50..633.52 rows=10 width=8)
   ->  Sort  (cost=633.50..658.50 rows=10000 width=8)
         Sort Key: a
         ->  Index Scan using bla_b on bla  (cost=0.00..417.40 rows=10000 width=8)
               Index Cond: (b > 990000)

The first explain reduce by the limit the cost by the faktor 10/10000.
This is good for data with no relation between a and b. But in my
example it is about 1000 times higher.

BTW: in the first explain this is not an Index Scan, it is an
sequential  scan on an index. It would be nice to show this in
the explain.

Perhaps it would be good to reduce the cost for the limit on
an sequential scan on an index not linear.


Best regards,
Andreas




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re-2: BUG #7495: chosen wrong index
@ 2012-08-16 10:13  psql@elbrief.de
  4 siblings, 0 replies; 6+ messages in thread

From: psql@elbrief.de @ 2012-08-16 10:13 UTC (permalink / raw)
  To: pgsql-bugs

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> wrote:
> insert into bla ( a , b )
>   select a , floor(random() * 1000000)   1
>   from generate_series( 1 , 1000000 ) as a ( a ) ;
>  
> On my machine, with that data, all of the queries run fast.

Yes, this runs by me fast too. But here is no relation
between a and b. By my data psql must scan mostly all data
to find the rows.

This is only an example. In my live environment i have a table with
a boolean where the boolean is usually true. The boolean is
false on new or changed entrys and if i select the false-rows
order by primary key i get slow querys. The table has a lot of
million rows and a very small amount of rows with false. The
boolean indicates that this row has an entry in a second table
so i can select the rows without a join which is expensive too.

BTW: it would be nice to have an index wich works on
select * from a left join b on b.id = a.id where b.id is null. 

explain select * from bla where b > 990000 order by a limit 10 ;
                                  QUERY PLAN
-------------------------------------------------------------------------------
 Limit  (cost=0.00..30.75 rows=10 width=8)
   ->  Index Scan using bla_a on bla  (cost=0.00..30747.29 rows=10000 width=8)
         Filter: (b > 990000)

drop index bla_a ;

explain select * from bla where b > 990000 order by a limit 10 ;
                                    QUERY PLAN
-----------------------------------------------------------------------------------
 Limit  (cost=633.50..633.52 rows=10 width=8)
   ->  Sort  (cost=633.50..658.50 rows=10000 width=8)
         Sort Key: a
         ->  Index Scan using bla_b on bla  (cost=0.00..417.40 rows=10000 width=8)
               Index Cond: (b > 990000)

The first explain reduce by the limit the cost by the faktor 10/10000.
This is good for data with no relation between a and b. But in my
example it is about 1000 times higher.

BTW: in the first explain this is not an Index Scan, it is an
sequential  scan on an index. It would be nice to show this in
the explain.

Perhaps it would be good to reduce the cost for the limit on
an sequential scan on an index not linear.


Best regards,
Andreas




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re-2: BUG #7495: chosen wrong index
@ 2012-08-16 10:14  psql@elbrief.de
  4 siblings, 1 reply; 6+ messages in thread

From: psql@elbrief.de @ 2012-08-16 10:14 UTC (permalink / raw)
  To: pgsql-bugs

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> wrote:
> insert into bla ( a , b )
>   select a , floor(random() * 1000000)   1
>   from generate_series( 1 , 1000000 ) as a ( a ) ;
>  
> On my machine, with that data, all of the queries run fast.

Yes, this runs by me fast too. But here is no relation
between a and b. By my data psql must scan mostly all data
to find the rows.

This is only an example. In my live environment i have a table with
a boolean where the boolean is usually true. The boolean is
false on new or changed entrys and if i select the false-rows
order by primary key i get slow querys. The table has a lot of
million rows and a very small amount of rows with false. The
boolean indicates that this row has an entry in a second table
so i can select the rows without a join which is expensive too.

BTW: it would be nice to have an index wich works on
select * from a left join b on b.id = a.id where b.id is null. 

explain select * from bla where b > 990000 order by a limit 10 ;
                                  QUERY PLAN
-------------------------------------------------------------------------------
 Limit  (cost=0.00..30.75 rows=10 width=8)
   ->  Index Scan using bla_a on bla  (cost=0.00..30747.29 rows=10000 width=8)
         Filter: (b > 990000)

drop index bla_a ;

explain select * from bla where b > 990000 order by a limit 10 ;
                                    QUERY PLAN
-----------------------------------------------------------------------------------
 Limit  (cost=633.50..633.52 rows=10 width=8)
   ->  Sort  (cost=633.50..658.50 rows=10000 width=8)
         Sort Key: a
         ->  Index Scan using bla_b on bla  (cost=0.00..417.40 rows=10000 width=8)
               Index Cond: (b > 990000)

The first explain reduce by the limit the cost by the faktor 10/10000.
This is good for data with no relation between a and b. But in my
example it is about 1000 times higher.

BTW: in the first explain this is not an Index Scan, it is an
sequential  scan on an index. It would be nice to show this in
the explain.

Perhaps it would be good to reduce the cost for the limit on
an sequential scan on an index not linear.


Best regards,
Andreas




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re-2: BUG #7495: chosen wrong index
@ 2012-08-16 10:15  psql@elbrief.de
  4 siblings, 0 replies; 6+ messages in thread

From: psql@elbrief.de @ 2012-08-16 10:15 UTC (permalink / raw)
  To: pgsql-bugs

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> wrote:
> insert into bla ( a , b )
>   select a , floor(random() * 1000000)   1
>   from generate_series( 1 , 1000000 ) as a ( a ) ;
>  
> On my machine, with that data, all of the queries run fast.

Yes, this runs by me fast too. But here is no relation
between a and b. By my data psql must scan mostly all data
to find the rows.

This is only an example. In my live environment i have a table with
a boolean where the boolean is usually true. The boolean is
false on new or changed entrys and if i select the false-rows
order by primary key i get slow querys. The table has a lot of
million rows and a very small amount of rows with false. The
boolean indicates that this row has an entry in a second table
so i can select the rows without a join which is expensive too.

BTW: it would be nice to have an index wich works on
select * from a left join b on b.id = a.id where b.id is null. 

explain select * from bla where b > 990000 order by a limit 10 ;
                                  QUERY PLAN
-------------------------------------------------------------------------------
 Limit  (cost=0.00..30.75 rows=10 width=8)
   ->  Index Scan using bla_a on bla  (cost=0.00..30747.29 rows=10000 width=8)
         Filter: (b > 990000)

drop index bla_a ;

explain select * from bla where b > 990000 order by a limit 10 ;
                                    QUERY PLAN
-----------------------------------------------------------------------------------
 Limit  (cost=633.50..633.52 rows=10 width=8)
   ->  Sort  (cost=633.50..658.50 rows=10000 width=8)
         Sort Key: a
         ->  Index Scan using bla_b on bla  (cost=0.00..417.40 rows=10000 width=8)
               Index Cond: (b > 990000)

The first explain reduce by the limit the cost by the faktor 10/10000.
This is good for data with no relation between a and b. But in my
example it is about 1000 times higher.

BTW: in the first explain this is not an Index Scan, it is an
sequential  scan on an index. It would be nice to show this in
the explain.

Perhaps it would be good to reduce the cost for the limit on
an sequential scan on an index not linear.


Best regards,
Andreas




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Re-2: BUG #7495: chosen wrong index
@ 2012-08-16 16:46  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: psql@elbrief.de
  0 siblings, 0 replies; 6+ messages in thread

From: Kevin Grittner @ 2012-08-16 16:46 UTC (permalink / raw)
  To: psql@elbrief.de; pgsql-bugs

<psql@elbrief.de> wrote:
 
> In my live environment i have a table with a boolean where the
> boolean is usually true. The boolean is false on new or changed
> entrys and if i select the false-rows order by primary key i get
> slow querys. The table has a lot of million rows and a very small
> amount of rows with false.
 
That sure sounds like a situation where you should use a partial
index.
 
http://www.postgresql.org/docs/current/interactive/indexes-partial.html
 
-Kevin




^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2012-08-16 16:46 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-08-16 10:12 ` psql@elbrief.de
2012-08-16 10:13 ` psql@elbrief.de
2012-08-16 10:13 ` psql@elbrief.de
2012-08-16 10:14 ` psql@elbrief.de
2012-08-16 16:46   ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-08-16 10:15 ` psql@elbrief.de

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox