Tracnsact SQL 获取性能

发布于 2024-11-07 16:59:04 字数 377 浏览 2 评论 0原文

您好,我想创建一个存储过程,它将连接 2 个表并检查一些要更新的值。 我们将使用 FETCH 对每条记录进行更新过程。

但我们正在考虑性能。使用 FETCH 是一个坏主意吗?

示例:

表1 图书编号 书名

表2 评级ID 书名 BookID

我们将像这样连接这两个表。

SELECT * FROM Table2
LEFT JOIN Table1 ON Table2.BookName = Table1.BookName 
WHERE Table2.BookID = 0

对于此查询提取的每条记录,我们将进行更新以将 Table1 的 bookID 设置为 Table2 的 BookID 列。

Hi i want to make a stored procedure that will join 2 table and will check some values to update.
We will use FETCH to make a update process for each record.

But we are considering performance. Is this a bad idea to use FETCH?

Example:

Table1
BookID
BookName

Table2
RatingID
BookName
BookID

We will join this two tables like this.

SELECT * FROM Table2
LEFT JOIN Table1 ON Table2.BookName = Table1.BookName 
WHERE Table2.BookID = 0

For each record which this query pulls, we will make an UPDATE to set Table1's bookID to Table2's BookID column.

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

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

发布评论

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

评论(2

风吹雪碎 2024-11-14 16:59:04

是的。

您几乎应该始终努力避免在 SQL Server 中使用游标,而使用基于集的替代方案。

UPDATE 语句确实支持 JOIN 到其他表。

Yes.

You should nearly always strive to avoid cursors in SQL Server and use set based alternatives instead.

The UPDATE statement does support JOINs to other tables.

装迷糊 2024-11-14 16:59:04

(和往常一样)马丁说的话。

另外,根据您的描述,更新看起来像这样(对于 SQL Server):

UPDATE Table1
 set BookID = t2.BookID
 from Table1 t1
  inner join Table2 t2
   on t2.BookName = t1.BookName
 where T2.BookID = 0
  • 内部联接,因为 where 子句;当 Table2.BookID = 0 时,Table2 行与联接不匹配,但无论如何都包含在外部联接中,将被丢弃,因为它们的 BookID 将始终为 NULL
  • 类似地设置 Table1 的 BookId Table2 的 BookId 将始终将其设置为 0,因为 where 子句表示“where Table2.BookId = 0”。您可能想重新考虑一下......?

(As usual) What Martin said.

Also, based on your description, the update would look something like this (for SQL Server):

UPDATE Table1
 set BookID = t2.BookID
 from Table1 t1
  inner join Table2 t2
   on t2.BookName = t1.BookName
 where T2.BookID = 0
  • Inner joins, becasue of the where clause; with Table2.BookID = 0, Table2 rows not matched by the join but included anyway by the outer join will be discarded, as their BookID will always be NULL
  • Similarly setting Table1's BookId to Table2's BookId will always set it to 0, because the where clause says "where Table2.BookId = 0". You might want to rethink this...?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文