我的查询存在性能问题。请提出建议

发布于 2024-12-06 19:26:43 字数 275 浏览 2 评论 0原文

MERGE INTO Analysis a
USING ( SELECT * FROM  Data ) b
ON ( a.User_Id = b.User_Id AND a.Upgrade_Flag = 0 )
WHEN MATCHED THEN
UPDATE SET Status = NVL(a.Status, 'ACTIVATE');

上面的查询工作正常。但是当存在数百万条记录时,这可能会导致性能问题。上面的查询是否有任何替代方案以获得更好的性能。请对此提出建议。感谢您的时间

MERGE INTO Analysis a
USING ( SELECT * FROM  Data ) b
ON ( a.User_Id = b.User_Id AND a.Upgrade_Flag = 0 )
WHEN MATCHED THEN
UPDATE SET Status = NVL(a.Status, 'ACTIVATE');

The above query works fine.But when millions of records are present this may lead to performance issue.Is there any alternative for the above query to get a better performace.Please suggest me on this.Thanks for your time

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

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

发布评论

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

评论(3

撧情箌佬 2024-12-13 19:26:43

如果没有看到执行计划,我无法预测替代方案是否会更有效。但我注意到您没有在更新中使用合并源,这表明这可以重写为一个简单的更新语句:

UPDATE Analysis a
SET Status = NVL(a.Status, 'ACTIVATE')
WHERE a.Upgrade_Flag = 0
AND a.User_Id IN (
  SELECT b.User_Id FROM Data b
)

与这些事情的情况一样,您可以选择使用 IN 子句,如我所示,或带有相关子查询的 EXISTS 子句。通常,在尝试调整性能时值得测试这两个选项,尽管至少在某些情况下优化器会自行尝试该转换。

Without seeing the execution plans I can't predict whether an alternative would be any more efficient. But I note that you are not using the merge source in the update, which indicates that this could rewritten as a simple update statement:

UPDATE Analysis a
SET Status = NVL(a.Status, 'ACTIVATE')
WHERE a.Upgrade_Flag = 0
AND a.User_Id IN (
  SELECT b.User_Id FROM Data b
)

As is always the case with these things, you have a choice between using an IN clause, as I have shown, or an EXISTS clause with a correlated subquery. Usually it's worth testing both options when trying to tune performance, although in at least some cases the optimizer will try that transformation on its own.

灯角 2024-12-13 19:26:43

这个查询对我来说看起来很好。也许你应该创建一些索引?

如果尚未创建,则为 Analysis.User_Id 创建索引,为 Data.User_Id 创建索引(主键自动创建索引)。

或者也可以为包含 User_Id 和 Upgrade_Flag 列的 Analysis 创建索引。

The query looks fine to me. Maybe you should create some indexes?

Create index for Analysis.User_Id and index for Data.User_Id if not yet created (primary key creates index automatically).

Or maybe also create index for Analysis containing both columns User_Id and Upgrade_Flag.

拥抱没勇气 2024-12-13 19:26:43

为什么使用 MERGE 而不是简单的 UPDATE 真的一点也不明显。

update analysis a
set a.status = 'ACTIVATE'
where a.status is null
and a.upgrade_flag = 0 
and a.user_id in ( select b.user_id from  data b )
/

假设 DATA.USER_ID 已建立索引,则仅从 DATA 中选择 USER_ID 而不是整个记录可以加快速度。

It's really not at all obvious why you are using MERGE instead of a simple UPDATE.

update analysis a
set a.status = 'ACTIVATE'
where a.status is null
and a.upgrade_flag = 0 
and a.user_id in ( select b.user_id from  data b )
/

Selecting only the USER_ID from DATA instead of the entire record could speed things up, presuming DATA.USER_ID is indexed.

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