代码之家  ›  专栏  ›  技术社区  ›  Moeb

RDBMS如何处理表更新?

  •  5
  • Moeb  · 技术社区  · 16 年前

    假设我有一个表和一个索引

    original simple table A
    ------------------------
    rowid  |  id   name
    123    |  1    A
    124    |  4    G
    125    |  2    R
    126    |  3    P
    
    index on A.id
    -------------
    id  rowid
    1   123
    2   125
    3   126
    4   124
    

    此时,我执行这个DML语句

    UPDATE A SET id = 5 WHERE id = 4
    

    执行此语句时会发生什么?

    a)

    BEGIN
    go to index
    search for `id == 4` (B tree index generally)
    find that `rowid = 124`
    go to that location
    update id in the table
    come back (? I am not sure)
    update the index
    END
    

    b)

    BEGIN
    go to index
    search for `id == 4` (B tree index generally)
    update the id value in index
    find that `rowid = 124`
    go to that location
    update id in the table
    END
    

    c)其他事情完全发生了

    因为这可能依赖于数据库本身, 在甲骨文中是如何发生的?

    2 回复  |  直到 9 年前
        1
  •  1
  •   Gary Myers    16 年前

    来自: http://jonathanlewis.wordpress.com/2006/11/22/tuning-updates/

    “如果Oracle使用(B-Tree)索引 找到要更新的数据, 推迟任何(B树)索引更新 直到更新结束, 然后对索引键(及其 rowids)之前和之后 对应用批量更新之前的值 索引“

    如果您进行了详细的跟踪,wait事件将显示IO的文件/块详细信息。从那里应该可以确定对象(使用dba扩展数据块)以及访问对象的顺序。

    这就是说,这是相当学术的,不应该影响你如何编码事情。

        2
  •  0
  •   dave    16 年前

    要查看特定更新语句的执行计划,请执行以下操作:

    1. 为会话打开跟踪
    2. 运行你的PL/SQL
    3. 对跟踪文件运行tkprof