472,782 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

question: db2 LUW V8 UNION ALL with table function month() have bad query performance

question: db2 LUW V8 UNION ALL with table function month() have bad
query performance

Env:
db2 LUW V8 + FP14

Problem :
We have history data from 2005/01/01 ~ 2007/05/xx in single big
table,
we try separate this big table into twelve tables and create a view
to join these 12 tables with UNION ALL .
after query the view, we get bad query performance than query single
big table.

follow our scenario :

create table sales_01(
sales_date date not null,
prod_id integer,
city_id integer,
channel_id integer,
revenue decimal(20,2) ) ;

create table sales_02 like sales_01 ;
create table sales_03 like sales_01 ;
create table sales like sales_01 ;
create table sales_05 like sales_01 ;
create table sales_06 like sales_01 ;
create table sales_07 like sales_01 ;
create table sales_08 like sales_01 ;
create table sales_09 like sales_01 ;
create table sales_10 like sales_01 ;
create table sales_11 like sales_01 ;
create table sales_12 like sales_01 ;

alter table sales_01 add constraint date_01
check(month(sales_date)=1);
alter table sales_02 add constraint date_02
check(month(sales_date)=2);
alter table sales_03 add constraint date_03
check(month(sales_date)=3);
alter table sales_04 add constraint date_04
check(month(sales_date)=4);
alter table sales_05 add constraint date_05
check(month(sales_date)=5);
alter table sales_06 add constraint date_06
check(month(sales_date)=6);
alter table sales_07 add constraint date_07
check(month(sales_date)=7);
alter table sales_08 add constraint date_08
check(month(sales_date)=8);
alter table sales_09 add constraint date_09
check(month(sales_date)=9);
alter table sales_10 add constraint date_10
check(month(sales_date)=10);
alter table sales_11 add constraint date_11
check(month(sales_date)=11);
alter table sales_12 add constraint date_12
check(month(sales_date)=12);

create view all_sales as
(
select * from sales_01
union all
select * from sales_02
union all
select * from sales
union all
select * from sales_05
union all
select * from sales_06
union all
select * from sales_07
union all
select * from sales_08
union all
select * from sales_09
union all
select * from sales_10
union all
select * from sales_11
union all
select * from sales_12
) ;
test select sql
select * from all_sales_04 where sales_date '2007-10-15' ;

explain command:
db2expln -d sample -f sel.sql -t -g -z ;

explain output :

SQL Statement:

select *
from all_sales_04
where sales_date '2007-10-15'
Section Code Page = 850

Estimated Cost = 142.257706
Estimated Cardinality = 267.666626

(
| Access Table Name = ALANCHEN.SALES_04_11 ID = 2,14
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_10 ID = 2,13
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_09 ID = 2,12
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_08 ID = 2,11
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_07 ID = 2,10
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_06 ID = 2,9
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_05 ID = 2,8
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_04 ID = 2,7
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_02 ID = 2,5
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_01 ID = 2,4
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
UNION
| Access Table Name = ALANCHEN.SALES_04_12 ID = 2,15
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 1
)
Return Data to Application
| #Columns = 5

End of section
our currently solution is re-create view similar follow :
create view all_sales_05 as
(
select * from sales_01
where sales_date between '2007-01-01' and '2007-01-31' or
sales_date between '2006-01-01' and '2006-01-31' or
sales_date between '2005-01-01' and '2005-01-31'
union all
select * from sales_02
where sales_date between '2007-02-01' and '2007-02-28'
union all
select * from sales_03
where sales_date between '2007-03-01' and '2007-03-31'
union all
select * from sales_04
where sales_date between '2007-04-01' and '2007-04-30'
union all
select * from sales_05
where sales_date between '2007-05-01' and '2007-05-31'
union all
select * from sales_06
where sales_date between '2007-06-01' and '2007-06-30'
union all
select * from sales_07
where sales_date between '2007-07-01' and '2007-07-31'
union all
select * from sales_08
where sales_date between '2007-08-01' and '2007-08-31'
union all
select * from sales_09
where sales_date between '2007-09-01' and '2007-09-30'
union all
select * from sales_10
where sales_date between '2007-10-01' and '2007-10-31'
union all
select * from sales_11
where sales_date between '2007-11-01' and '2007-11-30'
union all
select * from sales_12
where sales_date between '2007-12-01' and '2007-12-31'
) ;

new exlpain output:
select *
from all_sales_05
where sales_date '2007-10-15'
Section Code Page = 850

Estimated Cost = 38.844070
Estimated Cardinality = 21.900002

(
| Access Table Name = ALANCHEN.SALES_04_11 ID = 2,14
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 3
UNION
| Access Table Name = ALANCHEN.SALES_04_10 ID = 2,13
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 3
UNION
| Access Table Name = ALANCHEN.SALES_04_12 ID = 2,15
| | #Columns = 5
| | Relation Scan
| | | Prefetch: Eligible
| | Lock Intents
| | | Table: Intent Share
| | | Row : Next Key Share
| | Sargable Predicate(s)
| | | #Predicates = 3
)
Return Data to Application
| #Columns = 5

End of section
but our data is continue growing and this view is need to maintain
every year for DBA,
did anyone know other simple solution can maintain this kind data ?
Thanks

May 29 '07 #1
5 3740
You need to change your check constraints:
CHECK (salesdate BETWEEN '2007-10-01' AND '2007-10-31')
etc...
By doing round robin there is no way a query with >= or <= can prune any
branches because there is a new July next year too.

Uisng the changed check constraints you can also roll out data much nicer:
1. Empty the partition
2. alter table drop old 2007 constraint add 2008 constraint.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 29 '07 #2
On 5月30日, 上午3時55分, Serge Rielau <srie...@ca.ibm..comwrote:
You need to change your check constraints:
CHECK (salesdate BETWEEN '2007-10-01' AND '2007-10-31')
etc...
By doing round robin there is no way a query with >= or <= can prune any
branches because there is a new July next year too.

Uisng the changed check constraints you can also roll out data much nicer:
1. Empty the partition
2. alter table drop old 2007 constraint add 2008 constraint.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Hi Serge ,

Thanks your advice.

May 30 '07 #3
Serge Rielau wrote:
You need to change your check constraints:
CHECK (salesdate BETWEEN '2007-10-01' AND '2007-10-31')
etc...
By doing round robin there is no way a query with >= or <= can prune any
branches because there is a new July next year too.
Just curious, would adding a generated column as month(salesdate), and
an index on that column help?

/Lennart

[...]
May 30 '07 #4
Lennart wrote:
Serge Rielau wrote:
>You need to change your check constraints:
CHECK (salesdate BETWEEN '2007-10-01' AND '2007-10-31')
etc...
By doing round robin there is no way a query with >= or <= can prune
any branches because there is a new July next year too.

Just curious, would adding a generated column as month(salesdate), and
an index on that column help?
No. No pruning is no pruning. It's a semantic problem.
If the queries were of the form WHERE MONTH(DATE) = 5 it would help.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 30 '07 #5
Serge Rielau wrote:
Lennart wrote:
>Serge Rielau wrote:
>>You need to change your check constraints:
CHECK (salesdate BETWEEN '2007-10-01' AND '2007-10-31')
etc...
By doing round robin there is no way a query with >= or <= can prune
any branches because there is a new July next year too.

Just curious, would adding a generated column as month(salesdate), and
an index on that column help?
No. No pruning is no pruning. It's a semantic problem.
If the queries were of the form WHERE MONTH(DATE) = 5 it would help.
Thanks, after rereading wugons questions I realize what a stupid
question I asked. In my mind he where asking a question like the one you
suggest at the end, in reality something completely different.
/Lennart
May 31 '07 #6

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

Similar topics

3
by: Matias Silva | last post by:
Hi Everyone, I wrote a for loop to build several select statements that are combined with a UNION. When I execute one of the queries separately, it works, but when I execute the query with a UNION...
7
by: jane | last post by:
HI, I was ask to do a query to get the member active condition I had table to show the member number and active status in three month. ACC A1 A2 A3 ---- -----------...
1
by: Joachim Klassen | last post by:
Hi all, I'm investigating partitioned tables using a UNION ALL VIEW and found the following (see ddl below): If I create a check constraint like "check (month(tdate) = 1)" DB2 won't do branch...
8
by: lyn.duong | last post by:
Hi, I have a large table (about 50G) which stores data for over 7 years. I decided to split this table up into a yearly basis and in order to allow minimum changes to the applications which...
5
by: Alicia | last post by:
Hello everyone based on the data, I created a union query which produces this. SELECT ,,, 0 As ClosedCount FROM UNION SELECT ,, 0 AS OpenedCount, FROM ORDER BY , ;
1
by: PATGMorris | last post by:
I've got a Union Query that pulls data from two different tables for chemistry and micro testing. The tables containing very similar data but for reasons not necessary here, cannot be put into one...
3
by: dkintheuk | last post by:
Hi everyone, I have a table with a set of accounts in that contains one row for each account for each time it has been audited. (this may not include every account we deal with) Each month I...
3
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are...
2
kcdoell
by: kcdoell | last post by:
Hello: I am trying to create a union query but do not have a lot of experience. Basically I have the below tables: The Tables: Table Name = tblPrior CreditRegIDFK; Number; Foreign Key...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.