Re: Question on COUNT performance

From: Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>
To: Anders Østergaard Jensen <aj(at)itersys(dot)dk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Question on COUNT performance
Date: 2010-07-14 16:49:14
Message-ID: AANLkTimrKXcpemRDgR5g2NaeVkfsbxHTtV_WrkuSzQT-@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

In retrospect, it's a big assumption whether f_project_acl() or
f_customer_acl() always return TRUE. If they can return FALSE, you
probably want to replace the statements inside the FOR..LOOP with

> IF plan_record.project_id IS NOT NULL THEN
> IF f_project_acl(uid, plan_record.project_id) THEN i := i + 1; END IF;
> ELSEIF plan_record.customer_id IS NOT NULL THEN
> IF f_customer_acl(uid, plan_record.customer_id) THEN i := i + 1; END IF;
> ELSE
> i := i + 1;
> END IF;

This would mimic the results of your original query, although I must
confess I don't understand the usefulness of the count results, as a
number less that the number of rows in plan_events has an ambiguous
meaning. Either

(1) there is a matching event but f_project_acl returned FALSE

OR

(2) there is no matching event, there IS a matching customer, but
f_customer_acl returned FALSE

And of course you don't know which plan_ids these might be true of.

--Lee

2010/7/14 Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>:
> SELECT newfunc(uid);
>
> CREATE FUNCTION newfunc(uid int) RETURNS int AS $$
> DECLARE
>        plan_record record;
>        i int := 0;
> BEGIN
>        FOR plan_record IN SELECT DISTINCT plan_id, project_id, customer_id
> FROM plan_events LEFT JOIN project_plan_events USING (plan_id) LEFT
> JOIN customer_plan_events USING (plan_id)  LOOP
>                IF plan_record.project_id IS NOT NULL THEN
>                        PERFORM f_project_acl(uid, plan_record.project_id);
>                ELSEIF plan_record.customer_id IS NOT NULL THEN
>                        PERFORM f_customer_acl(uid, plan_record.customer_id);
>                END IF;
>                i := i + 1;
>        END LOOP ;
>        RETURN i;
> END;
> $$ LANGUAGE plpgsql;

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jean-David Beyer 2010-07-14 17:05:04 Re: subtract two dates to get the number of days
Previous Message Lee Hachadoorian 2010-07-14 16:21:26 Re: Question on COUNT performance