Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Both your second and third examples are valid. Here are a few points:</p> <ol> <li>In your second query, it is not neccessary to use <code>DISTINCT</code> that will simply add overhead. When you perform an <code>IN</code> operation, SQL will typically not perform the complete join operation and exit early as soon as a match is found. It also doesn't return all the rows, just true/false whether there is a match for a given value. </li> <li>Using <code>IN</code> in your second example may yield a more optimal join operator (semi-join vs join) because you're explicitly stating that you are not interested in the output from the subquery, just whether or not there are records returned. </li> <li>You may be better off using the <code>EXISTS</code> clause. Although it's a common misconception that <code>IN</code> less efficient than <code>EXISTS</code> (they actually implement queries the same in most cases) <code>IN</code> can give unexpected results when dealing with nulls. </li> </ol> <p>The <code>EXISTS</code> version would look something like this:</p> <pre><code>UPDATE FT SET Flag = 1 FROM FeedbackTable WHERE EXISTS(SELECT * FROM FeedbackIndexed FI INNER JOIN QueueTable QT ON FI.Label = QT.Label AND FI.AccountID = QT.AccountID WHERE FeedbackID = FT.feedbackID) </code></pre> <p>The underlying query plan will likely be exactly the same as your <code>IN</code> example (after you remove the redundant <code>DISTINCT</code>) and it may yield the same query plan as the 3rd example but it's always good to know different approaches to solving a problem. </p> <p>A few more points. </p> <ul> <li>Your <code>TABLOCK</code> will be released when the query completes unless you wrap the query and the query to drop the processed records in an explicit transaction. I'm <em>pretty sure</em> you'll want to add <code>HOLDLOCK</code> here too. <code>HOLDLOCK</code> will hold the lock for the duration of the transaction. </li> <li>You mentioned a concern preventing writes to your queue table, what about reads? <code>TABLOCK</code> will implement a shared lock which may cause a race condition if your consumer proc is running multiple instances simultaneously. Consider using <code>TABLOCKX</code> if that will be a problem. </li> <li>There may be better ways manage locking and race conditions there. I like to use SP_GETAPPLOCK for this. I wrote an article on the subject several years ago, it may be worth a read <a href="http://www.sqlservercentral.com/articles/Miscellaneous/2649/" rel="nofollow">sp_getapplock</a></li> </ul> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload