473,394 Members | 1,951 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,394 software developers and data experts.

Help : Complex SQL query

Hi

Need a help in rewritting the folwoing SQL which is taking more than a
day to give resultset. Is there anything wrong in this SQL coding ?

SELECT DISTINCT 214 AS BATCH_ID,
TAB1.customer_sk,
TAB1.CUSTOMER_ID,
TAB1.product_line_sk,
COUNT(DISTINCT TAB1.ACCOUNT_SK) AS COUNT
FROM SCHEMA1.TABLE1 TAB1
WHERE TAB1.acc_opened IN (SELECT datetime
from SCHEMA1.TAB2

where day between (select day-30 from SCHEMA1.TAB2 where datetime =
(SELECT batch_date FROM SCHEMA2.TAB3 WHERE BATCH = 214)) and (select
day from SCHEMA1.TAB2 where datetime = (SELECT batch_date FROM
SCHEMA2.TAB3 WHERE BATCH = 214)))

GROUP BY TAB1.customer_sk, TAB1.CUSTOMER_ID, TAB1.product_line_sk
HAVING COUNT(DISTINCT TAB1.ACCOUNT_SK) > 1
Nov 12 '05 #1
3 6394
What version and release of DB2? Which operating system?
Do the tables have accurate statistics (RUNSTATS)? Are any of the tables
partitioned (and what are the partitioning keys)? What indexes do you
have created? Have you tried the index advisor? How many rows are in
each table? The answers to these questions may help DB2 to optimize this
query without forcing you to hand-tune it.

Kumar wrote:
Hi

Need a help in rewritting the folwoing SQL which is taking more than a
day to give resultset. Is there anything wrong in this SQL coding ?

SELECT DISTINCT 214 AS BATCH_ID,
TAB1.customer_sk,
TAB1.CUSTOMER_ID,
TAB1.product_line_sk,
COUNT(DISTINCT TAB1.ACCOUNT_SK) AS COUNT
FROM SCHEMA1.TABLE1 TAB1
WHERE TAB1.acc_opened IN (SELECT datetime
from SCHEMA1.TAB2

where day between (select day-30 from SCHEMA1.TAB2 where datetime =
(SELECT batch_date FROM SCHEMA2.TAB3 WHERE BATCH = 214)) and (select
day from SCHEMA1.TAB2 where datetime = (SELECT batch_date FROM
SCHEMA2.TAB3 WHERE BATCH = 214)))

GROUP BY TAB1.customer_sk, TAB1.CUSTOMER_ID, TAB1.product_line_sk
HAVING COUNT(DISTINCT TAB1.ACCOUNT_SK) > 1


Nov 12 '05 #2
Try this one. I took out 2 joins.. should make a difference.

SELECT DISTINCT 214 AS BATCH_ID,
TAB1.customer_sk,
TAB1.CUSTOMER_ID,
TAB1.product_line_sk,
COUNT(DISTINCT TAB1.ACCOUNT_SK) AS COUNT
FROM SCHEMA1.TABLE1 TAB1
WHERE TAB1.acc_opened
IN (SELECT T1.datetime
from SCHEMA1.TAB2 T1,
SCHEMA1.TAB2 T2,
SCHEMA2.TAB3 T3
where T1.day between T2.day-30 AND T2.day
and T2.datetime = T3.batchdate
and BATCH = 214)
GROUP BY TAB1.customer_sk,
TAB1.CUSTOMER_ID,
TAB1.product_line_sk
HAVING COUNT(DISTINCT TAB1.ACCOUNT_SK) > 1
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
ku***@tumkurcity.com (Kumar) wrote in message news:<2f**************************@posting.google. com>...
Hi

Need a help in rewritting the folwoing SQL which is taking more than a
day to give resultset. Is there anything wrong in this SQL coding ?


Well, here's a few things to try:
1. run stats on all tables and try again to see if your performance
improves
2. look at the results of an explain for bad things
3. put indexes on tab3.batch, tab2.datetime, tab1.acc_opened
4. replace date-oriented subselects with joins, ideally with a temp
table with 30 days of data that correspond to the range you want.
5. or replace date-oriented subselects with values from prior queries

oh, and get rid of the distincts - if you've got duplicates then
you're probably not grouping properly.

ken
Nov 12 '05 #4

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

Similar topics

4
by: Starbuck | last post by:
OK, first let me say that I am no DB person. But I have a user here who keeps getting this error whenever she does, whatever it is she does, with databases... A google search takes me to...
1
by: Eva | last post by:
Hi, Im new to Vb.net and am trying to create my first practice project. The problem iv encountered is to do with SQL queries issued against my Access DB iv created. i want to wright a query...
14
by:  | last post by:
having a spot of trouble writing this one. if you are so inclined and have a moment, i'd really appreciate your insight. i have what amounts to a purchase order type of setup...a descriptive...
6
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the Lotus product). There are things I could easily...
1
by: Melissa Kay Beeline | last post by:
OK, here's the sitch : we have an access control system at work that registers ever entry/exit of every employee. I recently made some queries in Access so the ppl in HR could make reports (who...
3
by: dd_bdlm | last post by:
Please help this one is driving me mad! I have searched and read all the topics on the error message I am receiving but none seem to apply to me! I have quite a complex query linking all parts...
8
by: babyangel43 | last post by:
Hello, I have a query set up in Access. I run it monthly, changing "date of test". I would like this query to be merged with a Word document so that the cover letter is created in Word, the fields...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
3
by: william67 | last post by:
I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated this is the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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,...
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
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...

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.