【问题标题】:Why Simple Oracle Update Statement Make Block Session?为什么简单的 Oracle 更新语句生成块会话?
【发布时间】:2021-04-04 08:48:10
【问题描述】:

我有这样的简单 Oracle (19C) 更新语句,它有时会进行块会话。此更新查询从使用实体框架的 ASP.NET MVC 系统运行。由于我是这个问题的新手,任何人都可以告诉我如何找出确切的问题,可能是什么原因以及如何解决它?

UPDATE "TICKET_TRACKINGPRD"."TKT_TICKET"
SET
    "TITLE" = :b22,
    "SUMMARY" = :b21,
    "DESCRIPTION" = :b20,
    "ENVIRONMENT" = :b19,
    "TICKET_PROJECT_ID" = :b18,
    "TICKET_PROJECT_MODULE_ID" = NULL,
    "APPLICATION_TYPE_ID" = :b17,
    "APPLICATION_ID" = :b16,
    "APPLICATION_MODULE_ID" = :b15,
    "APPLICATION_SERVICE_ID" = :b14,
    "SERVICE_CHANNEL_ID" = :b13,
    "TICKET_STATUS_ID" = :b12,
    "TICKET_RESOLUTION_ID" = :b11,
    "TICKET_PRIORITY_ID" = :b10,
    "ASSIGNEE_ID" = :b9,
    "DUE_DATE" = :b8,
    "REPORTED_BY_USER_NAME" = :b7,
    "USER_LANGUAGE_ID" = :b6,
    "CLOSED_DATE" = NULL,
    "CLOSED_BY_USER_ID" = NULL,
    "CONTEXT_KEY" = NULL,
    "CONTEXT_URL" = NULL,
    "CREATED_DATE" = :b5,
    "MODIFIED_DATE" = :b4,
    "CREATED_BY_USER_ID" = :b3,
    "MODIFIED_BY_USER_ID" = :b2,
    "RECORD_STATUS_ID" = :b1
WHERE
    ("ID" = :b23)
RETURNING "KEY" INTO : o0

【问题讨论】:

  • ID是主键吗?
  • @APC 是 ID 是主键。

标签: oracle oracle11g oracle10g query-optimization oracle19c


【解决方案1】:

我并不是说这个“它”,但......可能是。

可能是什么原因

看起来像未索引的外键问题。基本上,所有外键列都应该被索引。正如documentation 所说:

为子表中的外键建立索引有以下好处:

  • 防止子表上的全表锁定。相反,数据库会在索引上获取行锁。

欲了解更多信息,请阅读Locks and foreign keys


我该如何解决?

显然,通过索引外键列。但是,首先你必须找到它们。我使用的是 Tom Kyte 的脚本(稍作调整):

/* 12.12.2012. Tom Kyte: locating unindexed foreign keys

   14.03.2016. PAR_WHAT: 0    - show those that aren't OK
                         1    - show those that are OK
                         NULL - show all
*/

WITH forkey
     AS (SELECT DECODE (b.table_name, NULL, '****', 'ok') Status,
                a.table_name,
                a.columns column_1,
                b.columns column_2
           FROM (  SELECT SUBSTR (a.table_name, 1, 30) table_name,
                          SUBSTR (a.constraint_name, 1, 30) constraint_name,
                             MAX (
                                DECODE (position,
                                        1, SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        2, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        3, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        4, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        5, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        6, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        7, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        8, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (position,
                                        9, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   10, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   11, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   12, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   13, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   14, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   15, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   position,
                                   16, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                             columns
                     FROM user_cons_columns a, user_constraints b
                    WHERE     a.constraint_name = b.constraint_name
                          AND b.constraint_type = 'R'
                 GROUP BY SUBSTR (a.table_name, 1, 30),
                          SUBSTR (a.constraint_name, 1, 30)) a,
                (  SELECT SUBSTR (table_name, 1, 30) table_name,
                          SUBSTR (index_name, 1, 30) index_name,
                             MAX (
                                DECODE (column_position,
                                        1, SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        2, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        3, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        4, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        5, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        6, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        7, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        8, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (column_position,
                                        9, ', ' || SUBSTR (column_name, 1, 30),
                                        NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   10, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   11, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   12, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   13, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   14, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   15, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                          || MAX (
                                DECODE (
                                   column_position,
                                   16, ', ' || SUBSTR (column_name, 1, 30),
                                   NULL))
                             columns
                     FROM user_ind_columns
                 GROUP BY SUBSTR (table_name, 1, 30),
                          SUBSTR (index_name, 1, 30)) b
          WHERE     a.table_name = b.table_name(+)
                AND b.columns(+) LIKE a.columns || '%'
                AND a.table_name NOT LIKE 'HEP_DP%')
  SELECT f.status,
         f.table_name,
         f.column_1,
         f.column_2
    FROM forkey f
   WHERE f.status =
            CASE
               WHEN :par_what = 1 THEN 'ok'
               WHEN :par_what = 0 THEN '****'
               ELSE f.status
            END
ORDER BY f.table_name, f.column_1, f.column_2
/

例如:

SQL> create table a (id number primary key);

Table created.

SQL> create table b (id number constraint fk_ba references a (id));

Table created.

B 缺少其外键约束列上的索引,因此查询返回:

SQL> /

STATUS     TABLE_NAME COLUMN_1   COLUMN_2
---------- ---------- ---------- ----------
****       B          ID

创建索引:

SQL> create index a1bid on b (id);

Index created.

查询现在不返回任何内容:

SQL> /

no rows selected

看看有没有帮助。

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    相关资源
    最近更新 更多