Re: Better performance possible for a pathological query?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexis Lê-Quôc <alq(at)datadoghq(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Better performance possible for a pathological query?
Date: 2013-08-07 16:07:37
Message-ID: 6581.1375891657@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

=?UTF-8?B?QWxleGlzIEzDqi1RdcO0Yw==?= <alq(at)datadoghq(dot)com> writes:
> The query itself is very simple: a primary key lookup on a 1.5x10^7 rows.
> The issue is that we are looking up over 11,000 primary keys at once,
> causing the db to consume a lot of CPU.

It looks like most of the runtime is probably going into checking the
c.key = ANY (ARRAY[...]) construct. PG isn't especially smart about that
if it fails to optimize the construct into an index operation --- I think
it's just searching the array linearly for each row meeting the other
restrictions on c.

You could try writing the test like this:
c.key = ANY (VALUES (1), (17), (42), ...)
to see if the sub-select code path gives better results than the array
code path. In a quick check it looked like this might produce a hash
join, which seemed promising anyway.

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Alexis Lê-Quôc 2013-08-07 16:28:45 Re: Better performance possible for a pathological query?
Previous Message Tom Lane 2013-08-07 15:53:14 Re: RE: [PERFORM] Re: [PERFORM] Sub-optimal plan for a paginated query on a view with another view inside of it.