Article View: borland.public.interbase.sql
Article #11248Re: query runs slow with IN and GROUP BY HAVING
From: Bojidar Alexandrov
Date: Fri, 29 Nov 2002 07:23
Date: Fri, 29 Nov 2002 07:23
63 lines
3113 bytes
3113 bytes
amita wrote:
> I have a master and detail table
> as
> Master_Table -> Mast_Key....
> Detail_Table -> Det_Key, Mast_key , Col_Date , Amount.....
>
> Now I have a query get the rows of the master
> where the Date lies between given dates and the total amount
> collected during that period exceeds say 2000
>
> so i have the query as
>
> select * from Master_Table inner join Detail_Table
> on(Master_Table.Mast_Key=Detail_Table.Mast_Key)
> where Col_Date between :FromDate and :ToDate
> and Mast_Key in
> (select Mast_key from Detail_Table
> where Col_Date between :FromDate and :ToDate
> group by Mast_Key having sum(Amount)>2000)
>
> Now the problem is that both Master_Table and
> Detail table have millions of records this
> query runs very slow
> I have indexes
> on Mast_Key, Det_key and Amount
Someone can help only if you say what is your server, post your methadata
definitions and plan that you receive from the server for this query and
what you expect as results.
On such large table you must ensure that everything is going by indexes.
Index on Amount is useless, is is used as Sum only.
Index on Col_Date is needed.
That I have understand from your query:
This is all Mast_keys where in given period sum is more than 2000
(select Mast_key from Detail_Table
where Col_Date between :FromDate and :ToDate
group by Mast_Key having sum(Amount)>2000)
and what you do then - why is this here also - Col_Date between :FromDate
and :ToDate ?
Is Col_Date exists in Master_Table also or this is from detail?
select *
from Master_Table
inner join Detail_Table on (Master_Table.Mast_Key=Detail_Table.Mast_Key)
where Col_Date between :FromDate and :ToDate
and Mast_Key in
(select Mast_key from Detail_Table
where Col_Date between :FromDate and :ToDate
group by Mast_Key having sum(Amount)>2000)
Something here I don't undertand... Please explain again what you expect as
result.
Also may be you can try to separate the qubquery in view and then join...
--
Bojidar Alexanrov
Kodar Ltd.
http://www.Kodar.net
Technical Details
Message-ID:
<3de71604@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!nntp2.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3de6f872@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114a0e,ba43de26dfc40e54 X-Google-Attributes: gid114a0e,public X-Google-ArrivalTime: 2002-11-28 23:32:16 PST From: "Bojidar Alexandrov" <bojo@kodar.net> Newsgroups: borland.public.interbase.sql References: <3de6f872@newsgroups.borland.com> Subject: Re: query runs slow with IN and GROUP BY HAVING Date: Fri, 29 Nov 2002 09:23:47 +0200 Lines: 65 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4920.2300 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4920.2300 NNTP-Posting-Host: 213.169.35.65 Message-ID: <3de71604@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1038554628 213.169.35.65 (28 Nov 2002 23:23:48 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!nntp2.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail