UPDATE [User] SET SellerRating = SellerRating + {0} WHERE Id='{1}';
SELECT SellerRating FROM [User] WHERE Id='{1}'", rating, toUserId);
当使用 SQLCommand 对象对上述查询执行标量时,
SQL Server 是否知道他手中有第二个查询的值?
还是会再次开始搜索索引?
它怎么知道?
感谢您的时间
UPDATE [User] SET SellerRating = SellerRating + {0} WHERE Id='{1}';
SELECT SellerRating FROM [User] WHERE Id='{1}'", rating, toUserId);
当使用 SQLCommand 对象对上述查询执行标量时,
SQL Server 是否知道他手中有第二个查询的值?
还是会再次开始搜索索引?
它怎么知道?
感谢您的时间
In fact, just to be clear, the return for the SELECT may not be the same as the rows updated in the UPDATE statement.
Between the two statements, another transaction may update, insert, or delete rows in the table. As a general approach, SQL has to evaluate the second SELECT to take such changes in the data into account.
Of course, there are caveats, which are database- and configuration- specific. These statements could be inside a transaction that has write locks on the table, for instance. But, in general, the second statement has to be evaluated.
What does change, though, is that the data touched by the UPDATE statement may still be in the page cache. If so, then the SELECT should run very quickly, since there would be no cache misses.
数据库操作彼此独立,因此不,SQL Server 不会考虑您有两个查询涉及相同行的事实。
此外,您需要参数化您的查询。
SQL Server 是否知道他手中有第二个查询的价值?还是会再次开始搜索索引?
世界上几乎所有受人尊敬的数据库都会 1)再次搜索并2)在缓存中找到行。因此,这两个问题的答案都是肯定的。我认为您缺少重要的知识,您必须阅读有关缓冲池及其工作原理的信息。