473,395 Members | 2,151 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,395 software developers and data experts.

SIMPLE but SLOW SELECT query!!!

4
Hi,

I have the following query which is running very slow

SELECT D.STORE_FACILITY_ID,D.PRIMARY_ACCT_NUM FROM DS_STORE D
WHERE D.PRIMARY_ACCT_NUM IN(SELECT W.COMPONENT1_ID FROM WK_FILTER_CMPNT W
WHERE W.COMPONENT1_ID = 1)FOR FETCH ONLY

can anyone help?
Jun 21 '07 #1
2 4577
MMcCarthy
14,534 Expert Mod 8TB
I don't really understand what you are doing the subquery selects COMPONENT1_ID Where COMPONENT1_ID = 1 so why not just say
WHERE PRIMARY_ACCT_NUM = 1

However, if as I suspect COMPONENT1_ID is not the field that holds the value corresponding to PRIMARY_ACCT_NUM then that is the field you should be Selecting in the subquery.
Jun 27 '07 #2
Snib
24
Assuming that the SQL you meant to post was as follows:

SELECT D.STORE_FACILITY_ID,
D.PRIMARY_ACCT_NUM
FROM DS_STORE D
WHERE D.PRIMARY_ACCT_NUM IN
(SELECT W.PRIMARY_ACCT_NUM
FROM WK_FILTER_CMPNT W
WHERE W.COMPONENT1_ID = 1)
FOR FETCH ONLY

Then there are a few things to consider:

1/ Temporary data created for an "IN" statement does not have an INDEX so the data is searched sequentially from start to end to find if the value being searched for is IN the list. If there is a large amount of data created for the IN statement this can slow down the SQL.

2/ If you "correlate" sub-selects then DB2 will use indexes, if they exist, to speed up the SQL. If the amount of data in the IN statement is large then correlated sub-selects can be faster.

3/ As you are not using any predicates to restrict the data selected on the first table then this table will be accessed via a table space scan. As the second table does have predicates (well one at least!) then this sub-select will be executed first by DB2 to create the data for the IN statement. Once it has this data it will then start the table space scan on the first table and search the data from the IN statement with the field selected - it will execute the search of the data for the IN statement for every row on the first table!

4/ You are not actually returning any of the data from the second table so you could use a correlated sub-select with an EXISTS clause. Something like this:

SELECT D.STORE_FACILITY_ID,
D.PRIMARY_ACCT_NUM
FROM DS_STORE D
WHERE EXISTS
(SELECT
FROM WK_FILTER_CMPNT W
WHERE W.COMPONENT1_ID = 1
AND W.PRIMARY_ACCT_NUM = D.PRIMARY_ACCT_NUM IN)
FOR FETCH ONLY

If you then have an index on DS_STORE with PRIMARY_ACCT_NUM as the first field and also an index on WK_FILTER_CMPNT with COMPONENT1_ID as the primary field then the SQL should be executed something like this:

A/ Execute sub-select, using the INDEX on WK_FILTER_CMPNT to find each row with a value of 1 in the COMPONENT1_ID field and return the PRIMARY_ACCT_NUM.

B/ Use this list of PRIMARY_ACCT_NUM values to then access table DS_STORE using the index with the PRIMARY_ACCT_NUM field. Only read the rows from the DS_STORE field were values in these fields match.

So, this will stop the repeated search of the data in the IN statement and should also remove the table space scan of the DS_STORE table - assuming you have the indexes I have mentioned above!

It is possible that the SQL could be executed as a table space scan on the first table and then index access on the second table - it will depend on the indexes and the values populated by RUNSTATS in the catalog.

One last thing, if you find you are getting duplicate rows on the result set then add a GROUP BY to the end of the SQL on D.STORE_FACILITY_ID and D.PRIMARY_ACCT_NUM.

Regards

Snib
Jun 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Roberto Esposito | last post by:
HI to All.... i need an help for a Select Query MYSQL Version 3.23.58 it's all day i try to resolve this problem.... but i don't find SOLUTION! sigh :-( This is the simple query.....
0
by: Jean | last post by:
Hi all, This problem has me dumbstruck at the moment and I was wondering if someone could offer some advice while I have a go at it myself. Background: I have a query qryAll that takes data...
9
by: MLH | last post by:
I have a table (tblCorrespondence) holding records with fields like , , , , , , , etc... About a dozen 's are defined and I often use queries to extract records of a given . That's pretty easy....
5
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
3
by: Antoni | last post by:
Hello, I have a table with more than 1,000,000 rows. mysqldescribe views_date; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key |...
6
by: Nano | last post by:
I have created ASP file from MS Access. It has the following Code. But it gives an error at: rs.Open sql, conn, 3, 3 The Error is: Error Type: Microsoft OLE DB Provider for ODBC Drivers...
3
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
8
by: Hamayun Khan | last post by:
Hi. I have a table with more than 30000 records having below structure. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE .( IDENTITY(1,1) NOT NULL,
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...

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.