473,657 Members | 2,524 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 2083
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
2954
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
4792
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. Obviously not workable! I know where the problem is, I just don't know how to fix it. The query calls a function, and I assume it gets slow because the function runs on every record. So--is there a way to rewrite the function so it's quicker?...
14
2458
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 far with a loop in a form. Basically, I have key sums Current savings Current Salary Current deposit amount
15
2618
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 = PROBLEM_DE.PROBLEMNR WHERE (((STATUSHISTORIE.STATUSDATUM)<#1/1/2005#) AND ((PROBLEM_DE.DATENBEREICH)='SPMO') AND (((Left((.),InStr(.,"-")-2)))='K29')
6
2036
by: Jack Orenstein | last post by:
Suppose I have a table as follows: testdb=> \d person Table "public.person" Column | Type | Modifiers ------------+-------------------------+----------- id | integer | not null age | integer | other_info | character varying(1000) | Indexes: person_pkey primary key btree (id),
4
2077
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 DATETIME -- non-clustered index VARCHAR(10) VARCHAR(10) INT etc..
1
4559
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 financial deal match, and obviously it takes ages. Can someone tell or explain me if I could use a Query or Macro for be able to compare each table with the rest of them and extract just the data which doesn't match with what I'm looking for.
3
4155
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 characteristics. Each patient record was repeated for each visit. I only want a table of unique records for each patient with their characteristics, simple enough with select distinct, but I found that I obtained duplicate records if the patient visited...
3
7258
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 curious about is the query, (if it works at all) takes ages to complete. I suppose 300,000 records is alot to process and I will sometimes want to change this boolean value on up to 20,000 of those, but I have had times of over an hour and it is still...
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.