473,699 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Slow query

DFS
This UNION query is very slow. With only 3,000 records in the Parent table
and 7,000 records in the Child table, it takes about 60 seconds to run and
returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.
-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL

UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)
);


Nov 12 '05 #1
7 2590
On Mon, 2 Feb 2004 19:20:01 -0500, DFS wrote:
This UNION query is very slow. With only 3,000 records in the Parent table
and 7,000 records in the Child table, it takes about 60 seconds to run and
returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.

-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL
This doesn't match your explanation. As I understand:
WHERE C.PROJECT_ID IS NULL
UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)
Same, see above.
);


1. There is no link between main and sub-queries in the 2nd part. Therefore
the sub-queries will run on the entire table for each entry of the
main-query.

2. There seems to be a problem with the combined sub-queries in the Not In
clause of the 2nd query. With split clauses you should have reasonable
results:

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE C.PROJECT_ID IS NULL

UNION

SELECT P.PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS P
WHERE P.PROJECT_ID NOT IN

(SELECT C.PROJECT_ID
FROM PROJECTS_CHILDR EN C
WHERE C.PROJECT_ID = P.PROJECT_ID
AND C.CHILD_TYPE Like "Z*")

AND P.PROJECT_ID NOT IN

(SELECT Q.PROJECT_ID
FROM PROJECTS Q LEFT JOIN PROJECTS_CHILDR EN C
ON Q.PROJECT_ID = C.PROJECT_ID
WHERE Q.PROJECT_ID=P. PROJECT_ID
AND C.PROJECT_ID IS NULL);

HTH - Peter

--
No mails please.
Nov 12 '05 #2
"DFS" <no****@nospam. com> wrote in
news:10******** *****@corp.supe rnews.com:
This UNION query is very slow. With only 3,000 records in the Parent
table and 7,000 records in the Child table, it takes about 60 seconds to
run and returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.
-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL

UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)


"NOT IN" is a pig. TRY a Left Join and WHERE ISNULL(The Child Table Linked
Field)

--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #3
DFS wrote:
This UNION query is very slow. With only 3,000 records in the Parent table
and 7,000 records in the Child table, it takes about 60 seconds to run and
returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.

-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL
I was thinking C.ProjectID is null. Oh weel.
UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")
That's your speed demon killer. The In (Select....) methd, from all of my
experiences, is useful for coffee breaks.

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)
);


Ok, you have a double whammy speed demon killer.

Nov 12 '05 #4
DFS
"Salad" <oi*@vinegar.co m> wrote in message
news:40******** *******@vinegar .com...
DFS wrote:
This UNION query is very slow. With only 3,000 records in the Parent table and 7,000 records in the Child table, it takes about 60 seconds to run and returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.

-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL


I was thinking C.ProjectID is null. Oh weel.


Yes, you're right - I cut and pasted and changed a few things before I
posted.
UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")


That's your speed demon killer. The In (Select....) methd, from all of my
experiences, is useful for coffee breaks.


This is the slow part. All other parts of the query run in 1 second or
less.

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)
);


Ok, you have a double whammy speed demon killer.

This part runs fast, about 1 second.

Thanks.
Nov 12 '05 #5
DFS

"Lyle Fairfield" <Mi************ @Invalid.Com> wrote in message
news:Xn******** ***********@130 .133.1.4...
"DFS" <no****@nospam. com> wrote in
news:10******** *****@corp.supe rnews.com:
This UNION query is very slow. With only 3,000 records in the Parent
table and 7,000 records in the Child table, it takes about 60 seconds to
run and returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.
-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL

UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)


"NOT IN" is a pig. TRY a Left Join and WHERE ISNULL(The Child Table Linked
Field)


Yes, the "NOT IN" section is the only slow part - actually, the first NOT IN
is slow, but the last one runs quickly. I'll try a LEFT JOIN for the first
section as well.

Danke.


Nov 12 '05 #6
DFS

"Peter Doering" <no****@doering .org> wrote in message
news:bv******** ****@ID-204768.news.uni-berlin.de...
On Mon, 2 Feb 2004 19:20:01 -0500, DFS wrote:
This UNION query is very slow. With only 3,000 records in the Parent table and 7,000 records in the Child table, it takes about 60 seconds to run and returns about 2200 records.

Access 97. All appropriate indexes and keys enforced.

Any ideas on speeding it up? Thanks.

-- PART 1: HAS NO CHILD RECORDS

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL
This doesn't match your explanation. As I understand:
WHERE C.PROJECT_ID IS NULL


True. I cut and pasted and made some changes to the post manually, and
mistyped. The actual query uses C.PROJECT_ID IS NULL.

UNION

-- PART 2: HAS CHILD RECORDS, BUT NOT OF TYPE Z

SELECT PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS
WHERE PROJECT_ID NOT IN

(
(SELECT PROJECT_ID
FROM PROJECTS_CHILDR EN
WHERE CHILD_TYPE Like "Z*")

AND

PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)


Same, see above.
);


1. There is no link between main and sub-queries in the 2nd part.

Therefore the sub-queries will run on the entire table for each entry of the
main-query.

2. There seems to be a problem with the combined sub-queries in the Not In
clause of the 2nd query. With split clauses you should have reasonable
results:

SELECT P.PROJECT_ID, 'No Child Data' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDR EN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE C.PROJECT_ID IS NULL

UNION

SELECT P.PROJECT_ID, 'Child Data, Not type Z' AS SUB_TYPE
FROM PROJECTS P
WHERE P.PROJECT_ID NOT IN

(SELECT C.PROJECT_ID
FROM PROJECTS_CHILDR EN C
WHERE C.PROJECT_ID = P.PROJECT_ID
AND C.CHILD_TYPE Like "Z*")

AND P.PROJECT_ID NOT IN

(SELECT Q.PROJECT_ID
FROM PROJECTS Q LEFT JOIN PROJECTS_CHILDR EN C
ON Q.PROJECT_ID = C.PROJECT_ID
WHERE Q.PROJECT_ID=P. PROJECT_ID
AND C.PROJECT_ID IS NULL);

HTH - Peter

Pete,

That didn't work. It tells me I have 'Child Data, not type Z' when there
are no child records.

But, if I change the WHERE clause in the last section of your query to read:
WHERE C.PROJECT_ID IS NULL
it returns the correct data in about one second, like I knew it should.

Thanks.


Nov 12 '05 #7
"DFS" <no****@nospam. com> wrote in
news:10******** *****@corp.supe rnews.com:
UNION


UNION ALL?

Keep in mind that UNION removes duplicates, while UNION ALL doesn't
check. If your component queries won't produce duplicates, UNION ALL
will give you some performance benefit.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #8

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

Similar topics

5
3139
by: Shay | last post by:
essentially I am trying to do some counts based on some assumptions in the recordset. So I get the RS back, put the values into a variable, move to the next record in the RS and compare what is in the variable to the value in the next record in the recordset and do a count. Then overwrite the value in the variables and do the same for the next record and so. But this runs extremly slow. 5000 records takes about 10 minutes in IE6 and I...
2
5346
by: Tim Fountain | last post by:
We've recently enabled slow query logging on a server and it's proving interesting seeing which queries are bogging things down. This one is puzzling me a little: SELECT articleid, type, authorid, authorname, text, posted FROM comments WHERE status = 'normal' ORDER BY posted DESC LIMIT 5; The purpose of this query is to list the five most recent (non-deleted) comments. Here is the table structure:
1
1649
by: Thomas Bartkus | last post by:
Is it possible that the elapsed time being measured includes waiting for the client to acknowledge that it has received all the data? In *addition* to the server execution time? Documentation seems to *imply* that the slow query log only looks at server execution time. But, it doesn't acknowledge this directly and there seems to be a distinct connection between slow network pipes and slow queries. For example - even the simplest...
2
6233
by: AG | last post by:
I am able to run a query which runs FAst in QA but slow in the application.It takes about 16 m in QA but 1000 ms on the Application.What I wanted to know is why would the query take a long time in the application when it runs fast on SQL server? How should we try debugging it? Ajay
2
2012
by: Yonatan Goraly | last post by:
I am in the process of adding PostgreSQL support for an application, in addition to Oracle and MS SQL. I am using PostgreSQL version 7.3.2, Red Hat 9.0 on Intel Pentium III board. I have a query that generally looks like this: SELECT t1.col1, t2.col1 FROM t1, t2 WHERE t1.x=t2.y AND t2.p='string' AND t2.q=1 This query is strikingly slow (about 100 sec when both t1 and t2 has
3
4795
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?...
3
2883
by: Jennyfer J Barco | last post by:
In my application I have a datagrid. The code calls a Stored procedure and brings like 200 records. I created a dataset and then a dataview to bind the results of the query to my grid using MyGrid.DataBind() Once the records are loaded, to handle the next, previous button is too slow. I have in the same screen OptionsBox and everytime I click in any option I show some text fields in the screen. Anything the user does is very slow. When...
0
2668
by: Dave Hammond | last post by:
Hi All, I'm trying to use the slow-query-log (with --log-queries-not-using-indexes enabled) to determine if any queries need optimization, and have a few questions about some entries I'm seeing. First question: given a table defined as: CREATE TABLE `oa_location` (
9
19151
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ------------------------- SELECT * FROM ( Select x.event_date From x FULL OUTER JOIN y ON x.event_date = y.event_date
2
9839
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes to open in datasheet view. As an experiment, I deleted all rows in all tables; after that, the query took only seconds to open in both design view and datasheet view. From these facts, I conclude that Access is evaluating the query when I go to...
0
8686
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
9173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9033
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...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
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 we have to send another system
2
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
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.