473,473 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Create 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 2064
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
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
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
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
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
by: Jack Orenstein | last post by:
Suppose I have a table as follows: testdb=> \d person Table "public.person" Column | Type | Modifiers ------------+-------------------------+----------- id |...
4
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
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.