使用远程数据库数据更新本地数据库
我在 Oracle 查询方面遇到了一些问题
create or replace
PROCEDURE "LOAD_USERNAME"
IS
cursor usu is
select userid
from local_user;
BEGIN
for usu_rec in usu
loop
update loc_user set username =(
select cod_user
from (
select cod_user, row_number() over (order by date_createad desc) r
from user_table@DBLINK where cod_person=usu_rec.userid
)
where r = 1
)
where externaluserid=usu_rec.userid;
end loop;
END;
基本上,尝试从其他数据库获取用户(最后创建的)的代码并更新本地表。这似乎有效,但我花了太多时间。我只能通过DBLINK检查远程数据库。
拜托,我需要一些帮助以获得更好的方法来做到这一点。
我预先感谢您的帮助。
I got a bit of a problem with a Oracle query
create or replace
PROCEDURE "LOAD_USERNAME"
IS
cursor usu is
select userid
from local_user;
BEGIN
for usu_rec in usu
loop
update loc_user set username =(
select cod_user
from (
select cod_user, row_number() over (order by date_createad desc) r
from user_table@DBLINK where cod_person=usu_rec.userid
)
where r = 1
)
where externaluserid=usu_rec.userid;
end loop;
END;
Basically, trying to get code of a user(last one created) from other database and update a local table. This seems to work but I takes too much time. I can only check the remote database through a DBLINK.
Please, I want some help for a better way to do this.
I thank your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望尽量减少通过网络的次数。因此,您应该加入驱动光标中的远程表并将用户名拉回那里。这会更好,因为该查询只执行一次(索引/设计将决定它的运行情况)。但您的更新将仅适用于本地数据。
编辑:删除了我的 PL/SQL,因为 @Aitor 的更好
You want to minimise the number of times you go over the network. So you should join to the remote table in your driving cursor and pull the username back there. This will be better as that query is only executed once (indexing/design will determine how well it goes). But your updates will then only be working with local data.
Edit: Removed my PL/SQL as @Aitor's was better
正如索德韦德所说,最好将连接放在光标中。您可以尝试这样的操作:
如果您必须加载大量更新,您可以尝试在游标中使用批量收集/为所有方法。
As Sodved said, is better to had the join in your cursor. You can try something like this:
If you have to load massive updates, you can try a bulk collect/for all approach in the cursor.
Oracle 已经为几个主要版本提供了此类功能的内置功能。如果您使用的是较旧的数据库,则应该使用 复制。在较新的版本中,此功能已被弃用,取而代之的是 Streams< /a>.
Oracle has provided built-in functionality for this sort of thing for several major versions. If you're on an older database you should use replication. In more recent versions this has been deprecated in favour of Streams.