oracle - 如何在PL/SQL包中将 session 变量skip_unusable_indexes设置为true以加速表删除/插入?

标签 oracle session plsql indexing

我正在尝试加快通过 PL/SQL 存储过程控制的数据加载速度。我已以编程方式将要刷新的表的索引更改为不可用。我希望 Oracle 忽略这些不可用的索引。我可以发表声明:

ALTER SESSION SET skip_unusable_indexes = TRUE

但我随后收到错误:

ORA-01502: index 'MY_INDEX_NAME' or partition of such index is in unusable state

所以它似乎忽略了我的更改 session 。

我可以在 PL/SQL 包中更改 session 吗?如果没有,我的替代方案是什么? 我还可以如何禁用(设置不可用)索引来加速加载?

一个有点相关的问题here .

最佳答案

您是否在存储过程正在使用的同一 session 中发出 ALTER SESSION 语句?或者 ALTER SESSION 是在单独的 session 中执行的吗?

您可以使用动态 SQL 将 ALTER SESSION 嵌入到 PL/SQL 中,即

BEGIN
  EXECUTE IMMEDIATE 'ALTER SESSION SET skip_unusable_indexes = TRUE';

  <<more code>>
END;

某些索引是否唯一(或用于强制执行唯一约束)?作为skip_unusable_indexes documentation

Note: If an index is used to enforce a UNIQUE constraint on a table, then allowing insert and update operations on the table might violate the constraint. Therefore, this setting does not disable error reporting for unusable indexes that are unique.

如果是这种情况,您可以禁用约束和/或将索引更改为非唯一吗?

唯一索引和非唯一索引之间差异的快速示例。请注意,当您有不可用的唯一索引时,skip_unusable_indexes 不会像有不可用的非唯一索引时那样抑制 ORA-01502 错误。

SQL> create table a (
  2    col1 number
  3  );

Table created.

SQL> create unique index idx_a on a( col1 );

Index created.

SQL> insert into a values( 1 );

1 row created.

SQL> commit;

Commit complete.

SQL> alter index idx_a unusable;

Index altered.

SQL> insert into a values( 2 );
insert into a values( 2 )
*
ERROR at line 1:
ORA-01502: index 'SCOTT.IDX_A' or partition of such index is in unusable state


SQL> alter session set skip_unusable_indexes = true;

Session altered.

SQL> insert into a values( 2 );
insert into a values( 2 )
*
ERROR at line 1:
ORA-01502: index 'SCOTT.IDX_A' or partition of such index is in unusable state


SQL> drop index idx_a;

Index dropped.

SQL> create index idx_a_nonunique on a( col1 );

Index created.

SQL> alter index idx_a_nonunique unusable;

Index altered.

SQL> insert into a values( 2 );

1 row created.

关于oracle - 如何在PL/SQL包中将 session 变量skip_unusable_indexes设置为true以加速表删除/插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/187988/

相关文章:

database - 如何使用 desc 顺序创建索引组织表

sql - 甲骨文 SQL : SQL join with group by (count) and having clauses

python:与cgi脚本中的 session 交互

session - 我的 SessionVar 对象应该在哪里?

oracle - 如何循环访问 oracle pl/sql 游标中的列

sql - 验证 dbms_sql.execute 执行的代码 PL/SQL

sql - 从SQL中的一个左属性中获取多个属性

sql - Oracle找到两个时间戳记的平均值

javascript - isset/空 php session (if else 语句)给出 undefined index

oracle - 附加到 Oracle 用户定义的集合类型