Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 8D6C8650060 for ; Mon, 21 Jul 2008 10:49:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 19829-03 for ; Mon, 21 Jul 2008 10:49:19 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.61.230.242]) by postgresql.org (Postfix) with ESMTP id 1C5B4650038 for ; Mon, 21 Jul 2008 10:49:23 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id C7CAA5C0EA; Mon, 21 Jul 2008 21:49:17 +0800 (WST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id A2AFE1E0435; Mon, 21 Jul 2008 21:49:17 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id zu6kAF-OGnn0; Mon, 21 Jul 2008 21:49:16 +0800 (WST) Received: from [192.168.44.95] (203.161.97.213.static.amnet.net.au [203.161.97.213]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTP id 4146E1E0434; Mon, 21 Jul 2008 21:49:16 +0800 (WST) Message-ID: <488493DE.4000806@postnewspapers.com.au> Date: Mon, 21 Jul 2008 21:49:18 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.14 (X11/20080505) MIME-Version: 1.0 To: valiouk@yahoo.co.uk Cc: psql performance Subject: Re: Perl/DBI vs Native References: <768129.60859.qm@web25802.mail.ukl.yahoo.com> In-Reply-To: <768129.60859.qm@web25802.mail.ukl.yahoo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200807/178 X-Sequence-Number: 30778 Valentin Bogdanov wrote: > Hi, > > I have ran quite a few tests comparing how long a query takes to execute from Perl/DBI as compared to psql/pqlib. No matter how many times I run the test the results were always the same. > > I run a SELECT all on a fairly big table and enabled the log_min_duration_statement option. With psql postgres consistently logs half a second while the exact same query executed with Perl/DBI takes again consistently 2 seconds. > > If I were timing the applications I would have been too much surprised by these results, obviously, processing with Perl would be slower than a native application. But it's the postmaster that gives these results. Could it be because the DBI module is slower at assimilating the data? > > Any light on the subject would be greatly appreciated. Random guess: Perl's DBI is using parameterized prepared statements, preventing the optimizer from using its knowledge about common values in the table to decide whether or not index use is appropriate. When you're writing the query in psql, you're not using prepared statements so the optimizer can be cleverer. Try comparing: SELECT statement to PREPARE test(params) AS statement; EXECUTE test(params); eg: SELECT x + 44 FROM t; vs: PREPARE test(int) AS x + $1 FROM t; EXECUTE test(44); Use EXPLAIN ANALYZE to better understand the changes in the query plan. -- Craig Ringer