473,511 Members | 10,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query takes ages to run...

Hi All,

I have a Table1 with ID, FirstName, Surname, Address1, Address2, PostCode,
Suburb, Purchase.
The table list all purchases made by all customers.
A customer can have multiple purchases and there will be multiple records
for the same customer. The address details might not be the same (for
example Suburb might be W Gosford in one record and West Gosford in another
record)
I want to build a query which shows all the customers from the table, only
once. A customer can be considered unique if he has distinct Surname and
Address1.

I need the following fields: ID, FirstName, Surname, Address1, Address2,
Postcode, Suburb

The query is required for mailing purposes, a customer should receive only
one letter, no matter of how many purchases he made.

I created this query, which works fine for tables with a few thousand
records. But in the original table, with over 200,000 records, the query
seems to take a long time (runs longer than 30 minutes and actually I've
never seen the finished result). On a sample table with about 20,000 records
it took over 5 minutes to run.
The query actually displays records, but it doesn't show the maximum number
of records until finishes.

SELECT a.*
FROM Table1 AS a
WHERE a.ID =
(
SELECT min(b.ID)
FROM Table1 AS b
WHERE b.Address1 = a.Address1 AND b.FirstName = a.FirstName
);

Is there a better performing query than this one? I am aware that the design
of the database is not good at all, but it isn't in my power to do changes
in it (at least not now).
Regards,
Nicolae


Nov 13 '05 #1
1 2069
Nicolae Fieraru wrote:
Hi All,

I have a Table1 with ID, FirstName, Surname, Address1, Address2, PostCode,
Suburb, Purchase.
The table list all purchases made by all customers.
A customer can have multiple purchases and there will be multiple records
for the same customer. The address details might not be the same (for
example Suburb might be W Gosford in one record and West Gosford in another
record)
I want to build a query which shows all the customers from the table, only
once. A customer can be considered unique if he has distinct Surname and
Address1.

I need the following fields: ID, FirstName, Surname, Address1, Address2,
Postcode, Suburb

The query is required for mailing purposes, a customer should receive only
one letter, no matter of how many purchases he made.

I created this query, which works fine for tables with a few thousand
records. But in the original table, with over 200,000 records, the query
seems to take a long time (runs longer than 30 minutes and actually I've
never seen the finished result). On a sample table with about 20,000 records
it took over 5 minutes to run.
The query actually displays records, but it doesn't show the maximum number
of records until finishes.

SELECT a.*
FROM Table1 AS a
WHERE a.ID =
(
SELECT min(b.ID)
FROM Table1 AS b
WHERE b.Address1 = a.Address1 AND b.FirstName = a.FirstName
);

Is there a better performing query than this one? I am aware that the design
of the database is not good at all, but it isn't in my power to do changes
in it (at least not now).
Regards,
Nicolae


Subqueries are nortoriously slow. If your Where was
WHere ID IN ("1","2","3")
it would be extrememly fast.

My suggestion is to create a second query. Select the ID, firstname.
and Address and group on them as a totals query.

Next, link that query to your current query and get rid of the subquery.

Run your current query to this new query method and verify the record
count is the same.

You might want to create an index on the first name and Address1 prior
to running the code to speed things up even more.

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2946
by: Matthias Haffke | last post by:
Ok, this is a tricky question for the pro's: My access sheet: line, id a, id b, val% ---------------- 1, a, ac, 0.04 2, a, ac, 0.28 3, a, ac, 0.015 4, a, ac, 0.205
3
4779
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
14
2445
by: Crimsonwingz | last post by:
Need to calculate a sum based on a number of factors over a period of years. I can use formula ^x for some of it, but need totals to carry over in the sum and have only been able to do this thus...
15
2605
by: Jean | last post by:
Hello, I have the following query that I set up as a test, and it runs fine: SELECT STATUSHISTORIE.* FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON STATUSHISTORIE.PROBLEM_ID =...
6
2028
by: Jack Orenstein | last post by:
Suppose I have a table as follows: testdb=> \d person Table "public.person" Column | Type | Modifiers ------------+-------------------------+----------- id |...
4
2070
by: JayCallas | last post by:
I have a SQL 2000 table containing 2 million rows of Trade data. Here are some of the columns: INT IDENTITY(1,1) -- PK, non-clustered DATETIME -- clustered index ...
1
4548
by: The Rookie | last post by:
Hi everybody, The rookie is back... Ok I'm working actually on a lot of differents financial excel tables (some with calculation'macro) and most of the time I need to check manually if every...
3
4147
by: johnds | last post by:
Another basic question from newbie. I have a 10 million record database of patient contact with their physician. The original file contained all thier clinical information and personal...
3
7246
by: Shal | last post by:
Hi all, I have a table with 300,000 records in it and I want to run an update that has a few clauses in the WHERE command and just changes one boolean value on a subset of records. What I am...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.