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_CHILDREN 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_CHILDREN
WHERE CHILD_TYPE Like "Z*")
AND
PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_CHILDREN C
ON P.PROJECT_ID = C.PROJECT_ID
WHERE P.PROJECT_ID IS NULL)
); 7 2560
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_CHILDREN 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_CHILDREN WHERE CHILD_TYPE Like "Z*")
AND
PROJECT_ID NOT IN ( SELECT P.PROJECT_ID FROM PROJECTS P LEFT JOIN PROJECTS_CHILDREN 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_CHILDREN 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_CHILDREN 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_CHILDREN 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.
"DFS" <no****@nospam.com> wrote in
news:10*************@corp.supernews.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_CHILDREN 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_CHILDREN WHERE CHILD_TYPE Like "Z*")
AND
PROJECT_ID NOT IN ( SELECT P.PROJECT_ID FROM PROJECTS P LEFT JOIN PROJECTS_CHILDREN 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)
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_CHILDREN 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_CHILDREN 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_CHILDREN C ON P.PROJECT_ID = C.PROJECT_ID WHERE P.PROJECT_ID IS NULL) );
Ok, you have a double whammy speed demon killer.
"Salad" <oi*@vinegar.com> 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_CHILDREN 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_CHILDREN 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_CHILDREN 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.
"Lyle Fairfield" <Mi************@Invalid.Com> wrote in message
news:Xn*******************@130.133.1.4... "DFS" <no****@nospam.com> wrote in news:10*************@corp.supernews.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_CHILDREN 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_CHILDREN WHERE CHILD_TYPE Like "Z*")
AND
PROJECT_ID NOT IN ( SELECT P.PROJECT_ID FROM PROJECTS P LEFT JOIN PROJECTS_CHILDREN 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.
"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_CHILDREN 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_CHILDREN WHERE CHILD_TYPE Like "Z*")
AND
PROJECT_ID NOT IN ( SELECT P.PROJECT_ID FROM PROJECTS P LEFT JOIN PROJECTS_CHILDREN 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_CHILDREN 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_CHILDREN 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_CHILDREN 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.
"DFS" <no****@nospam.com> wrote in
news:10*************@corp.supernews.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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. ...
|
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 ...
|
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...
|
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
...
|
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...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |