🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: query runs slow with IN and GROUP BY HAVING

#11257
From: Fausto Brador
Date: Sun, 01 Dec 2002 05:15
47 lines
2374 bytes
Hi Amita!
I think your problem is the order of the fields in your detail primary key.
It should be mast_key, Det_Key, ....etc.
If you get it. This should work fine for you:
Select *
  From master_table a
 Where 2000< (Select Sum(amount)
                        From detail_table b
                       Where b.mast_key =a.mast_key
                           And b.col_date between :fromdate and :todate)

Bye
FBrador

"amita" <amita@agnisoft.com> wrote in message
news:3de6f872@newsgroups.borland.com...
> 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
>
> --
> Amita
> Agni Software Pvt Ltd
> Bangalore, India
>
>
Technical Details
Message-ID: <3de99ae4@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail
References: <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-30 21:15:52 PST
From: "Fausto Brador" <fbrador@hotmail.com>
Newsgroups: borland.public.interbase.sql
References: <3de6f872@newsgroups.borland.com>
Subject: Re: query runs slow with IN and GROUP BY HAVING
Date: Sun, 1 Dec 2002 01:15:22 -0400
Lines: 49
Organization: None
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2720.3000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
NNTP-Posting-Host: 64.32.112.113
Message-ID: <3de99ae4@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1038719716 64.32.112.113 (30 Nov 2002 21:15:16 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail