pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Allan Kelly <allan.kelly@buildstore.co.uk>
To: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Mystery: functions are slow with group by, but...
Date: Tue, 18 Apr 2000 16:16:29 +0100
Message-ID: <38FC7C4D.7D8E1C12@buildstore.co.uk> (raw)

Hi there. I appear to have much quicker performance by asking for more work!

First of all, though: can I see the definition of a function in psql?
\df total_live_a4_properties( text )
just gives me the args, return type etc. Hox can I see the AS '...' clause?

Q2:
I have a big performance problem using a function:

create function total_live_a4_properties( text )
returns int4
as
'select count(*) from properties where
status<>''sold'' and status<>''suspended''
and a4 = $1'
language 'sql';

This takes 5-10 _minutes_ :

select a4, total_live_a4_properties( a4 )
from properties group by a4;

There are 5132 records in the properties table, and 64 distinct a4 values.

Explain gives

NOTICE: QUERY PLAN:

Group ( cost=411.70 rows=5112 width=12)
  -> Sort ( cost=411.70 rows=5112 width=12)
    -> Seq Scan on Properties ( cost=411.70 rows=5112 width=12)

However this next one is < 3 _seconds_!

select a4, count(*), (100*count(*)/total_live_a4_properties( a4 )) as percentage
from properties group by a4;

Explain gives

NOTICE: QUERY PLAN:

Aggregate ( cost=411.70 rows=5112 width=12)
  Group ( cost=411.70 rows=5112 width=12)
    -> Sort ( cost=411.70 rows=5112 width=12)
      -> Seq Scan on Properties ( cost=411.70 rows=5112 width=12)

Can anyone tell me what's going on?
thanks, al.

-- 

 # Allan Kelly                                http://www.plotsearch.co.uk
 # (+44) (0)131 524 8500
 # allan.kelly@buildstore.co.uk...     ..
 # /Software Engineer/i            . .    .    . .
 # ------------------------------   *      . .     .    . .
 # "If you are a Visual Basic programmer,   *       . .     .
 #  these details are none of your business."        *       .  . .
 # Mr Bunny's Guide to Active X, by Carlton Egremont III      *     . .
 # ------------------------------      vi: set noet tw=80 sts=4 ts=8  : .



view thread (2+ messages)  latest in thread

Message-ID: <38FC7C4D.7D8E1C12@buildstore.co.uk>
Permalink:  ../38FC7C4D.7D8E1C12@buildstore.co.uk/
Also on:    postgresql.org/message-id/38FC7C4D.7D8E1C12@buildstore.co.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: allan.kelly@buildstore.co.uk
  Subject: Re: Mystery: functions are slow with group by, but...
  In-Reply-To: <38FC7C4D.7D8E1C12@buildstore.co.uk>
  References: <38FC7C4D.7D8E1C12@buildstore.co.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox