Tracnsact SQL 获取性能
您好,我想创建一个存储过程,它将连接 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。
您几乎应该始终努力避免在 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 supportJOIN
s to other tables.(和往常一样)马丁说的话。
另外,根据您的描述,更新看起来像这样(对于 SQL Server):
Table2.BookID = 0
时,Table2 行与联接不匹配,但无论如何都包含在外部联接中,将被丢弃,因为它们的 BookID 将始终为 NULL(As usual) What Martin said.
Also, based on your description, the update would look something like this (for SQL Server):
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