Jun 15 2011

Rebind and Rewind in Execution Plans

Ever looked at an execution plan? If you answered no, you can’t possibly have ever tried to tune a query, or, you’re doing it wrong. For every one else, no doubt you’ve looked at the tool tips or the property sheets of an operator and you’ve seen the Rebind & Rewind properties and wondered what the heck they mean. Me too.

I learned as much as I could for the book on execution plans and I spent two pages describing it. Then, a little while ago, on the SQL Cruise, someone asked me to describe them and I was flummoxed. Specifically they said the explanation in the book was insufficiently clear, so I promised to put together a blog post on the topic, both to attempt to clarify my explanation and to reacquaint myself with the topic.

These properties reflect the number of times an operator was initialized, or started. It’s that easy for the initial definition. But it gets complicated quickly. Rebind & Rewind are only applicable when dealing with a loop join. They don’t apply to other situations, which is why they’re mostly zero when you see them. They are only applicable to the inner side of the loop.

Not only are Rebind and Rewind only involved with loop joins, but they only apply to certain operators: Nonclustered Index Spool, Remote Query, Row Count Spool, Sort, Table Spool, and Table-valued Function.

The difference between a rebind and rewind has to do with the values that are part of the loop join itself. As those values change, as different data is accessed in the outer part of the loop, you see rebinds. When those values aren’t changed then you’ll see a rewind. You’ll also see that the aggregation of these values is equal to the number of rows in the outer join.

Here’s an example query:

[sourcecode language=”sql”]SELECT  sod.SalesOrderDetailID
FROM    Sales.SalesOrderDetail AS sod
WHERE   LineTotal < (SELECT AVG(dos.LineTotal)
FROM   Sales.SalesOrderDetail AS dos
WHERE  dos.ModifiedDate < sod.ModifiedDate
)[/sourcecode]

It puts out an execution plan that’s pretty ugly, but at least it shoes a table spool in a loop join as you can see:

image

If you look at the properties then we can see the values showing multiple rebinds and rewinds that total up to the number of rows in the outer part of the loop.

image

That’s it in a nutshell. Hopefully I was able to clarify some from the original book. The book is actively undergoing an update and I’ll try to clarify that section further. For more on this topic, I recommend you look at this article by Fabiano Amorim (blog|twitter).

8 Comments

  • By Jim Murphy, June 15, 2011 @ 10:19 am

    Oh. That is a better explanation than the Tech-Ed style explanation in your book. So we can sort of get a feel for an indexes selectivity by comparing the differences between Rebinds and Rewinds (or we can interrogate the index itself).

    I’m still unclear about what to do about RB and RW’s. Why do they impact my life, except as a row count indicator and possibly indicating how many rows have unique data values.

  • By Grant Fritchey, June 15, 2011 @ 10:55 am

    Well, it shows how well the loop is working for you. If I understand it correctly (always an issue), you should generally see few rebinds & more rewinds for a data set that is appropriately using loops. I’ll have to experiment with that some more (in my copious spare time).

  • By alzdba, June 15, 2011 @ 11:19 am

    Since RB/RW only comes in with spooled conditions – as you stated – that means these (worker)objects are being materialized in tempdb.
    IMO this may actually be one important conclusion to add to the clarification.
    Thanks again for the elaboration.

  • By Fabiano Amorim, July 14, 2011 @ 10:44 am

    Hey grant, tks for the indication… Just a note my twitter is @mcflyamorim not @FabianoAmorim2 :-)

    Rebind/Rewinds are a mess on tolltips… The important thing about it is, you should have more rewinds than rebinds, yes you were correct, but in case of an Index Spool, this works diferent.
    Index Spools keeps the cached (rebinded) value in his cache, so when a diferent value from previews one is searched, the rewind occurs, but tooltips shows a rebind… That’s confuse I know… but I think you have more details on my article…

    Another important thing is, sometimes you can optimize the number of rewinds by ordering the resultset, this will sort the values sequential, making more rewinds instead of rebinds…

    Regards

  • By Yuri Makassiouk, September 4, 2011 @ 8:18 pm

    Thank you for the clarification. Why do you think a Sort operator would usually show
    Actual Rebinds = 1
    I doubt it is important, but I’m curious nonetheless. Should it be attributed to the “messy” (quoting mr. Amorim) implementation of the counters?

  • By Grant Fritchey, September 5, 2011 @ 9:51 am

    You’re going to see counts of 1 for operators as part of initialization. It’s after that when things get tricky. And yeah, Fabiano has it nailed better than I do.

Other Links to this Post

  1. Something for the Weekend – SQL Server Links 17/06/11 — June 17, 2011 @ 6:46 am

  2. Technical: Microsoft – SQL Server – Data I/O – Computed Columns | Daniel Adeniji's – Learning in the Open — January 17, 2014 @ 6:01 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment