473,395 Members | 1,692 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.

Merge statement: Index on source table


I got a dumb question on the merge statement. I read the following
example of merge statement at the IBM page:
http://publib.boulder.ibm.com/infoce.../r0010873.htm:

MERGE INTO archive ar
USING (SELECT activity, description FROM activities) ac
ON (ar.activity = ac.activity)
WHEN MATCHED THEN
UPDATE SET
description = ac.description
WHEN NOT MATCHED THEN
INSERT
(activity, description)
VALUES (ac.activity, ac.description)

Can't we use activities as the source table directly, as:

MERGE INTO archive ar
USING activities ac
ON (ar.activity = ac.activity)
....

Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?

Thanks!
Sep 28 '08 #1
7 2741
Henry J. wrote:
I got a dumb question on the merge statement. I read the following
example of merge statement at the IBM page:
http://publib.boulder.ibm.com/infoce.../r0010873.htm:

MERGE INTO archive ar
USING (SELECT activity, description FROM activities) ac
ON (ar.activity = ac.activity)
WHEN MATCHED THEN
UPDATE SET
description = ac.description
WHEN NOT MATCHED THEN
INSERT
(activity, description)
VALUES (ac.activity, ac.description)

Can't we use activities as the source table directly, as:

MERGE INTO archive ar
USING activities ac
ON (ar.activity = ac.activity)
....
yes
>
Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?
yes

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 28 '08 #2
Serge Rielau wrote:
> MERGE INTO archive ar
USING (SELECT activity, description FROM activities) ac
ON (ar.activity = ac.activity)
WHEN MATCHED THEN
UPDATE SET
description = ac.description
WHEN NOT MATCHED THEN
INSERT
(activity, description)
VALUES (ac.activity, ac.description)
This looks like a poster-child case of someone needing about five
minutes behind the woodshed with Joe Celko on the topic of column
naming.
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)
Sep 28 '08 #3
*Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?

yes
Thanks. So, in general, indexes on a table inside a subquery can be
utilized just fine. Like in this example:

select T1.col1, T3.col4
from T1, (select col3, col4 from T2 where col1 = col2) T3
where T1.col2 = T3.col3

If there is an index on T2(col3), it will be used in joining T1 and T3
on (T1.col2 = T3.col3), right? How if the subquery on T2 gets more
complex? The optimizer can still figure out to use the indexes on T2
to join with T1? How's it implemented?

Or should I avoid subquery in general if possible to increase the
chance of the indexes being utilized? I noticed some of our legacy
code tends to use nested sub-queries, in some cases 3 or 4 level
deep. I guess the performance might be better if the level of nesting
is smaller?

Thanks.

Sep 29 '08 #4
On 29 Sep, 05:05, "Henry J." <tank209...@yahoo.comwrote:
*Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?
yes

Thanks. *So, in general, indexes on a table inside a subquery can be
utilized just fine. *Like in this example:

* select T1.col1, T3.col4
* from T1, (select col3, col4 from T2 where col1 = col2) T3
* where T1.col2 = T3.col3

If there is an index on T2(col3), it will be used in joining T1 and T3
on (T1.col2 = T3.col3), right? *How if the subquery on T2 gets more
complex? *The optimizer can still figure out to use the indexes on T2
to join with T1?
A while back there was a discussion regarding db2's optimizer and
indexes. It will not answer your questions, but perhaps it gives you
some thoughts:

http://tinyurl.com/3k6es2

/Lennart

[...]
Sep 29 '08 #5
On Sep 29, 12:54*am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
On 29 Sep, 05:05, "Henry J." <tank209...@yahoo.comwrote:


*Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?
yes
Thanks. *So, in general, indexes on a table inside a subquery can be
utilized just fine. *Like in this example:
* select T1.col1, T3.col4
* from T1, (select col3, col4 from T2 where col1 = col2) T3
* where T1.col2 = T3.col3
If there is an index on T2(col3), it will be used in joining T1 and T3
on (T1.col2 = T3.col3), right? *How if the subquery on T2 gets more
complex? *The optimizer can still figure out to use the indexes on T2
to join with T1?

A while back there was a discussion regarding db2's optimizer and
indexes. It will not answer your questions, but perhaps it gives you
some thoughts:

*http://tinyurl.com/3k6es2

/Lennart

[...]- Hide quoted text -

- Show quoted text -
Thanks but that thread is regarding the index usage when a function is
used? My question is on index usage when the base table is tucked
away in a sub-query or in a nested sub-query.
Sep 29 '08 #6
Henry J. wrote:
On Sep 29, 12:54 am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
>On 29 Sep, 05:05, "Henry J." <tank209...@yahoo.comwrote:


>>>> Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?
yes
Thanks. So, in general, indexes on a table inside a subquery can be
utilized just fine. Like in this example:
select T1.col1, T3.col4
from T1, (select col3, col4 from T2 where col1 = col2) T3
where T1.col2 = T3.col3
If there is an index on T2(col3), it will be used in joining T1 and T3
on (T1.col2 = T3.col3), right? How if the subquery on T2 gets more
complex? The optimizer can still figure out to use the indexes on T2
to join with T1?
A while back there was a discussion regarding db2's optimizer and
indexes. It will not answer your questions, but perhaps it gives you
some thoughts:

http://tinyurl.com/3k6es2

/Lennart

[...]- Hide quoted text -

- Show quoted text -

Thanks but that thread is regarding the index usage when a function is
used? My question is on index usage when the base table is tucked
away in a sub-query or in a nested sub-query.
Henry,

DB2 has a sophisticated query-rewrite engine. One of it's goals is to
bring predicates as close to the data as possible so the optimizer can
choose the best access plan.
There is no "will" with a cost based optimizer. Its decisions depend on
statistics. As to how it is implemented that si part of either the
secret sauce, patented or published in research papers. The later two
you can find if you look hard enough, for the first you'll need to hire
into our team :-)

So see what DB2 has come up with you can perform explains (db2expln,
viusal explain or db2exfmt). You will find that in general subqueries
will provide no handicap to DB2 finding a good plan.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 30 '08 #7
On Sep 30, 6:38*am, Serge Rielau <srie...@ca.ibm.comwrote:
Henry J. wrote:
On Sep 29, 12:54 am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
On 29 Sep, 05:05, "Henry J." <tank209...@yahoo.comwrote:
>>>*Also, in the original example, if the table activities has an index
on the column activity, can it be utilized since the source table is a
query on the table activities?
yes
Thanks. *So, in general, indexes on a table inside a subquery can be
utilized just fine. *Like in this example:
* select T1.col1, T3.col4
* from T1, (select col3, col4 from T2 where col1 = col2) T3
* where T1.col2 = T3.col3
If there is an index on T2(col3), it will be used in joining T1 and T3
on (T1.col2 = T3.col3), right? *How if the subquery on T2 gets more
complex? *The optimizer can still figure out to use the indexes on T2
to join with T1?
A while back there was a discussion regarding db2's optimizer and
indexes. It will not answer your questions, but perhaps it gives you
some thoughts:
*http://tinyurl.com/3k6es2
/Lennart
[...]- Hide quoted text -
- Show quoted text -
Thanks but that thread is regarding the index usage when a function is
used? *My question is on index usage when the base table is tucked
away in a sub-query or in a nested sub-query.

Henry,

DB2 has a sophisticated query-rewrite engine. One of it's goals is to
bring predicates as close to the data as possible so the optimizer can
choose the best access plan.
There is no "will" with a cost based optimizer. Its decisions depend on
* statistics. As to how it is implemented that si part of either the
secret sauce, patented or published in research papers. The later two
you can find if you look hard enough, for the first you'll need to hire
into our team :-)

So see what DB2 has come up with you can perform explains (db2expln,
viusal explain or db2exfmt). You will find that in general subqueries
will provide no handicap to DB2 finding a good plan.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Excellent. So subqueries will be rewritten by the optimizer, that
answered my "how" question. I guess anything more detailed would not
interest me much for the time being as long as I know it's Serge's
magic in work :)
Oct 1 '08 #8

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

Similar topics

3
by: jaykchan | last post by:
One of the table that is in a merge replication somehow is missing an index. Strangely, only the table in one of the subscriber of the merge replication is missing the index; another subscriber and...
3
by: Bob Stearns | last post by:
We have a very poorly performing MERGE statement (an hour or more on tables of ~10000 and ~100000). This may require building temporary tables with appropriate indexes, but I thought I would ask...
4
by: marklawford | last post by:
Hi guys, I'm getting the following error message when trying to run a MERGE statement I'm putting together. The syntax looks right to me But i must be missing something. The "srce" table...
11
by: UDBDBA | last post by:
Hi: This is a merge questions which has been posted and answered... in my case need more clairification when target table (tableB) matched multiple rows to be updated based on the ON condition...
16
by: UDBDBA | last post by:
Hi All: I need some clarification on a MERGE statement. The database is on V8 FP12 (AIX) 64bit. The source table is tableA. The target is a View "FACT" with UNION ALL because of the 512 Gig...
3
by: John Cosmas | last post by:
I have a DATATABLE which I have populated in my application, and I need it written out to a particular table I specify in my ACCESS database. My code works to the point of the MERGE and UPDATE,...
16
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan...
1
by: UDBDBA | last post by:
Hi, If someone can clarify this, it would be of great help! We have a merge, which is written *only* to update the target table. but the access plan shows branches with inserts and deletes on...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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
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...

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.