Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id F0A036333DE for ; Thu, 4 Feb 2010 02:35:28 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 81981-02 for ; Thu, 4 Feb 2010 06:35:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f201.google.com (mail-pz0-f201.google.com [209.85.222.201]) by mail.postgresql.org (Postfix) with ESMTP id 6058C633085 for ; Thu, 4 Feb 2010 02:35:18 -0400 (AST) Received: by pzk39 with SMTP id 39so2305108pzk.15 for ; Wed, 03 Feb 2010 22:35:17 -0800 (PST) MIME-Version: 1.0 Received: by 10.142.152.15 with SMTP id z15mr469968wfd.98.1265265006168; Wed, 03 Feb 2010 22:30:06 -0800 (PST) In-Reply-To: <4B67F908.1060507@gmail.com> References: <4B66B7E0.3020605@gmail.com> <4B67F908.1060507@gmail.com> Date: Thu, 4 Feb 2010 01:30:06 -0500 Message-ID: Subject: Re: Slow query: table iteration (8.3) From: Glenn Maynard To: Yeb Havinga Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201002/75 X-Sequence-Number: 37496 On Tue, Feb 2, 2010 at 5:06 AM, Yeb Havinga wrote: > I believe it does for (re) binding of parameter values to prepared > statements, but not in the case of an sql function. To test an idea, ther= e > might be a workaround where you could write a pl/pgsql function that make= s a > string with the query and actual parameter values and executes that new > query everytime. It's not as pretty as a sql function, but would give an > idea of how fast things would run with each loop replanned. Another idea = is That, or just have the code generate a function on the fly, and then delete it. For example: CREATE FUNCTION tmp_highscores_for_steps_and_card_PID(steps_id int) RETURNS SETOF INTEGER LANGUAGE SQL AS $$ SELECT r.id FROM stomp_round r WHERE ($1 IS NULL OR r.steps_id =3D $1) AND r.user_card_id =3D 591 ORDER BY r.score DESC LIMIT 1 $$; SELECT tmp_highscores_for_steps_and_card_PID(s.id) FROM stomp_steps s; DROP FUNCTION tmp_highscores_for_steps_and_card_PID(int); An ugly hack, but it'd unblock things, at least. (Or, I hope so. I do have other variants of this, for things like "high scores in your country", "your 5 most recent high scores", etc. That's why I'm doing this dynamically like this, and not just caching high scores in another table.) > With indeed is not a solution because the with query is executed once, so= it > cannot take a parameter. What about a window function on a join of > stomp_steps and stomp_round with partition by on =C2=A0steps_id and user_= card is > and order by score and with row_number() < your third parameter. From the > docs I read that window functions cannot be part of the where clause: an > extra subselect leven is needed then to filter the correct row numbers. Someone suggested window functions for this back when I was designing it, and I looked at them. I recall it being very slow, always doing a seq scan, and it seemed like this wasn't quite what windowing was designed for... --=20 Glenn Maynard