473,385 Members | 2,014 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.

DB2 Optimizer is not using Summary Table (DB2 V 8.1.7)

Hi,

I've read the former postings but was not able to solve my problem:

I have a Summary Table (or MQT as you like it) and the query optimizer
does not seem to use the summary table.

I run the following commands:

set current query optimization 9;
set current refresh age = any;

drop table db2admin.PIW_MQT_PROC_AVGDUR;

create summary table db2admin.PIW_MQT_PROC_AVGDUR AS (select
p.proc_template_name, d.the_year, d.quarter, d.month_of_year,
count(*) as count,
avg(p.overall_duration) as avg_duration,
max(p.overall_duration) as max_duration,
min(p.overall_duration) as min_duration,
avg(p.waiting_duration) as wait_duration,
max(p.waiting_duration) as max_waitduration,
min(p.waiting_duration) as min_waitduration
from db2admin.piw_process p, db2admin.piw_date d
where p.start_date_key=d.date_key
group by p.proc_template_name, d.the_year, d.quarter, d.month_of_year)
Data initially deferred
Refresh deferred
Enable Query Optimization
Maintained by system
Not Logged initially
IN EVENTSPACE ;

comment on table db2admin.PIW_MQT_PROC_AVGDUR Is 'AST created for
OLAPEVT';

commit;

Alter Table db2admin.PIW_MQT_PROC_AVGDUR Activate Not Logged Initially;

Refresh Table db2admin.PIW_MQT_PROC_AVGDUR;

commit;

Runstats On Table db2admin.PIW_MQT_PROC_AVGDUR;

commit;

(I took the sequence of commands from the example sql script to create
a summary table:
http://publib.boulder.ibm.com/infoce...coptscript.htm)
Afterwards I try the following command in the Command Editor:

select p.proc_template_name, d.the_year, d.quarter, d.month_of_year,
count(*) as count,
avg(p.overall_duration) as avg_duration,
from db2admin.piw_process p, db2admin.piw_date d
where p.start_date_key=d.date_key
group by p.proc_template_name, d.the_year, d.quarter, d.month_of_year

Viewing the Access Plan (Explain SQL) the query is using the basic
tables (piw_date, piw_process) and not the summary table.

Any ideas? Are there any other settings I didn't consider?

Thanks,
Andrea

Nov 12 '05 #1
9 2778
Hello Andrea;
I had this problem a few years ago... If you issue the "set current
refresh age=any;" statement before your query, you will tell the
optimizer that you don't care if the data in the MQT is a little stale.
You can set this to various numeric values as well to control WHEN the
optimizer chooses the MQT based on how "fresh" the data is. See the doc
for details. Good luck!

Pete H

Nov 12 '05 #2
Hello Pete,

I tried your hint setting the current refresh age to 99999999999999 (as
written in the help possible values are 0, 99999999999999 or ANY; but
ANY ist just a shorthand for 99999999999999).

Beside this I found another special register and tried it as well: set
current maintained table types for optimization = ALL; This specifies
that all possible types of maintained tables controlled by this special
register, now and in the future, are to be considered when optimizing
the processing of dynamic SQL queries.

Nevertheless the optimizer is still not using the summary table!

Thanks,
Andrea

################################################## ###########

Sequence of used sql statements:

set current query optimization = 9;
set current refresh age = 99999999999999;
set current maintained table types for optimization = ALL;

- the rest is the same as in the first posting

Nov 12 '05 #3
All of the parameters you are manipulating are used to inform the
optimizer that it's acceptable to attempt to use use the MQT table
during optimization. None of these guarantees that the optimizer will
choose to use it. Your query asks for the count(*) which, I would guess,
is prohibiting use of the MQT. This is reasonable because a single
insert or update would invalidate the count maintained in the MQT. Have
you tried explaining retrievals for count and avg independently? I
wouldn't be surprised if the optimizer chose not to use the MQT for either.

The AVG function is not allowed (SQL ref V2 [db2s2e81-3] p326) in an MQT
when using REFRESH IMMEDIATE; a reasonable performance restriction.
Imagine the overhead of recalculating AVG for every insert, delete,
update statement.

You could try setting up indexes on the columns used by the query. Split
the index data between the index columns and INCLUDE columns to make
best advantage of the indexes for this and other queries. The optimizer
will tell you if the indexes are better performing that the base tables.

The column names (date information) you are using indicate that the
tables will contain much historical data. With the exception of the
current month; all of the data may be static and would not change once
the month ends. If this is true; then you can retrieve all data except
the current month directly from the MQT. You will have to refresh the
MQT immediately after the end of the month to keep its data "current".
Queries that include the current month should be split into a UNION of
the MQT and the base tables where only the current month's data is
retrieved from the base tables.
Phil Sherman

Andrea wrote:
Hello Pete,

I tried your hint setting the current refresh age to 99999999999999 (as
written in the help possible values are 0, 99999999999999 or ANY; but
ANY ist just a shorthand for 99999999999999).

Beside this I found another special register and tried it as well: set
current maintained table types for optimization = ALL; This specifies
that all possible types of maintained tables controlled by this special
register, now and in the future, are to be considered when optimizing
the processing of dynamic SQL queries.

Nevertheless the optimizer is still not using the summary table!

Thanks,
Andrea

################################################## ###########

Sequence of used sql statements:

set current query optimization = 9;
set current refresh age = 99999999999999;
set current maintained table types for optimization = ALL;

- the rest is the same as in the first posting


Nov 12 '05 #4
Andrea,

As Pete mentioned above, you have to issue the

SET CURRENT REFRESH AGE=ANY;

in the same session/window where you're issuing your query or your
explain statement.
And as Philip said, there is no guarantees that DB2 will choose the
MQT, the decision is based on cost, although in this case, assuming you
have some real data in your tables and runstats have been ran on both
base tables as well as the mqt I would expect DB2 to pick the MQT.

Nov 12 '05 #5
Thanks for your hints!

The count should be no problem: for refresh immediate Tables with GROUP
BY it is even required. Anyways I tried it without count and changed
the avg function to a sum function - without success.

Concerning the prohibition of AVG for REFRESH IMMEDIATE: thats why the
Summary Table is REFRESH DEFERRED as it would not make sense to refresh
the summary tables for every little change in the base tables.

I also set up indexes now - the give a good performance gain.
Nevertheless the costs idicate that it would be better to use the
summaryt table. I simlyfied the table like this:
create summary table db2admin.PIW_MQT_TEST AS (select
p.proc_template_name,
sum(p.overall_duration) as sum_overall_duration
from db2admin.piw_process p
where p.wpc_eventcode = 21004
group by p.proc_template_name)
Data initially deferred
Refresh deferred
Enable Query Optimization
Maintained by system
IN EVENTSPACE ;

The total cost of the query as used to create the summary table is
133.87 timerons and the cost of a direct query to the summary table
db2admin.PIW_MQT_TEST is 10.66 timerons.

Yes thats right: the tables are meant to analyse historical data. I
will most probably implement the refresh of the summary tables at first
in the corresponding web application: just a button and the user can
decide when to refresh. This is just a prototpye and for that reason
that should be sufficient.

Nov 12 '05 #6
I still use the following sequence of commands in one and only one
connection to the database:

set current query optimization = 9;
set current refresh age = ANY;
set current maintained table types for optimization = ALL;

drop table db2admin.PIW_MQT_TEST;

create summary table db2admin.PIW_MQT_TEST AS (select
p.proc_template_name,
sum(p.overall_duration) as sum_overall_duration
from db2admin.piw_process p
where p.wpc_eventcode = 21004
group by p.proc_template_name)
Data initially deferred
Refresh deferred
Enable Query Optimization
Maintained by system
IN EVENTSPACE ;
comment on table db2admin.PIW_MQT_TEST Is 'AST created for OLAPEVT';

commit;

Refresh Table db2admin.PIW_MQT_TEST ;

commit;

Runstats On Table db2admin.PIW_MQT_TEST WITH DISTRIBUTION AND DETAILED
INDEXES ALL;

Runstats On Table db2admin.PIW_PROCESS WITH DISTRIBUTION AND DETAILED
INDEXES ALL;

commit;

Afterwards I test it with the same query as used to create the summary
table and the optimizer decides to use the base tables.

Are there any other settings/commands I might have fogotten?

Nov 12 '05 #7
You don't have to set the refresh age prior to the creation of the mqt,
but you do have to have it set to ANY when running the query.

Nov 12 '05 #8
The setting of refresh age is by chance prior to the creation of the
mqt. I run the query in the same database connection as the creation of
the mqt.

Nov 12 '05 #9
I found a solution:

The Visual Explain (integrated into the Command Editor or Control
Center) is showing the wrong Access Plan. I tested this by changing the
basic date, NOT refreshing the MQT - running a query matching to the
MQT the "old" data will be displayed, running a query not matching to
the MQT (e.g. with other column functions) the fresh data from the
basic table will be displayed. Nevertheless the same Access plan will
be showed.

When I am using the Command line explain I can see that the optimizer
is using the MQT. This one shows also an Access Plan.

I run the following commands in a db2 command window. (You get this by
running the command "db2cmd" in a normal command window):

-- setting the registers: current refresh age = any, current query
optimization = 5 and current maintained table types for optimization =
all

-- delete all entries from the explain_instance table
db2 "delete from explain_instance"

-- set the current explain mode to yes
db2 set current exlpain mode yes

-- run the query
query

-- optional depending if you want to explain more sql statements
db2 set current explain mode no

- generate a ASCII file from the explain information which are at the
moment in the database (as the explain_instance was emptied before
there is just the one query in)
db2exfmt -d <database_name> -o <output_file> -1

Nov 12 '05 #10

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

Similar topics

4
by: Fan Ruo Xin | last post by:
Hi, I have been watching MDC for a long time ... I have seen it from INDEX-SCAN to INDEX ONLY for the DISTINCT (block index columns) operator .... But the Elapsed time shows MDC still run slower....
4
by: Angela | last post by:
I had built a summary table as follow: db2 "create table db2inst1.thirdcube as (select a,b,c,sum(fact) as amount from db2inst1.fact1 group by cube(a,b,c) ) DATA INITIALLY DEFERRED REFRESH...
4
by: jane | last post by:
HI, I try to create summary table like following: create table summary (a int, b int, c int) (select a.aa, b.bb, b.cc from table_a a ,table_b b where a.key=b.key) data initially deferred...
3
by: Peter Arrenbrecht | last post by:
Hi all We ran into a very annoying optimizer problem recently. We had added a new key and self-join relation to a large production table. That key will be filled very rarely and having just...
0
by: Rob K. | last post by:
I have a new summary table built againt a single data table. Simple select, count(*) and group by type. After I built it, I found that the optimizer will choose to use it for existing queries IF...
5
by: Venkatesh Subbaramu | last post by:
Hi Expert DBAs, I am facing an issue with a query, can anyone help me through? I have a Query: UPDATE VMUS.MSG_MSTR a SET MSG_STA_ID =5 WHERE a.PROC_ID =2 AND a.file_type_id =1 AND MSG_STA_ID...
5
by: Kevin | last post by:
Using a base table, a MQT table was created. With optimization - when querying the base table with calcuation that are already completed in the MQT - I would assume the optimizer would use the MQT...
3
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 Sorry if these are newbie questions. Optimizer stuff is black magic to me. For both of these, assume stats are current and an even distribution of data....
0
by: P. Adhia | last post by:
Hi, Consider the following query select * from t1 where c1 = ? and c2 = ?
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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...

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.