<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx</link><description>Most tuning efforts for data-changing operations concentrate on the SELECT side of the query plan. Sometimes people will also look at important storage engine considerations like locking and transaction log throughput that can have dramatic effects. As</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47316</link><pubDate>Fri, 25 Jan 2013 23:30:31 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47316</guid><dc:creator>tobi</dc:creator><description>&lt;p&gt;&amp;quot;it does not have a sort specially optimized for index updates which could make a best effort to sort within the memory allocated, but never spill to disk. Perhaps I should suggest this on Connect :)&amp;quot;&lt;/p&gt;
&lt;p&gt;You should. I already had that thought before you mentioned it. It looks comparably easy to implement although it probably gets closed. Hopefully it lives on in their internal bug trackers.&lt;/p&gt;</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47317</link><pubDate>Sat, 26 Jan 2013 01:20:12 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47317</guid><dc:creator>Alejandro Mesa</dc:creator><description>&lt;p&gt;Paul,&lt;/p&gt;
&lt;p&gt;Thanks to you for sharing this with us. It was a pleasure reading, as always.&lt;/p&gt;
&lt;p&gt;--&lt;/p&gt;
&lt;p&gt;AMB&lt;/p&gt;</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47410</link><pubDate>Thu, 31 Jan 2013 12:38:57 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47410</guid><dc:creator>Hugo Kornelis</dc:creator><description>&lt;p&gt;Thanks for an interesting read, Paul!&lt;/p&gt;
&lt;p&gt;I do have to point out a correction, though. You present the three basic forms of update plan, but you don't mention that there are also mixed plans - plans where some indexes are updates on a per-row basis and others on a per-index basis.&lt;/p&gt;
&lt;p&gt;This also invalidates your comment that &amp;quot;if the update target has an indexed view defined on it, a wide update plan is the only choice&amp;quot;. A mixed plan is also possible. There will always be seperate operators for updating the index' clustered index (and possible nonclustered indexes built on top of that), but other indexes on the base table may be updated either per row or per index.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Hugo&lt;/p&gt;
</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47422</link><pubDate>Thu, 31 Jan 2013 19:03:40 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47422</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;@tobi,&lt;/p&gt;
&lt;p&gt;The more I think about it, the more I think of little complexities that might make a non-spilling sort a non-starter. I'm still thinking about it, I'll update the post if I do submit the suggestion.&lt;/p&gt;
&lt;p&gt;@AMB&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;@Hugo&lt;/p&gt;
&lt;p&gt;3,582 words in this post and you think it needs more detail? ;c)&lt;/p&gt;
&lt;p&gt;Yes ok, I'll add that distinction somewhere at some point.&lt;/p&gt;
&lt;p&gt;As an aside, I see you are from the 'per row' or 'per index' school of nomenclature. Out of curiosity, how would you describe the per-index and per-row update plan? I think I have now decided to use 'wide' versus 'narrow' only, reserving 'per-index' to describe only plans with a sort or eager spool where the difference is important.&lt;/p&gt;
</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47428</link><pubDate>Thu, 31 Jan 2013 22:22:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47428</guid><dc:creator>Hugo Kornelis</dc:creator><description>&lt;p&gt;Paul: I use the terms &amp;quot;per-index&amp;quot; and &amp;quot;per-row&amp;quot; because they enable me to remember which is which. For &amp;quot;wide&amp;quot; and &amp;quot;narrow&amp;quot;, I always have to think or look it up.&lt;/p&gt;
&lt;p&gt;That also solves the issue of having a mixed plan, because I can now point to specific operators or specific indexes and say that they are updated per row or per index. The terms &amp;quot;narrow&amp;quot; and &amp;quot;wide&amp;quot;, to me, only make sense in the context of an entire plan.&lt;/p&gt;
</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47429</link><pubDate>Thu, 31 Jan 2013 23:09:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47429</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;That's an interesting point, Hugo. I'll give it some more thought :)&lt;/p&gt;
</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47480</link><pubDate>Mon, 04 Feb 2013 21:12:07 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47480</guid><dc:creator>Aaron Morelli</dc:creator><description>&lt;p&gt;Paul,&lt;/p&gt;
&lt;p&gt;Great post as always! And it just so happens to open a door for a Q that has bothered me for some time... :-)&lt;/p&gt;
&lt;p&gt;Does SQL/SQLOS have any IO-reordering logic? For example, Unordered Prefetch would seem to result in a rapid-fire series of Async IOs that are only microseconds apart, and reordering IOs in page-order would be a &amp;quot;best effort&amp;quot; way of turning random into sequential. (Though of course, we are ultimately at the mercy of actual block order and the disk controller's ability to IO reorder). &lt;/p&gt;
&lt;p&gt;This would also seem to benefit prefetches on NL Joins, logical-order index scans, and maybe an access method or two that I'm not thinking of?&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;Aaron&lt;/p&gt;</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47486</link><pubDate>Tue, 05 Feb 2013 07:33:30 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47486</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Hi Aaron,&lt;/p&gt;
&lt;p&gt;There's no generalized reordering for unordered prefetch, but unordered prefetch on a Nested Loops join may have the 'optimized' property set in which case a best-effort batch sort of keys is performed. As far as I know, this does not happen in any other case.&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;
</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47493</link><pubDate>Tue, 05 Feb 2013 18:53:54 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47493</guid><dc:creator>Mark Freeman</dc:creator><description>&lt;p&gt;I vote with Hugo. Per-row and per-index are better terms, simply because they don't require an additional mental look-up. We need to optimize even the queries that run in our own heads. :-)&lt;/p&gt;</description></item><item><title>re: Optimizing T-SQL queries that change data</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/01/26/optimizing-t-sql-queries-that-change-data.aspx#47504</link><pubDate>Tue, 05 Feb 2013 22:43:28 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47504</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Thanks Mark, I've updated the main text to a form I'm comfortable with and at least partly based on the feedback.&lt;/p&gt;
</description></item></channel></rss>