如果Oracle表有数据,我们应该在哪个顺序上应用主密钥,外键约束并创建索引?

发布于 2025-01-22 12:15:49 字数 145 浏览 2 评论 0 原文

1.在哪个顺序中,我们应该应用主键,外键约束并创建索引,如果Oracle表具有数百万个数据并且没有先前的约束?

2.在应用(创建)索引时应用主密钥和外键约束时,我们可以使用“ Noging Parallel”?还是任何其他方法,以便更快地应用主密钥和外键约束?

1.In which order should we apply primary key, foreign key constraints and create index if the Oracle table has millions of data and does not have prior constraints?

2.Can we use 'NOLOGGING PARALLEL' while applying primary key and foreign key constraints like we do while applying(creating) indexes? Or any other method so that primary key and foreign key constraints could be applied faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2025-01-29 12:15:49

注意:我将使用子弹,以便更容易阅读,因为很容易在长句子中迷失。

我对这个主题的想法;看看这是否有帮助。


好吧,

  • 列,它不是主键的一部分或唯一键
  • 如果

由于您无法创建外键约束,

  • 一部分约束,
  • Oracle会自动创建支持它的索引,除非您已经有一个可以使用索引(使用使用索引子句),
  • 这意味着您可以“跳过”某些索引(这些索引(对于主要键约束时,它们'll已经存在)和实际上节省一些时间
  • 并创建“其他”索引

  • 如果您首先创建了 unique indure Future> Future 主键列,
  • 然后使用子句添加的主键约束,Oracle将“跳过”检查可能的重复值,因为唯一的索引不允许它们

相同,因为

  • not not null Future 主键列的约束;主键不允许 null s so-如果列已经 而不是null ,则执行主密钥约束可以跳过 null 检查

我不知道

  • 您会额外索引哪些列,但是 - 当您在Oracle 11g上时 -
  • 不要忘记为所有外键约束列索引
  • ,因为您可能会遇到意外桌子锁,如果您
    • 在父表中更新主键列,或
    • 删除父记录

您可以在没有日志记录的情况下进行操作吗?是的:

SQL> create table test (id number, name varchar2(20));

Table created.

SQL> create unique index ui1_test_id on test (id) nologging parallel 20;

Index created.

SQL> alter table test add constraint pk_test primary key (id) using index ui1_test_id nologging parallel 20;

Table altered.

SQL>

但是您可能需要将索引和表更改回 noparallel 记录完成初始创建后。

SQL> alter index ui1_test_id noparallel;

Index altered.

SQL> alter table test logging noparallel;

Table altered.

Note: I'll use bullets so that it is easier to read, as it is easy to get lost in long sentences.

My thoughts on the subject; see if anything of this helps.


Well,

  • as you can't create a foreign key constraint if column(s) it references aren't part of primary or unique key
  • you'll obviously first have to create primary key constraints
  • and then foreign key constraints

When you

  • create a primary key constraint,
  • Oracle automatically creates index that supports it, unless there's already an index you can use (with the USING INDEX clause)
  • which means that you can "skip" some indexes (those for primary key constraints as they'll already exist) and virtually save some time
  • and create "other" indexes

On the other hand,

  • if you first create unique index on future primary key columns and
  • later add primary key constraint with the USING INDEX clause, Oracle will "skip" check for possible duplicate values because unique index won't allow them

The same goes for

  • NOT NULL constraint on future primary key columns; primary key doesn't allow NULLs so - if a column already is NOT NULL, enforcing primary key constraint can skip NULL check as well

I don't know

  • which columns you'll additionally index, but - as you're on Oracle 11g -
  • don't forget to index all foreign key constraint columns
  • because you might encounter unexpected table locks if you
    • update primary key column in parent table, or
    • delete parent record

Can you do it with no logging and in parallel? Yes:

SQL> create table test (id number, name varchar2(20));

Table created.

SQL> create unique index ui1_test_id on test (id) nologging parallel 20;

Index created.

SQL> alter table test add constraint pk_test primary key (id) using index ui1_test_id nologging parallel 20;

Table altered.

SQL>

But you'll probably want to change the index and table back to NOPARALLEL and LOGGING when the initial creation is done.

SQL> alter index ui1_test_id noparallel;

Index altered.

SQL> alter table test logging noparallel;

Table altered.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文