473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

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)
);


Jul 20 '05 #1
4 5765
"DFS" <no****@nospam.com> wrote in message 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.

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)
);


Without table DDL and sample data, this is a guess, but it looks like
you could simplify the query to something along these lines:

select
p.project_id,
case when c.child_type is null then 'No Child Data' else 'Child Data,
Not type Z' end as sub_type
from
projects p
left outer join projects_children c
on p.project_id = c.project_id
where
c.child_type not like 'Z*'

If this isn't correct, please consider posting the DDL and sample
data.

Simon
Jul 20 '05 #2
Hi, looks like you have 'not exists ' there, which results in table scans,
and essentially cartesian product twice for both 'not exists' in your lower
UNION.

Usually with some indexes you should be able to help query like that, but in
your case WHERE clauses 'not null' and 'CHILD_TYPE Like "Z*"' don't look
like good candidates for index, due to low selectivity.

But, you could try use #table or @table to limit your I/O cost.

That is , take you first sub-select

SELECT PROJECT_ID
FROM PROJECTS_CHILDREN
WHERE CHILD_TYPE Like "Z*"

and put it into temp table @t1.

take second,
SELECT P.PROJECT_ID
FROM PROJECTS P
WHERE P.PROJECT_ID IS NULL

in put it into second table @t2

now , instead of having not exists agaist PROJECTS_CHILDREN and PROJECTS ,
use @t1 and @t2 tables.

Make sure you have index on PROJECTS (PROJECT_ID).

It's good habit to test something like that in Query Analyzer with 'set
statistics io on'. This tells you number of I/O you are doing. What you are
trying to achieve is , lower number of I/O. So basically, look at the total
I/O and make sure this number is decreasing as you are making your sql
changes.

Thing to remember though, whatever query you run on such small tables should
not take so long. (assume no blocking, or hardware problems,etc.. ..)

Hope this helps,

Lucjan


"Simon Hayes" <sq*@hayes.ch> wrote in message
news:60**************************@posting.google.c om...
"DFS" <no****@nospam.com> wrote in message

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.

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)
);


Without table DDL and sample data, this is a guess, but it looks like
you could simplify the query to something along these lines:

select
p.project_id,
case when c.child_type is null then 'No Child Data' else 'Child Data,
Not type Z' end as sub_type
from
projects p
left outer join projects_children c
on p.project_id = c.project_id
where
c.child_type not like 'Z*'

If this isn't correct, please consider posting the DDL and sample
data.

Simon

Jul 20 '05 #3
There are several ways to rewrite your query. The performance they will
give partly depends on the size of your tables, and definitely depends
on the available indexes.

Here is one way that might perform better if there are many projects of
child-type "Z%". Also, the performance is likely to improve by changing
UNION to UNION ALL. This can be safely done here, because the joined
sets are mutually exclusive.

SELECT P.Project_ID
, 'No Child Data' AS Sub_Type
FROM Projects P
WHERE NOT EXISTS (
SELECT 1
FROM Projects_Children C
WHERE C.Project_ID = P.Project_ID
)

UNION ALL

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

SELECT DISTINCT P.Project_ID
, 'Child Data, Not type Z' AS Sub_Type
FROM Projects P
INNER JOIN Projects_Children C
WHERE C.Project_ID = P.Project_ID
AND C.Child_Type NOT LIKE 'Z%'

Hope this helps,
Gert-Jan
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.

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)
);

Jul 20 '05 #4
DFS

"Simon Hayes" <sq*@hayes.ch> wrote in message
news:60**************************@posting.google.c om...
"DFS" <no****@nospam.com> wrote in message

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.

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)
);


Without table DDL and sample data, this is a guess, but it looks like
you could simplify the query to something along these lines:

select
p.project_id,
case when c.child_type is null then 'No Child Data' else 'Child Data,
Not type Z' end as sub_type
from
projects p
left outer join projects_children c
on p.project_id = c.project_id
where
c.child_type not like 'Z*'

If this isn't correct, please consider posting the DDL and sample
data.

Simon,

Thanks, but that didn't work. I got an answer on the Access newsgroup which
worked well - 1 second data return.

Here's the actual SQL (I used aliases when I posted the question here)
-- This section produces parent records that have no children

SELECT P.PROJECT_ID, 'NO SUBMITTALS' AS SUB_TYPE
FROM PROJECTS P LEFT JOIN PROJECTS_SUBMITTALS S
ON P.PROJECT_ID = S.PROJECT_ID
WHERE S.SUBMITTAL_ID IS NULL

-- This UNION section produces parent records having children,
-- but the child records are not of type 'drawing*'

UNION SELECT P.PROJECT_ID, 'SUBMITTALS, BUT NO DRAWINGS' AS SUB_TYPE
FROM PROJECTS P
WHERE

-- This section makes sure the child records are not of type 'drawing'
(
P.PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P INNER JOIN PROJECTS_SUBMITTALS S
ON P.PROJECT_ID = S.PROJECT_ID
WHERE S.SUBMITTAL_TYPE Like "drawing*") AND

-- This section makes sure to return only parent records having child
records
P.PROJECT_ID NOT IN (
SELECT P.PROJECT_ID
FROM PROJECTS P LEFT JOIN PROJECTS_SUBMITTALS S
ON P.PROJECT_ID = S.PROJECT_ID
WHERE S.SUBMITTAL_ID IS NULL)
);


Jul 20 '05 #5

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

Similar topics

5
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...
2
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,...
1
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...
2
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...
2
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...
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. ...
3
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 ...
0
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...
9
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 ...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.