🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

how can I speed up this query

#10987
From: Marcos Pegoraro
Date: Sat, 21 Dec 2002 00:52
42 lines
2502 bytes
I have one table which has 90.000 records and it has all accounts to receive
Each record has a Contract_ID and the same Contract can have up to 20
Accounts to receive
But, I want to show only the first record of each Contract which was not
paid yet or if it was paid between two dates.

Select P.Name R.* from Receive R inner join Customer P on
R.Customer_ID = P.Customer_ID where R.Receive_ID = (select min(Receive_ID)
from Receive
where (PayDate is Null or PayDate between '12/01/02' and '12/31/02') and
Contract_ID=R.Contract_ID group by Contract_ID ) and R.DueDate between
'12/01/02' and '12/31/02'

Example:
ID Contract_ID DueDate PayDate
1    1                 09/01/02 09/01/02
2    1                 10/01/02
3    1                 11/01/02
4    2                 10/01/02 10/01/02
5    2                 11/01/02
6    3                 10/01/02
7    3                 11/01/02
8    4                 12/01/02 12/01/02
9    4                 01/01/03
ID is Primary Key
Contract_ID is Foreign Key
DueDate is indexed
PayDate is indexed too.

The result I want and it results is:
2    1                 10/01/02
5    2                 11/01/02
6    3                 10/01/02
8    4                 12/01/02 12/01/02

So, the result is OK but my problem is the speed.
It takes up to 11 seconds to return using IBConsole.
and, if I use:
select * from Receive where DueDate between '12/01/02' and '12/31/02'
It takes just 0.006 Seconds to return

Then, What do need I do to run that first query faster, like this last one ?
Technical Details
Message-ID: <3e03bb4d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!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

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114a0e,bdd368e2d37d69d2,start
X-Google-Attributes: gid114a0e,public
X-Google-ArrivalTime: 2002-12-20 16:58:28 PST
From: "Marcos Pegoraro" <mpegoraro@brturbo.com>
Newsgroups: borland.public.interbase.sql
Subject: how can I speed up this query
Date: Fri, 20 Dec 2002 22:52:34 -0200
Lines: 44
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: 200.181.182.51
Message-ID: <3e03bb4d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1040431949 200.181.182.51 (20 Dec 2002 16:52:29 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!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