Thread: Query Cost

Query Cost

From
ShepherdHill DB Subscriptions
Date:
Hi,

I have a table with this schema:

CREATE TABLE billing.bill
(
  sno serial NOT NULL,
  billno int4,
  det date NOT NULL,
 .
 .
 .
  CONSTRAINT bill_pkey PRIMARY KEY (sno)
)

I want to execute a query that will not return any record. Which of
these queries is cheaper please?

1.   Select * from billing.bill where 1=0
2.   Select * from billing.bill where sno=0

Thanks for your assistance.

Chris.

Re: Query Cost

From
"Jim C. Nasby"
Date:
On Sun, Sep 18, 2005 at 07:40:26PM +0100, ShepherdHill DB Subscriptions wrote:
> Hi,
>
> I have a table with this schema:
>
> CREATE TABLE billing.bill
> (
>   sno serial NOT NULL,
>   billno int4,
>   det date NOT NULL,
>  .
>  .
>  .
>   CONSTRAINT bill_pkey PRIMARY KEY (sno)
> )
>
> I want to execute a query that will not return any record. Which of
> these queries is cheaper please?
>
> 1.   Select * from billing.bill where 1=0
> 2.   Select * from billing.bill where sno=0

Do an explain analyze and see. :)

1 will be faster because the optimizer can discard it right off the bat.
--
Jim C. Nasby, Sr. Engineering Consultant      jnasby@pervasive.com
Pervasive Software      http://pervasive.com    work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf       cell: 512-569-9461

plperl function to return nulls

From
Brent Wood
Date:

I can't find a reference in the docs for this...

I have a plperl function returning an int. The int is returned as the
result of a system call.

It is set to return a null if one of the inputs is null, but I can't see
how to return a null if the result is indeterminate. The function
currently returns a 0 instead.

How do I stick an if in the function to return a null where appropriate?


Thanks,

  Brent Wood

Re: plperl function to return nulls

From
Michael Fuhr
Date:
On Mon, Sep 19, 2005 at 10:52:23AM +1200, Brent Wood wrote:
> I have a plperl function returning an int. The int is returned as the
> result of a system call.
>
> It is set to return a null if one of the inputs is null, but I can't see
> how to return a null if the result is indeterminate. The function
> currently returns a 0 instead.
>
> How do I stick an if in the function to return a null where appropriate?

From the PL/Perl documentation:

"As shown above, to return an SQL null value from a PL/Perl function,
return an undefined value."

http://www.postgresql.org/docs/8.0/interactive/plperl.html

Is that what you're looking for?

--
Michael Fuhr