If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to avoid table scan in insert? Pls help!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-07-04, 23:51
jmychung jmychung is offline
Registered User
 
Join Date: Oct 2003
Location: Hong Kong
Posts: 41
Unhappy next key share lock problem

Hi,

I am facing a serious locking problem and need to solve it urgently.

In session 1 I run the following sql:
INSERT INTO table_a (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10) VALUES (4538947, 4476205, 20557, 4476224, 226, 1001498, 2650, 0, 2650, 0);

execution plan shows tablescan on table_a
shapshot for locks shows 1 row lock(w) and 1 table lock(ix) on table_a

In session 1 I run the following sql:
update table_a set col10=4539505 where col1=4538947 AND col10=0;

execution plan shows indexscan (unique idex on col1) on table_a
shapshot for locks shows 1 row lock(x) and 1 table lock(ix) on table_a

Then in session 2 I run the following sql:
INSERT INTO table_a (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10) VALUES (4538971, 4476205, 20557, 4476224, 1210729, 1001498,
33320, 0, 33320, 0);

Session 2 hangs.
shapshot for locks shows 1 row lock(x) and 1 table lock(ix) on table_a
for session 1, and another row lock(x) and 1 table lock(ix) on table_a for session 2.

After I commit or rollback session 1, session 2 resumes.
Execution plan shows tablescan.
Shapshot for locks shows 1 row lock(w) and 1 table lock(ix) on table_a for session 2.

I think after session 1 issued an update statement, the updated row is locked exclusively. When session 2 inserts a new row, the optimizer choose to use tablescan. However, it cannot read the row locked by session 1.

If the assumption is right, I have several questions:
1. I have specified 'volatile' in creating table_a, and indexes exist for table_a, why the optimizer still use tablescan in insert statement?
2. How can I solve this locking problem?

Many thanks for any suggestion.

ps: no effect by using 'with ur/cs' in the sqls.

Last edited by jmychung; 09-15-04 at 04:55. Reason: for a better title
Reply With Quote
  #2 (permalink)  
Old 09-08-04, 02:58
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Give information on the db2 version/OS ...
Also the table and index definiton, including any related tables/indexes..
If possible post the execution plan ..

This will help the forum to give a better answer ..

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.

Last edited by sathyaram_s; 09-08-04 at 03:33.
Reply With Quote
  #3 (permalink)  
Old 09-08-04, 03:34
jmychung jmychung is offline
Registered User
 
Join Date: Oct 2003
Location: Hong Kong
Posts: 41
Post

OS: Win2K
DB2: V7.2 FP7

attached:
1. actual sql, table and index definition,
2. execution plan and
3. snapshot report(captured when application 26 hangs, application 18 run first then application 26)

Thanks a lot!
Attached Files
File Type: doc explaindump.doc (161.5 KB, 79 views)
File Type: txt snapshot6_SQL1.txt (10.8 KB, 61 views)
File Type: txt SQL_TBL_IDX.TXT (5.4 KB, 49 views)
Reply With Quote
  #4 (permalink)  
Old 09-08-04, 03:48
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
The statement is not doing tablescan on any 'physical' table .. (The table scan is on the GENROW) .. All locks seem to be OK ...

I think it is likely to be because of Next-Key Share locks (V7 Type 1 indexes) ... take an application snapshot to see why the session B is waiting (I thought lock snapshot shows this info, but doesn't seem to be there) at the tail end of the B's snapshot

HTH

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 09-08-04, 05:07
jmychung jmychung is offline
Registered User
 
Join Date: Oct 2003
Location: Hong Kong
Posts: 41
Post

Thanks for your reply.

I generate the application snapshot but the content are quite complicated to me. attached 28.txt(session A) and 32.txt(session B).

When will the next-key share lock hold? after insert or update? I tried different sequence of those sqls and found that if session A issue inserts statements only, session B can also issue insert statements, but once session A issues update statement, session B cannot issue insert or update statement anymore. It seems that the problem is caused by update by session A, but not insert.
Attached Files
File Type: txt 28.txt (7.2 KB, 57 views)
File Type: txt 32.txt (7.1 KB, 58 views)
Reply With Quote
  #6 (permalink)  
Old 09-08-04, 05:54
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
The lock wait is because of the Next Key Weak Exclusive lock confilicts ....

Next Key locks is a property of Type 1 indexes where a lock is obtained on an adjecent row when inserting into an index ...

The lock-wait is caused by the index

CREATE INDEX "SM_SET "."EV_STK_ENT_IDX_2" ON "SM_SET "."EV_STOCKS_ENTITLE"
("TRADE_LEG_OID" ASC)
PCTFREE 10 ALLOW REVERSE SCANS;

Google for 'Next Key Weak Exclusive" for more information

HTH

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #7 (permalink)  
Old 09-08-04, 06:11
jmychung jmychung is offline
Registered User
 
Join Date: Oct 2003
Location: Hong Kong
Posts: 41
Exclamation

Thanks for your info.

Since other sqls need that index. Is it possible to avoid this problem but not dropping that index?

also, when I test the same case but using AIX, the optimizer will use EV_STK_ENT_IDX_2 instead of pk_ev_stk_ent in update sql. That cause another performance problem. are there any method to force or direct the optimizer to use a particular index?
Reply With Quote
  #8 (permalink)  
Old 09-08-04, 06:22
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
You need not drop the index ... Find a balance between having the overhead of frequent commits and concurrency ... Have the update as nearer to the commit point as possible ...
In almost all cases, I would allow the optimizer to do what it thinks best .... DB2 UDB optimizer is quiet sophisticated to pick up the best access path if you provide it with proper information on statistics, bufferpool size, num of concurrent applications etc. For complex SQLs or joins, if you see a problem with the access path, get in touch with IBM ...

HTH

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #9 (permalink)  
Old 09-09-04, 00:55
jmychung jmychung is offline
Registered User
 
Join Date: Oct 2003
Location: Hong Kong
Posts: 41
Hi Sathyaram,

Your info are really useful to me. Thanks a lot!

However, I have 1 more question. There are several indexes on the table, but why only the existence of EV_STK_ENT_IDX_2 causes this problem?

EV_STK_ENT_IDX_2 is a non-unique index while other indexes are all unique. Is it true that NW will not be acquired if there are only unique indexes associated with a table?
Reply With Quote
  #10 (permalink)  
Old 09-09-04, 02:46
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Because that is the index with the updated column TRADE_LEG_OID

Version 8 has introducted Type-2 indexes ... Tnerefore you will not have this problem

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #11 (permalink)  
Old 09-09-04, 05:02
jmychung jmychung is offline
Registered User
 
Join Date: Oct 2003
Location: Hong Kong
Posts: 41
Thank you very much for your help.

As frequent commit has big impact on performance, I really hope there are other ways to solve this problem.

Again, thanks.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On