将数据从一个表更新到另一个表(在数据库中)
数据库大师,
我希望有人能给我指明正确的方向。
我有两张桌子。表 A 和表 B。当系统启动时,表 A 中的所有条目都会被处理并复制到表 B(根据表 B 的架构)。表 A 可以有数万行。
当系统启动时,表 B 通过数据库更改通知与表 A 保持同步。
如果系统重新启动,或者我的服务重新启动,我想重新初始化表 B。但是,我想用尽可能少的数据库更新来完成此操作。具体来说,我想要:
- 添加表 A 中但不在表 B 中的任何行,并
- 删除表 A 中不存在但在表 B 中的任何行。
- 表 A 和表 B 共有的任何行都应该是现在
,我不是一个“数据库人”,所以我想知道执行此操作的传统方法是什么。
DB gurus,
I am hoping someone can set set me on the right direction.
I have two tables. Table A and Table B. When the system comes up, all entries from Table A are massaged and copied over to Table B (according to Table B's schema). Table A can have tens of thousands of rows.
While the system is up, Table B is kept in sync with Table A via DB change notifications.
If the system is rebooted, or my service restarted, I want to re-initialize Table B. However, I want to do this with the least possible DB updates. Specifically, I want to:
- add any rows that are in Table A, but not in Table B, and
- delete any rows that are not in Table A, but are in Table B
- any rows that are common to Table A and Table B should be left untouched
Now, I am not a "DB guy", so I am wondering what is conventional way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
exists
将处理量保持在最低限度。按照这些思路进行修改,以便连接正确(还要验证我没有做一些愚蠢的事情并从您的描述中向后获取
TableA
和TableB
):Use
exists
to keep processing to a minimum.Something along these lines, modified so the joins are correct (also verify that I didn't do something stupid and get
TableA
andTableB
backwards from your description):Informix 的企业复制功能将为您完成这一切。 ER 的工作原理是将逻辑日志从一台服务器传送到另一台服务器,然后在辅助服务器上将它们前滚。
您可以根据需要将其配置为细粒度(即只有少数表)。
您使用术语“数据库更改通知” - 您是否已经在使用 ER 或者这是某种基于触发器的安排?
如果由于某种原因 ER 无法适合您的配置,我建议重写通知模型以异步运行,即:
因此服务器“B”负责确保其副本与“A”同步。 “B”不可用并不会给“A”带来不便。
Informix's Enterprise Replication features would do all this for you. ER works by shipping the logical logs from one server to another, and rolling them forward on the secondary.
You can configure it to be as finely-grained as you need (ie just a handful of tables).
You use the term "DB change notifications" - are you already using ER or is this some trigger-based arrangement?
If for some reason ER can't work for your configuration, I would suggest rewriting the notifications model to behave asynchronously, ie:
So Server 'B' is responsible for ensuring its copy is in sync with 'A'. 'A' is not inconvenienced by 'B' being unavailable.
一种简单的方法是使用历史表,您可以在其中放置自上次更新以来发生的 A 更改,并使用该表同步表 B,而不是从 A 直接复制到 B。同步完成后,您删除整个历史表并重新开始。
我不明白的是,如果您的服务或计算机没有运行,如何更新表 A 而不是表 B。它们是否在 2 个不同的数据库或服务器上找到?
A simple way would be to use a historic table where you would put the changes from A that happened since the last update, and use that table to sync the table B instead of a direct copy from A to B. Once the sync is done, you delete the whole historic table and start anew.
What I don't understand is how table A can be update and not B if your service or computer is not running. Are they found on 2 different database or server?
根据公共列连接两个表中的数据,这会给出两个表中都有匹配的行,即 A 和 B 中的数据。然后使用该值(我们称之为集合 M)进行集合操作,即集合减运算以获得差异。
第一个要求:A减去M
第二个要求:B减去A
第三个要求:M
你明白了吗?
Join data from both tables according to comon columns and this gives you the rows that have a match in both tables, i.e. data in A and in B. Then use this values (lets call this set M) with set operations, i.e. set minus operations to get the differences.
first requirement: A minus M
second requrement: B minus A
third requirement: M
Do you get the idea?
我是一个 Sql Server 人员,但从 Sql Server 2008 开始,对于这种操作,可以使用功能调用
MERGE
。通过使用 MERGE 语句,我们可以在单个语句中执行插入、更新和删除操作。
所以我用谷歌搜索,发现
Informix 也支持相同的
MERGE 语句,但我不确定它是否也处理删除,尽管正在插入和更新照顾好了。此外,该语句自行处理事务I am a Sql Server guy but since Sql Server 2008, for this kind of operation , a feature call
MERGE
is available.By using MERGE statement we can perform insert, update and delete operations in a single statement.
So I googled and found that
Informix also supports the same
MERGE statement but I am not sure whether it takes care of delete too or not though insert and update is being taken care off. Moreover, this statement takes care of transaction by itself