X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 85C4D53801 for ; Mon, 18 Apr 2005 11:19:43 -0300 (ADT) 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 20065-10 for ; Mon, 18 Apr 2005 14:19:33 +0000 (GMT) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.196]) by svr1.postgresql.org (Postfix) with ESMTP id 9D61F53762 for ; Mon, 18 Apr 2005 11:19:32 -0300 (ADT) Received: by zproxy.gmail.com with SMTP id 34so1597613nzf for ; Mon, 18 Apr 2005 07:19:37 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=q6y8Kw1FF8rjEiA/D5eGLfn9NSnHGeyfy2BcrXByHk4hxyzeu8axcdxP6gUFdDbSixk+0QfEIehGf8/vL7SxZt6yNOm3xlNCVTgYAitUPkkq7g+SLJgufrPeHdu9b4icQuw0DzE/QtKw+9WrWVJW2jhYt7OuXPyDZLZX6Wi8wI4= Received: by 10.36.12.3 with SMTP id 3mr372273nzl; Mon, 18 Apr 2005 07:19:37 -0700 (PDT) Received: by 10.36.22.15 with HTTP; Mon, 18 Apr 2005 07:19:37 -0700 (PDT) Message-ID: <758d5e7f05041807194a78b55a@mail.gmail.com> Date: Mon, 18 Apr 2005 16:19:37 +0200 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: pgsql-performance@postgresql.org Subject: Re: immutable functions vs. join for lookups ? In-Reply-To: <6EE64EF3AB31D5448D0007DD34EEB3415C2596@Herge.rcsinc.local> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <6EE64EF3AB31D5448D0007DD34EEB3415C2596@Herge.rcsinc.local> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.758 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200504/396 X-Sequence-Number: 11793 On 4/18/05, Merlin Moncure wrote: > > d) self-join with a function ;) > > EXPLAIN ANALYZE SELECT * FROM (SELECT n, id2username(n) AS username > > FROM (SELECT DISTINCT n FROM aaa) AS values) AS v_lookup RIGHT JOIN > > aaa USING (n); >=20 > That's pretty clever. > It sure seems like the server was not caching the results of the > function...maybe the server thought it was to small a table to bother? Nah, I don't thinks so. Having around 2 097 152 rows of 1s and 0s takes 48 seconds for id2username() query. The "self join" you've quoted above takes 32 seconds. SELECT n FROM aaa; takes 7 seconds. Thinking further... SELECT CASE n WHEN 0 THEN 'foo' WHEN 1 THEN 'bar' END FROM aaa; takes 9 seconds. CREATE OR REPLACE FUNCTION id2un_case(oid int) RETURNS text AS $$ BEGIN RETURN CASE oid WHEN 0 THEN 'foo' WHEN 1 THEN 'bar' END; END; $$ LANGUAGE plpgsql IMMUTABLE; SELECT id2un_case(n) FROM aaa; ...takes 36 seconds ...and to see how it depends on flags used: SELECT count(id2un_case(n)) FROM aaa; ...id2un_case(n) IMMUTABLE takes 29900,114 ms ...id2un_case(n) IMMUTABLE STRICT takes 30187,958 ms ...id2un_case(n) STABLE takes 31457,560 ms ...id2un_case(n) takes 33545,178 ms ...id2un_case(n) VOLATILE takes 35150,920 ms (and a count(CASE n WHEN ... END) FROM aaa takes: 2564,188 ms I understand that these measurements are not too accurate. They were done on idle system, and the queries were run couple of times (to make sure they're cached :)). I believe either something is minor performance difference between IMMUTABLE STABLE and even VOLATILE plpgsql... :( Oh, and doing things like "ORDER BY n" or "WHERE n =3D 1" didn't help either... I still wonder whether it's only my case or is there really something wrong with these functions? Regards, Dawid