pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedUseless index
11+ messages / 4 participants
[nested] [flat]
* Useless index
@ 2002-02-14 13:59 Brian McCane <bmccane@mccons.net>
0 siblings, 1 reply; 11+ messages in thread
From: Brian McCane @ 2002-02-14 13:59 UTC (permalink / raw)
To: pgsql-admin
HELP!!!
Okay, now that I have your attention :). I have a table with
about 560M rows in it. Performance is usually pretty good except when I
do a specific query:
SELECT fooid FROM foo
WHERE bazid = 123456
ORDER BY score DESC LIMIT 100 ;
My index looks like:
CREATE INDEX foo_index ON foo (bazid, score) ;
This query should return the 100 highest scores out a possible 1.1M rows
(for the query that got me attention). When I do an explain, I get:
# explain select fooid from foo where bazid = 123456 order by score desc
limit 100 ;
NOTICE: QUERY PLAN:
Limit (cost=1552.10..1552.10 rows=100 width=6)
-> Sort (cost=1552.10..1552.10 rows=382 width=6)
-> Index Scan using foo_index on foo (cost=0.00..1535.69
rows=382 width=6)
EXPLAIN
Doesn't look too bad, but I have a LOT of data where there is only a
single fooid for a bazid, which really skews the EXPLAIN results. On
mysql (and I believe Oracle, gotta find my other hat), I could create the
index as:
CREATE INDEX foo_index ON foo (bazid, score desc) ;
Which would be exactly what I want, and would complete in a split second.
Instead, this thing runs FOREVER (okay, it just seems that way to my
client :). Is there any way to get the equivalent index from PostgreSQL?
This is a major show stopper for me at this point. I have looked through
Chapter 7 of the 'idocs', but I didn't find anything that would help.
- brian
Wm. Brian McCane | Life is full of doors that won't open
Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 15:06 Tom Lane <tgl@sss.pgh.pa.us>
parent: Brian McCane <bmccane@mccons.net>
0 siblings, 2 replies; 11+ messages in thread
From: Tom Lane @ 2002-02-14 15:06 UTC (permalink / raw)
To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin
Brian McCane <bmccane@mccons.net> writes:
> CREATE INDEX foo_index ON foo (bazid, score desc) ;
> Which would be exactly what I want, and would complete in a split second.
> Instead, this thing runs FOREVER (okay, it just seems that way to my
> client :). Is there any way to get the equivalent index from PostgreSQL?
You don't need a funny index, you just need to get the planner to notice
that that index can serve to create the desired output ordering. Try
create table foo(bazid int, score int);
CREATE INDEX foo_index ON foo (bazid, score) ;
explain select * from foo where bazid = 123456
order by bazid desc, score desc limit 100 ;
NOTICE: QUERY PLAN:
Limit (cost=0.00..17.07 rows=5 width=8)
-> Index Scan Backward using foo_index on foo (cost=0.00..17.07 rows=5 width=8)
EXPLAIN
regards, tom lane
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 15:15 Brian McCane <bmccane@mccons.net>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 11+ messages in thread
From: Brian McCane @ 2002-02-14 15:15 UTC (permalink / raw)
To: pgsql-admin
On Thu, 14 Feb 2002, Tom Lane wrote:
> Brian McCane <bmccane@mccons.net> writes:
> > CREATE INDEX foo_index ON foo (bazid, score desc) ;
>
> > Which would be exactly what I want, and would complete in a split second.
> > Instead, this thing runs FOREVER (okay, it just seems that way to my
> > client :). Is there any way to get the equivalent index from PostgreSQL?
>
> You don't need a funny index, you just need to get the planner to notice
> that that index can serve to create the desired output ordering. Try
>
> create table foo(bazid int, score int);
> CREATE INDEX foo_index ON foo (bazid, score) ;
>
> explain select * from foo where bazid = 123456
> order by bazid desc, score desc limit 100 ;
>
> NOTICE: QUERY PLAN:
>
> Limit (cost=0.00..17.07 rows=5 width=8)
> -> Index Scan Backward using foo_index on foo (cost=0.00..17.07 rows=5 width=8)
>
> EXPLAIN
>
>
> regards, tom lane
>
Thanks,
That did it. I tried the explain with "score desc, bazid desc"
and it looked the same to me. I had myself convinced it had to be that
way. Then I tried your way (after I realized I had already selected a
specific bazid so it didn't matter if it was descending :), and it was all
okay.
-brian
Wm. Brian McCane | Life is full of doors that won't open
Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 15:34 Tom Lane <tgl@sss.pgh.pa.us>
parent: Brian McCane <bmccane@mccons.net>
0 siblings, 0 replies; 11+ messages in thread
From: Tom Lane @ 2002-02-14 15:34 UTC (permalink / raw)
To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin
Brian McCane <bmccane@mccons.net> writes:
> Then I tried your way (after I realized I had already selected a
> specific bazid so it didn't matter if it was descending :),
Yup, that's the little flash of insight that the planner is incapable
of having :-(
regards, tom lane
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 15:42 Bruce Momjian <pgman@candle.pha.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 11+ messages in thread
From: Bruce Momjian @ 2002-02-14 15:42 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Brian McCane <bmccane@mccons.net>; pgsql-admin
Tom Lane wrote:
> Brian McCane <bmccane@mccons.net> writes:
> > CREATE INDEX foo_index ON foo (bazid, score desc) ;
>
> > Which would be exactly what I want, and would complete in a split second.
> > Instead, this thing runs FOREVER (okay, it just seems that way to my
> > client :). Is there any way to get the equivalent index from PostgreSQL?
>
> You don't need a funny index, you just need to get the planner to notice
> that that index can serve to create the desired output ordering. Try
>
> create table foo(bazid int, score int);
> CREATE INDEX foo_index ON foo (bazid, score) ;
>
> explain select * from foo where bazid = 123456
> order by bazid desc, score desc limit 100 ;
>
> NOTICE: QUERY PLAN:
>
> Limit (cost=0.00..17.07 rows=5 width=8)
> -> Index Scan Backward using foo_index on foo (cost=0.00..17.07 rows=5 width=8)
Yes, I suspected it was the mixing of non-DESC and DESC that caused the
index to be ignored. The user knows the index can be used because the
have specified 'col = constant' but the index doesn't code doesn't have
those details. Certainly a case of:
col >= 10 AND col <= 11
would not allow a secondary DESC column to be used, while a secondary
non-DESC column would be fine. I suppose there is no automatic fix we
can do here except to ask users to use matching DESC when they are
testing or a constant.
In fact, I am unsure why you are specifying the primary column in the
ORDER BY anyway if you know it will be a single value, except perhaps to
try and get it to use the index, right?
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 16:05 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bruce Momjian <pgman@candle.pha.pa.us>
0 siblings, 2 replies; 11+ messages in thread
From: Tom Lane @ 2002-02-14 16:05 UTC (permalink / raw)
To: Bruce Momjian <pgman@candle.pha.pa.us>; +Cc: Brian McCane <bmccane@mccons.net>; pgsql-admin
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> In fact, I am unsure why you are specifying the primary column in the
> ORDER BY anyway if you know it will be a single value, except perhaps to
> try and get it to use the index, right?
Exactly. The sort ordering of the index is (col1,col2) while the
query as originally written wanted an ordering of (col2 desc).
The planner's not smart enough to realize that since the WHERE
constrains col1 to a single value, you could pretend the query
requests an ordering of (col1 desc, col2 desc) which is compatible
with the index.
regards, tom lane
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 16:19 Brian McCane <bmccane@mccons.net>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 11+ messages in thread
From: Brian McCane @ 2002-02-14 16:19 UTC (permalink / raw)
To: pgsql-admin
On Thu, 14 Feb 2002, Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > In fact, I am unsure why you are specifying the primary column in the
> > ORDER BY anyway if you know it will be a single value, except perhaps to
> > try and get it to use the index, right?
>
> Exactly. The sort ordering of the index is (col1,col2) while the
> query as originally written wanted an ordering of (col2 desc).
> The planner's not smart enough to realize that since the WHERE
> constrains col1 to a single value, you could pretend the query
> requests an ordering of (col1 desc, col2 desc) which is compatible
> with the index.
>
> regards, tom lane
>
The annoying thing is that it is smart enough to get the correct index, I
have another index that has the right col1, but different col2. It seems
it should be possible to have the optimizer leave col1 in a "high
impedence" state, and then allow col2 to make a decision. I have not
looked at the code, but if we have a known value for col1 as a constant,
I would think we could let subsequent information make the decision and
then just bubble it up to the top.
Wm. Brian McCane | Life is full of doors that won't open
Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 16:40 Jean-Luc Lachance <jllachan@nsd.ca>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 11+ messages in thread
From: Jean-Luc Lachance @ 2002-02-14 16:40 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-admin
Here is another peeve
Having to specify the type of a constant in a SELECT DISTINCT 'foo'...
I which I had more spare time to contribute...
Jean-Luc
Tom Lane wrote:
>
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > In fact, I am unsure why you are specifying the primary column in the
> > ORDER BY anyway if you know it will be a single value, except perhaps to
> > try and get it to use the index, right?
>
> Exactly. The sort ordering of the index is (col1,col2) while the
> query as originally written wanted an ordering of (col2 desc).
> The planner's not smart enough to realize that since the WHERE
> constrains col1 to a single value, you could pretend the query
> requests an ordering of (col1 desc, col2 desc) which is compatible
> with the index.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 16:52 Bruce Momjian <pgman@candle.pha.pa.us>
parent: Brian McCane <bmccane@mccons.net>
0 siblings, 1 reply; 11+ messages in thread
From: Bruce Momjian @ 2002-02-14 16:52 UTC (permalink / raw)
To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin
Brian McCane wrote:
> On Thu, 14 Feb 2002, Tom Lane wrote:
>
> > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > In fact, I am unsure why you are specifying the primary column in the
> > > ORDER BY anyway if you know it will be a single value, except perhaps to
> > > try and get it to use the index, right?
> >
> > Exactly. The sort ordering of the index is (col1,col2) while the
> > query as originally written wanted an ordering of (col2 desc).
> > The planner's not smart enough to realize that since the WHERE
> > constrains col1 to a single value, you could pretend the query
> > requests an ordering of (col1 desc, col2 desc) which is compatible
> > with the index.
> >
> > regards, tom lane
> >
>
> The annoying thing is that it is smart enough to get the correct index, I
> have another index that has the right col1, but different col2. It seems
> it should be possible to have the optimizer leave col1 in a "high
> impedence" state, and then allow col2 to make a decision. I have not
> looked at the code, but if we have a known value for col1 as a constant,
> I would think we could let subsequent information make the decision and
> then just bubble it up to the top.
Tom, do we have a TODO item here?
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 17:16 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bruce Momjian <pgman@candle.pha.pa.us>
0 siblings, 1 reply; 11+ messages in thread
From: Tom Lane @ 2002-02-14 17:16 UTC (permalink / raw)
To: Bruce Momjian <pgman@candle.pha.pa.us>; +Cc: Brian McCane <bmccane@mccons.net>; pgsql-admin
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Tom, do we have a TODO item here?
I'm not excited about it; it seems a weird case and there's a simple
query change to get the right thing to happen. AFAICS we'd have to
expend a lot of planner cycles to detect this case, and most of the
time those cycles would be wasted.
regards, tom lane
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Useless index
@ 2002-02-14 17:19 Bruce Momjian <pgman@candle.pha.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 11+ messages in thread
From: Bruce Momjian @ 2002-02-14 17:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Brian McCane <bmccane@mccons.net>; pgsql-admin
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > Tom, do we have a TODO item here?
>
> I'm not excited about it; it seems a weird case and there's a simple
> query change to get the right thing to happen. AFAICS we'd have to
> expend a lot of planner cycles to detect this case, and most of the
> time those cycles would be wasted.
Yes, however, in our case the TODO list or FAQ does show tips or
limitations of our existing code. Not sure either.
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2002-02-14 17:19 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-02-14 13:59 Useless index Brian McCane <bmccane@mccons.net>
2002-02-14 15:06 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-02-14 15:15 ` Brian McCane <bmccane@mccons.net>
2002-02-14 15:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-02-14 15:42 ` Bruce Momjian <pgman@candle.pha.pa.us>
2002-02-14 16:05 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-02-14 16:19 ` Brian McCane <bmccane@mccons.net>
2002-02-14 16:52 ` Bruce Momjian <pgman@candle.pha.pa.us>
2002-02-14 17:16 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-02-14 17:19 ` Bruce Momjian <pgman@candle.pha.pa.us>
2002-02-14 16:40 ` Jean-Luc Lachance <jllachan@nsd.ca>
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