🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #95

Re: can I do that with one select statement

#95
From: Maxim Shiryaev
Date: Thu, 29 May 2008 14:08
60 lines
2452 bytes
I cannot confirm that it will be faster in all cases.

But _generally_, "sequential scan and in-memory sorting is faster than
several index scans". (C) Someone from FB dev team.
And I can confirm that in most cases it's so.
Especially if your indicies are not very selective.

So it all depends.

Probably the ultimate way would be:

   declare variable v1 integer;
   declare variable v2 integer;

   v1 = 0;
   v2 = 0;

   select field_a, field_b
   from t
   into :v_a, :v_b
 do begin
   if (v_a = 1) then v1 = v1 + v_b;
   else if (v_a = 2) then v2 = v2 + v_b;
   /* and more if's if ther are more distinct values in field_a */
 end

But here you again trade code clarity and "declarativeness" for performance.

Just test it.

But if there are many different values in field_a and you count only sums
for subset of them than several indexed scans would be faster of course.


Maxim.


"Isaac" <no@no.com> ???????/???????? ? ???????? ?????????:
news:483c12b2@newsgroups.borland.com...
> Maxim Shiryaev wrote:
> > Hi.
> >
> > Try:
> >
> > for
> >   select field_a, sum(field_b)
> >   from t
> >   group by field_a
> >   into :v_a, :v_sum_b
> > do begin
> >   if (v_a) = 1 then some_var_1 = v_sum_b;
> >   else if (v_a) = 2 then some_var_2 = v_sum_b;
> >   /* and more if's if ther are more distinct values in field_a */
> > end
> Is this faster than using Two Selects
>
> Select sum(f2) from table_1 where f1 = 1
> Select sum(f2) from table_2 where f1 = 0
>
> Thanks
Technical Details
Message-ID: <483eb8fe$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <483bca8e@newsgroups.borland.com> <483bf82c@newsgroups.borland.com> <483c12b2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,e4861ce108d51b43
X-Google-Attributes: gid114a0e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Maxim Shiryaev" <MaximShiryaev@gmail.com>
Newsgroups: borland.public.interbase.sql
References: <483bca8e@newsgroups.borland.com> <483bf82c@newsgroups.borland.com> <483c12b2@newsgroups.borland.com>
Subject: Re: can I do that with one select statement 
Date: Thu, 29 May 2008 18:08:50 +0400
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 213.234.197.62
Message-ID: <483eb8fe$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1212070142 213.234.197.62 (29 May 2008 07:09:02 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail