home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / database / informix / 2403 < prev    next >
Encoding:
Internet Message Format  |  1992-11-12  |  5.3 KB

  1. Path: sparky!uunet!gossip.pyramid.com!pyramid!infmx!davek
  2. From: davek@informix.com (David Kosenko)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re: *** HELP - Index Locks are creating DEADLOCKS ***
  5. Message-ID: <1992Nov12.160935.21426@informix.com>
  6. Date: 12 Nov 92 16:09:35 GMT
  7. References: <1992Nov9.111120.25041@bernina.ethz.ch> <1992Nov10.160339.29203@informix.com> <1992Nov11.120353.14284@bernina.ethz.ch>
  8. Sender: news@informix.com (Usenet News)
  9. Organization: Informix Software, Inc.
  10. Lines: 120
  11.  
  12. Thomas Schiele writes:
  13. >The test database: 
  14. >   CREATE DATABASE test WITH LOG;
  15. >   CREATE TABLE rel (
  16. >      id CHAR(1),
  17. >      data CHAR(10)
  18. >   ) 
  19. >   LOCK MODE ROW;
  20. >   INSERT INTO rel VALUES ("1", "one");
  21. >   INSERT INTO rel VALUES ("2", "two");
  22. >   INSERT INTO rel VALUES ("3", "three");
  23. >   INSERT INTO rel VALUES ("4", "four");
  24. >   INSERT INTO rel VALUES ("5", "five");
  25. >
  26. >Example 1:
  27. >   We use the test database.
  28. >   User A does the following and stays in transaction mode:
  29. >      BEGIN WORK;
  30. >      DELETE FROM rel WHERE id = "2";
  31. >
  32. >   User B does the following:
  33. >      BEGIN WORK; 
  34. >      DELETE FROM rel WHERE id = "4"; 
  35. >      result: 244: Could not do a physical-order read to fetch next row.
  36. >              107: ISAM error: record is locked.
  37. >   It doesn't seem to work as expected.
  38.  
  39.  
  40. Aha!  This is where the explanation of what you are trying to do makes it all
  41. clear.  In this case, a sequential scan is needed to find the row, since no
  42. indexes are available.  But since row "2" is locked, the data cannot be scanned
  43. past that locked row (it cannot "jump over" a lock), so the lock error is
  44. returned.
  45.  
  46. >Example 2:
  47. >   We use our test database.
  48. >   We install an index in our test database:
  49. >      CREATE INDEX relind ON rel (
  50. >         id
  51. >      );
  52. >
  53. >   User A does the following and stays in transaction mode (same as above): 
  54. >      BEGIN WORK; 
  55. >      DELETE FROM rel WHERE id = "2"; 
  56. >    
  57. >   User B does the following::   
  58. >      BEGIN WORK;  
  59. >      DELETE FROM rel WHERE id = "4";  
  60. >      result: 1 row(s) deleted.
  61. >   It works as expected/
  62.  
  63. That is because by using the index, it no longer has to scan the data 
  64. sequentially, so the locked row is not a problem.  If, on the other hand,
  65. you specified a range (e.g. WHERE id > "1"), you would have run into the
  66. same problem even with the index, i.e. it could not get past the lock on
  67. "2" in order to get the whole range.  
  68.  
  69. In both cases this is because we do not know what the value in the locked
  70. row is, so it is not "acceptable" to skip over it.  By using a unique key
  71. and specifying that key in the update, you can ususally avoid this problem.
  72.  
  73. >Just some questions:
  74. >1. Does this mean that the delete statement of user A in example 1 places
  75. >   an exclusive lock on the table?
  76.  
  77. No.
  78.  
  79. >2. Or does this mean that the delete statement (or select etc.) places
  80. >   locks on all rows (shared or exclusive locks) before (!) evaluating
  81. >   the WHERE clause? (I can't imagine that :-) )
  82.  
  83. That would be true if you had used an isolation level of REPEATABLE READ
  84. (whose purpose is to guarantee that the same rows would be returned for
  85. multiple executions of the query within a transaction, which requires the
  86. locking of all rows examined to generate the row set satisfying the query
  87. conditions).  Since you are using the default isolation level (COMMITTED
  88. READ) this would not be the case.
  89.  
  90. >   Or does this even mean that informix uses the "optimization" mentioned
  91. >   above to prevent exaclty that effect?
  92.  
  93. The optimization method I mentioned applied only when a REPEATABLE READ is 
  94. combined with a sequential scan of the table, which would require every row
  95. in the table to be locked; in such a case, a table lock is much more
  96. efficient.  That is NOT happening in this case (as explained above).
  97.  
  98. >3. Does this mean that I have to use an index as demonstrated in example 2
  99. >   to turn off the "optimization" with table locks in order to get row
  100. >   level locking?
  101.  
  102. Again, this would only apply to REPEATABEL READS.  In your case, though,
  103. indexes would be prudent (I'd venture to say necessary).  In general,
  104. you want to avoid sequential scans of the data.  General wisdom for SE
  105. used to be to avoid indexing "small" tables.  For OnLine this is really
  106. not applicable (for SE, it was to reduce physical i/o, but OnLine uses
  107. shared memory buffering, so it really isn't costly).  For SE, this is
  108. sometimes not a good strategy either, this case being a prime example.
  109. I would suggest using indexes on your tables where you are running into
  110. the problem you describe.
  111.  
  112. >4. If question 3 can be answered with yes, then why is my statement
  113. >   "Row level locking works only if you use an index on that table"
  114. >   completely false?
  115.  
  116. Besause row level locking does work even without indexes.  The problem
  117. lies not in the row level locking, but in how set generation has to work.
  118. The implication of your statement is that the product is somehow "broken"
  119. when in fact it is working correctly, but the access of the data by the
  120. application is not well thought out relative to how it works.
  121.  
  122. By the way, you can also (at least in most cases) avoid the error return by
  123. setting your lock mode to WAIT.
  124.  
  125.  
  126. Dave
  127. -- 
  128. Disclaimer: These opinions are not those of Informix Software, Inc.
  129. **************************************************************************
  130. "I look back with some satisfaction on what an idiot I was when I was 25,
  131.  but when I do that, I'm assuming I'm no longer an idiot." - Andy Rooney
  132.