X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5F7C2D8055 for ; Mon, 7 Nov 2005 23:01:11 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 84192-09 for ; Tue, 8 Nov 2005 03:01:04 +0000 (GMT) X-Greylist: delayed 01:00:45.562753 by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id B1811D7C60 for ; Mon, 7 Nov 2005 23:01:05 -0400 (AST) Received: from moonunit2.moonview.localnet (wsip-68-15-5-150.sd.sd.cox.net [68.15.5.150]) by svr2.postgresql.org (Postfix) with ESMTP id 02D08F0D2C for ; Tue, 8 Nov 2005 02:00:23 +0000 (GMT) Received: from [192.168.0.3] (moonunit3.moonview.localnet [192.168.0.3]) by moonunit2.moonview.localnet (8.13.1/8.13.1) with ESMTP id jA827ZxT023899 for ; Mon, 7 Nov 2005 18:07:36 -0800 Message-ID: <4370062B.4050202@modgraph-usa.com> Date: Mon, 07 Nov 2005 17:58:03 -0800 From: "Craig A. James" User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Expensive function and the optimizer Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.025 required=5 tests=[AWL=0.025] X-Spam-Score: 0.025 X-Spam-Level: X-Archive-Number: 200511/134 X-Sequence-Number: 15391 I have a function, call it "myfunc()", that is REALLY expensive computationally. Think of it like, "If you call this function, it's going to telephone the Microsoft Help line and wait in their support queue to get the answer." Ok, it's not that bad, but it's so bad that the optimizer should ALWAYS consider it last, no matter what. (Realistically, the function takes 1-2 msec average, so applying it to 40K rows takes 40-80 seconds. It's a graph-theory algorithm, known to be NP-complete.) Is there some way to explain this cost to the optimizer in a permanent way, like when the function is installed? Here's what I get with one critical query (somewhat paraphrased for simplicity): explain analyze select A.ID from A join B ON (A.ID = B.ID) where A.row_num >= 0 and A.row_num <= 43477 and B.ID = 52 and myfunc(A.FOO, 'FooBar') order by row_num; QUERY PLAN ---------------------------------------------------------------------------------- Nested Loop (cost=0.00..72590.13 rows=122 width=8) -> Index Scan using i_a_row_num on a (cost=0.00..10691.35 rows=12222 width=8) Index Cond: ((row_num >= 0) AND (row_num <= 43477)) Filter: myfunc((foo)::text, 'FooBar'::text) -> Index Scan using i_b_id on b (cost=0.00..5.05 rows=1 width=4) Index Cond: ("outer".id = b.id) Filter: (id = 52) Total runtime: 62592.631 ms (8 rows) Notice the "Filter: myfunc(...)" that comes in the first loop. This means it's applying myfunc() to 43477 rows in this example. The second index scan would cut this number down from 43477 rows to about 20 rows, making the query time drop from 62 seconds down to a fraction of a second. Is there any way to give Postgres this information? The only way I've thought of is something like this: select X.id from (select A.id, A.foo, A.row_num from A join B ON (A.id = B.id) where A.row_num >= 0 and A.row_num <= 43477 and B.id = 52) as X where myfunc(X.foo, 'FooBar') order by X.row_num; I can do this, but it means carefully hand-crafting each query rather than writing a more natural query. Thanks, Craig