Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 0BAF564FD42 for ; Wed, 12 Nov 2008 10:31:30 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 22313-01 for ; Wed, 12 Nov 2008 10:31:26 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail3.eaglepress.com (72-242-202-251.eaglepress.com [72.242.202.251]) by mail.postgresql.org (Postfix) with ESMTP id 3E6A964FCD8 for ; Wed, 12 Nov 2008 10:31:25 -0400 (AST) Received: by mail3.eaglepress.com (Postfix, from userid 60001) id F3AC94E; Wed, 12 Nov 2008 09:32:26 -0500 (EST) Received: from eaglepress.com (challenger.eaglepress.com [10.10.0.18]) by mail3.eaglepress.com (Postfix) with ESMTP id A68D431; Wed, 12 Nov 2008 09:32:26 -0500 (EST) Received: from 10.10.2.48 ([10.10.2.48]) by challenger.EaglePress.local ([10.10.0.18]) with Microsoft Exchange Server HTTP-DAV ; Wed, 12 Nov 2008 14:31:22 +0000 X-MimeOLE: Produced By Microsoft Exchange V6.5 MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-tBmITM3Mt7Vq79iDVfhH" Received: from lin-it1.eaglepress.local by challenger.eaglepress.local; 12 Nov 2008 09:30:47 -0500 Content-class: urn:content-classes:message Subject: Re: function executes sql 100 times longer it should Date: Wed, 12 Nov 2008 09:30:47 -0500 Message-ID: <1226500247.2221.2.camel@lin-it1.eaglepress.local> In-Reply-To: <491AE3F2.601@nsoft.lt> X-MS-Has-Attach: yes X-MS-TNEF-Correlator: Thread-Topic: [ADMIN] function executes sql 100 times longer it should Thread-Index: AclE01WiFNEoyWEdSFikIp1dkLU3BQ== References: <491AE3F2.601@nsoft.lt> From: "Sean Brown" To: "Julius Tuskenis" Cc: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.1 tagged_above=0 required=5 tests=RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 200811/63 X-Sequence-Number: 30717 --=-tBmITM3Mt7Vq79iDVfhH Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Wed, 2008-11-12 at 16:10 +0200, Julius Tuskenis wrote: > Hello, list. >=20 > I have one simple SQL function returning result set that takes around 3=20 > seconds to execute. But if I execute the Select it executes directly -=20 > it takes only around 30 ms. Why so big difference? Probably caching the results. > What should I check? Explain plan might be a good start > I must also say, that this started this afternoon. >=20 > PG: 8.3.3 > OS: Windows Server 2003 >=20 > Example below: > //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D > CREATE OR REPLACE FUNCTION fnk_grupes_prekes(prm_pardavimo_taskas=20 > integer, prm_grupe integer, prm_filtras character varying) > RETURNS SETOF frt_grupes_prekes AS > $BODY$SELECT DISTINCT prek_id, prek_pavadinimas, ks_kaina, mvnt_trumpinys > FROM filter_b_preke_matoma() > LEFT OUTER JOIN b_grupes_prekes ON (gp_preke=3Dprek_id) > LEFT OUTER JOIN b_grupe ON (gp_grupe=3Dgrup_id) > JOIN b_mato_vienetai ON (mvnt_id=3Dprek_matovnt) > JOIN b_kainorascio_sudetis ON (ks_preke =3D prek_id) > JOIN b_kainorascio_grafikas ON (ks_kainorastis=3Dkag_kainorastis) > JOIN b_pardavimo_tasko_kainorastis ON (ks_kainorastis=3Dptk_kainorastis) > WHERE (grup_id=3D$2 OR $2 is Null) > AND ptk_pardavimotaskas=3D$1 > AND ((prek_pavadinimas ILIKE ('%'||$3||'%')) OR $3 is NULL) > AND fnk_grafikas_galioja(kag_grafikas) =3D true > ORDER BY prek_pavadinimas > $BODY$ > LANGUAGE 'sql' VOLATILE > COST 100 > ROWS 1000; > ALTER FUNCTION fnk_grupes_prekes(integer, integer, character varying)=20 > OWNER TO postgres; > GRANT EXECUTE ON FUNCTION fnk_grupes_prekes(integer, integer, character=20 > varying) TO postgres; > GRANT EXECUTE ON FUNCTION fnk_grupes_prekes(integer, integer, character=20 > varying) TO public; >=20 >=20 > select * from fnk_grupes_prekes(18,42,NULL); > Total query runtime: 2172 ms. > 0 rows retrieved. >=20 >=20 > SELECT DISTINCT prek_id, prek_pavadinimas, ks_kaina, mvnt_trumpinys > FROM filter_b_preke_matoma() > LEFT OUTER JOIN b_grupes_prekes ON (gp_preke=3Dprek_id) > LEFT OUTER JOIN b_grupe ON (gp_grupe=3Dgrup_id) > JOIN b_mato_vienetai ON (mvnt_id=3Dprek_matovnt) > JOIN b_kainorascio_sudetis ON (ks_preke =3D prek_id) > JOIN b_kainorascio_grafikas ON (ks_kainorastis=3Dkag_kainorastis) > JOIN b_pardavimo_tasko_kainorastis ON (ks_kainorastis=3Dptk_kainorastis) > WHERE (grup_id=3D42 OR 42 is Null) > AND ptk_pardavimotaskas=3D18 > AND ((prek_pavadinimas ILIKE ('%'||null||'%')) OR null is NULL) > AND fnk_grafikas_galioja(kag_grafikas) =3D true > ORDER BY prek_pavadinimas; >=20 > Total query runtime: 47 ms. > 0 rows retrieved. >=20 First run probably put the plan and results into the query cache. The second run could just pull it from there instead of going to the tables on the disk. > --=20 >=20 > Julius Tuskenis > Programavimo skyriaus vadovas > UAB nSoft > mob. +37068233050 >=20 --=-tBmITM3Mt7Vq79iDVfhH Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQBJGuiXHADH59cE9zIRAmq8AJ9lIy1dJVh/lL3YMYyzVjn/a/cCTwCfY5s9 17db47I2RF7Tcd9QSCe1cJU= =DDX5 -----END PGP SIGNATURE----- --=-tBmITM3Mt7Vq79iDVfhH--