473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_MQ T_PROC_AVGDUR;

create summary table db2admin.PIW_MQ T_PROC_AVGDUR AS (select
p.proc_template _name, d.the_year, d.quarter, d.month_of_year ,
count(*) as count,
avg(p.overall_d uration) as avg_duration,
max(p.overall_d uration) as max_duration,
min(p.overall_d uration) as min_duration,
avg(p.waiting_d uration) as wait_duration,
max(p.waiting_d uration) as max_waitduratio n,
min(p.waiting_d uration) as min_waitduratio n
from db2admin.piw_pr ocess p, db2admin.piw_da te d
where p.start_date_ke y=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_MQ T_PROC_AVGDUR Is 'AST created for
OLAPEVT';

commit;

Alter Table db2admin.PIW_MQ T_PROC_AVGDUR Activate Not Logged Initially;

Refresh Table db2admin.PIW_MQ T_PROC_AVGDUR;

commit;

Runstats On Table db2admin.PIW_MQ T_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_d uration) as avg_duration,
from db2admin.piw_pr ocess p, db2admin.piw_da te d
where p.start_date_ke y=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 2796
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_MQ T_TEST AS (select
p.proc_template _name,
sum(p.overall_d uration) as sum_overall_dur ation
from db2admin.piw_pr ocess 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_MQ T_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_MQ T_TEST;

create summary table db2admin.PIW_MQ T_TEST AS (select
p.proc_template _name,
sum(p.overall_d uration) as sum_overall_dur ation
from db2admin.piw_pr ocess 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_MQ T_TEST Is 'AST created for OLAPEVT';

commit;

Refresh Table db2admin.PIW_MQ T_TEST ;

commit;

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

Runstats On Table db2admin.PIW_PR OCESS 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_instanc e table
db2 "delete from explain_instanc e"

-- 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_instanc e was emptied before
there is just the one query in)
db2exfmt -d <database_nam e> -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
2092
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. This did confuse me. Of course I expected it should run faster, because of the small block index tree, compare with the RID index tree. select distinct(int(l_shipdate)/100) from lineitem;
4
4368
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 DEFERRED ENABLE QUERY OPTIMIZATION" db2 "refresh table db2inst1.thirdcube" db2 "RUNSTATS ON table db2inst1.thirdcube WITH DISTRIBUTION" But when I run a qurey: select a, sum(fact) from fact1 group by a
4
3483
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 refresh immediate enable query optimization in tablespace_test replicated;
3
2082
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 being added was practically never filled in when the first user tried to delete a row from the table. Now, the optimizer tried to enforce RI on the new relation. But instead of using the index generated by the relation, it used a table scan!
0
1242
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 there is no order by in query. Has anyone read that this is a limitation? create summary table s_t as (select f1, f2, count(*) from t1 group by f1, f2) select distinct f1, f2 from t1 - explain shows optimizer uses s_t
5
9837
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 IN (1, 3) AND DATA_REQ_DT IS NOT NULL We have 2 indexes for the table: Indname Column names
5
3047
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 table instead of the base table. What causes the optimizer to use the MQT tables rather than the tables as defined by the query? Is there a way to "encourage" use of the MQT tables? Thanks.
3
2372
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. ------------------------- Lets say I have a table FOO1 that has, among other columns, a column named A. There is a non-unique index on A that has medium selectivity. Lets also say I have a table FOO2 that has, among other columns, a column named B. ...
0
1659
by: P. Adhia | last post by:
Hi, Consider the following query select * from t1 where c1 = ? and c2 = ?
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6634
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.