Using Mview over tables having FKs - Consideration

主外键在数据库的应用当中比较常见,它就像一把双刃剑,用得好的话会给你的系统带来坚固的约束关系。用得不好或者滥用的话会给维护和排错带来非常大的麻烦。

最近刚好接到一起数据复制的需求,主库和MV库面向的应用类型差不多,MV库上面连接的应用需要访问主库的一张表,现状是有专门的人员负责对这份数据进行写入,比较繁琐,所以考虑使用MV的方式来进行数据的共享。

其中要求同步的是一张表,但是这张表在备库有被其他表的FK关联到,经过调查,相关联的几个表在主库也是存在的,并且具有同样的约束条件。

考虑到主外键的约束关系如果单独同步可能导致违反约束,所以的话有主外键约束的表必须放在一个事务进行同步。

经过了解,实际上这几个表的数据都是从主库来的,并且可以放在一起同步。总算可以继续做下去。

那么在MV节点需要对这几个关联到的约束进行属性调整:

1. 初始约束校验,后续约束校验。

2. 开启允许延缓检查属性。

3. 调整为事务结束校验。

(从非延缓到延缓需要重建约束)

联机文档原文:

DEFERRABLE Clause The DEFERRABLE and NOT DEFERRABLE parameters indicate whether or not, in subsequent transactions, constraint checking can be deferred until the end of the transaction using the SET CONSTRAINT(S) statement. If you omit this clause, then the default is NOT DEFERRABLE.

  • Specify NOT DEFERRABLE to indicate that in subsequent transactions you cannot use theSET CONSTRAINT[S] clause to defer checking of this constraint until the transaction is committed. The checking of a NOT DEFERRABLE constraint can never be deferred to the end of the transaction.

    If you declare a new constraint NOT DEFERRABLE, then it must be valid at the time theCREATE TABLE or ALTER TABLE statement is committed or the statement will fail.

  • Specify DEFERRABLE to indicate that in subsequent transactions you can use the SETCONSTRAINT[S] clause to defer checking of this constraint until after the transaction is committed. This setting in effect lets you disable the constraint temporarily while making changes to the database that might violate the constraint until all the changes are complete.

You cannot alter the deferrability of a constraint. That is, whether you specify either of these parameters, or make the constraint NOT DEFERRABLE implicitly by specifying neither of them, you cannot specify this clause in an ALTER TABLE statement. You must drop the constraint and re-create it.

INITIALLY Clause The INITIALLY clause establishes the default checking behavior for constraints that are DEFERRABLE. The INITIALLY setting can be overridden by a SETCONSTRAINT(S) statement in a subsequent transaction.

  • Specify INITIALLY IMMEDIATE to indicate that Oracle should check this constraint at the end of each subsequent SQL statement. If you do not specify INITIALLY at all, then the default is INITIALLY IMMEDIATE.

    If you declare a new constraint INITIALLY IMMEDIATE, then it must be valid at the time the CREATE TABLE or ALTER TABLE statement is committed or the statement will fail.

  • Specify INITIALLY DEFERRED to indicate that Oracle should check this constraint at the end of subsequent transactions.

This clause is not valid if you have declared the constraint to be NOT DEFERRABLE, because aNOT DEFERRABLE constraint is automatically INITIALLY IMMEDIATE and cannot ever beINITIALLY DEFERRED.

ENABLE Clause Specify ENABLE if you want the constraint to be applied to the data in the table.

If you enable a unique or primary key constraint, and if no index exists on the key, then Oracle Database creates a unique index. Unless you specify KEEP INDEX when subsequently disabling the constraint, this index is dropped and the database rebuilds the index every time the constraint is reenabled.

You can also avoid rebuilding the index and eliminate redundant indexes by creating new primary key and unique constraints initially disabled. Then create (or use existing) nonunique indexes to enforce the constraint. Oracle does not drop a nonunique index when the constraint is disabled, so subsequent ENABLE operations are facilitated.

  • ENABLE VALIDATE specifies that all old and new data also complies with the constraint. An enabled validated constraint guarantees that all data is and will continue to be valid.

    If any row in the table violates the integrity constraint, the constraint remains disabled and Oracle returns an error. If all rows comply with the constraint, Oracle enables the constraint. Subsequently, if new data violates the constraint, Oracle does not execute the statement and returns an error indicating the integrity constraint violation.

    If you place a primary key constraint in ENABLE VALIDATE mode, the validation process will verify that the primary key columns contain no nulls. To avoid this overhead, mark each column in the primary key NOT NULL before entering data into the column and before enabling the primary key constraint of the table.

  • ENABLE NOVALIDATE ensures that all new DML operations on the constrained data comply with the constraint. This clause does not ensure that existing data in the table complies with the constraint and therefore does not require a table lock.

If you specify neither VALIDATE nor NOVALIDATE, the default is VALIDATE.

If you change the state of any single constraint from ENABLE NOVALIDATE to ENABLE VALIDATE, the operation can be performed in parallel, and does not block reads, writes, or other DDL operations.

Restriction on the ENABLE Clause You cannot enable a foreign key that references a disabled unique or primary key.

MV的操作就不用说了,把关联的MV放到一个刷新组搞定。

时间: 2024-10-28 06:28:21

Using Mview over tables having FKs - Consideration的相关文章

Heaps of data: tables without clustered indexes

If you create a table on Adaptive Server, but do not create a clustered index, the table is stored as a heap. The data rows are not stored in any particular order. This section describes how select, insert, delete, and update operations perform on he

InnoDB 中文参考手册 --- InnoDB Tables 概述

参考|参考手册|中文 InnoDB 中文参考手册 --- 犬犬(心帆)翻译 1 InnoDB Tables 概述 InnoDB 给 MySQL 提供了具有事务(commit).回滚(rollback)和崩溃修复能力(crash recovery capabilities)的事务安全(transaction-safe (ACID compliant))型表.InnoDB 提供了行锁(locking on row level),提供与 Oracle 类型一致的不加锁读取(non-locking re

hdu 1213 How Many Tables ([kuangbin带你飞]专题五 并查集)

点击打开链接 C - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know

mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES

AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES错误,具体内容如下所示 [root@DB-Server ~]# /usr/bin/automysqlbackup /etc/automysqlbackup/myserver.conf Pars

如何利用DataSet.Tables 获取想要的参数

问题描述 小弟请教:帮朋友程序加2个页面这2个页面是从数据库查询一些需要的数据给前台分页显示如图:后台页面获取数据集合代码如下:BLL.OrderCarddal=newOrderCard();DataSetpageData=dal.PageSearch(listParam,20,1,orderby);this.rptOrders.DataSource=pageData.Tables[1];this.rptOrders.DataBind();获取数据库数据集合如下:如何提取比如:神州行卡统计数据如

c# winform sql-MyDataSet.Tables[0].Rows.Count =0

问题描述 MyDataSet.Tables[0].Rows.Count =0 conn.Open();//打开数据库 //用于填充DataSet和更新SQL数据库及其一个连接 SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlStrconn); //定义一个缓存 DataSet MyDataSet = new DataSet(); //清空缓存 MyDataSet.Clear(); ------这个地方就为0 //将MyAdapeter获取的数据添

HDU1050-Moving Tables

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19290    Accepted Submission(s): 6585 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

show tables能看到表却无法读写?

MySQL版本 5.7.14(社区版源码基础上增加一些debug代码) 在MySQL使用innodb的时候我们有时候会看到如下报错: ERROR 1146 (42S02): Table 'test.test1bak' doesn't exist 首先总结下原因: 缺少frm文件: innodb数据字典不包含这个表. 我们重点讨论情况2,因为情况1是显而易见的. 在使用innodb存储引擎的时候某些时候我们show tables能够看到这个表,但是如果进行任何操作会报错如下: mysql> sho

MySQL can only use 61 tables in a join

问题描述 MySQL can only use 61 tables in a join Too many tables; MySQL can only use 61 tables in a join.怎么解决 解决方案 mysql最多在一个查询中join 61个表.你的查询太复杂了.你怎么会用那么多表? 解决方案二: 没办法增加,只能从优化SQL语句上入手了. 解决方案三: 在mysql中一个sql连接查询最多只能有61张表,你应该考虑拆散.临时表等优化sql.在设计上面层面考虑!! 解决方案四