70-465 · Question #42
You need to recommend a solution to improve the performance of usp.UpdateInventory. The solution must minimize the amount of development effort. What should you include in the recommendation?
The correct answer is A. A table variable. When optimizing stored procedures that perform iterative or batch updates, replacing cursors or inefficient row-by-row operations with table variables can significantly improve performance with minimal code changes.
Question
Options
- AA table variable
- BA common table expression
- CA subquery
- DA cursor
How the community answered
(67 responses)- A84% (56)
- B3% (2)
- C4% (3)
- D9% (6)
Why each option
When optimizing stored procedures that perform iterative or batch updates, replacing cursors or inefficient row-by-row operations with table variables can significantly improve performance with minimal code changes.
A table variable stores intermediate result sets in memory (tempdb), allowing set-based operations that replace slow row-by-row processing. Table variables have less locking overhead than temporary tables, require no explicit cleanup, and can be used to batch updates in usp.UpdateInventory with minimal refactoring, satisfying the requirement to minimize development effort.
A common table expression (CTE) is a named temporary result set scoped to a single statement and does not persist data between operations, making it unsuitable as a standalone performance improvement for a stored procedure performing inventory updates.
A subquery is evaluated inline within a single query and does not provide a mechanism to store or reuse intermediate result sets across multiple statements within the stored procedure, offering limited performance benefit for complex update logic.
A cursor processes rows one at a time (row-by-row), which is typically the cause of poor performance in stored procedures rather than a solution to it; replacing existing logic with a cursor would likely worsen performance.
Concept tested: Optimizing stored procedures using table variables
Source: https://learn.microsoft.com/en-us/sql/t-sql/data-types/table-transact-sql?view=sql-server-ver16
Topics
Community Discussion
No community discussion yet for this question.