From: Li Jin <ljin@tripadvisor.com>
To: Robert Klemme <shortcutter@googlemail.com>
Cc: Merlin Moncure <mmoncure@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Performance penalty when using WITH
Date: Wed, 3 Aug 2011 09:27:11 -0400
Message-ID: <D64784E1-ACFF-4CC8-93E9-EE86620BBF01@tripadvisor.com> (raw)
In-Reply-To: <CAM9pMnMmqpMyMBvp8RPocevcYED89tWxL0LfrjYrnzPOOyvQCw@mail.gmail.com>
References: <339959BC-3A27-4269-8A3E-4C157EBABB86@tripadvisor.com>
<CAM9pMnNN6t5NhFvnrDuheJUFrj5+-HfW4TE3ggguZN2B8==CBA@mail.gmail.com>
<CAHyXU0w+cZcFB+REAKbFcdB_YhU62_375cQdqT=VVE90v5MuRw@mail.gmail.com>
<CAM9pMnMmqpMyMBvp8RPocevcYED89tWxL0LfrjYrnzPOOyvQCw@mail.gmail.com>
Robert,
I've built an index on this expression firstname || ' ' || substring(lastname,1,1). I believe this is the best index for this particular query. Correct me if I am wrong.
Li
On Aug 3, 2011, at 3:18 AM, Robert Klemme wrote:
> On Tue, Aug 2, 2011 at 11:48 PM, Merlin Moncure <mmoncure@gmail.com> wrote:
>> On Sat, Jul 30, 2011 at 8:10 AM, Robert Klemme
>> <shortcutter@googlemail.com> wrote:
>>> On Thu, Jul 28, 2011 at 11:00 PM, Li Jin <ljin@tripadvisor.com> wrote:
>>>> I met with the problem that when I was using WITH clause to reuse a
>>>> subquery, I got a huge performance penalty because of query planner.
>>>> Here are the details, the original query is
>>>> EXPLAIN ANALYZE WITH latest_identities AS
>>>> (
>>>> SELECT DISTINCT ON (memberid) memberid, username, changedate
>>>> FROM t_username_history
>>>> WHERE memberid IN (SELECT memberid FROM t_member WHERE firstname || ' '
>>>> || substring(lastname,1,1) = 'Eddie T')
>>>> ORDER BY memberid, changedate DESC
>>>> )
>>>
>>> Another observation: That criterion looks suspicious to me. I would
>>> expect any RDBMS to be better able to optimize this:
>>>
>>> WHERE firstname = 'Eddie' AND lastname like 'T%'
>>>
>>> I know it's semantically not the same but I would assume this is good
>>> enough for the common usecase. Plus, if there is an index on
>>> (firstname, lastname) then that could be used.
>>
>> disagree. just one of the ways that could be stymied would to change
>> the function behind the '||' operator.
>
> I don't understand what you mean. Can you please elaborate?
>
> To explain my point a bit: I meant that by querying individual fields
> separately instead of applying a criterion on a function of the two
> the RDBMS has a better chance to use indexes and come up with a better
> plan for this part of the query.
>
> Kind regards
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-performance@postgresql.org
Cc: ljin@tripadvisor.com, shortcutter@googlemail.com, mmoncure@gmail.com
Subject: Re: Performance penalty when using WITH
In-Reply-To: <D64784E1-ACFF-4CC8-93E9-EE86620BBF01@tripadvisor.com>
References: <339959BC-3A27-4269-8A3E-4C157EBABB86@tripadvisor.com> <CAM9pMnNN6t5NhFvnrDuheJUFrj5+-HfW4TE3ggguZN2B8==CBA@mail.gmail.com> <CAHyXU0w+cZcFB+REAKbFcdB_YhU62_375cQdqT=VVE90v5MuRw@mail.gmail.com> <CAM9pMnMmqpMyMBvp8RPocevcYED89tWxL0LfrjYrnzPOOyvQCw@mail.gmail.com> <D64784E1-ACFF-4CC8-93E9-EE86620BBF01@tripadvisor.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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