title: SQL Server query taking up 100% CPU and runs for hours
body: <p>I have a query that has been running every day for a little over 2 years now and has typically taken less than 30 seconds to complete. All of a sudden, yesterday, the query started taking 3+ hours to complete and was using 100% CPU the entire time.</p>
  
  <p>The SQL is:</p>
  
  <pre><code>SELECT
    @id, 
    alpha.A, alpha.B, alpha.C, 
    beta.X,  beta.Y,  beta.Z, 
    alpha.P, alpha.Q
  FROM 
    [DifferentDatabase].dbo.fnGetStuff(@id) beta 
    INNER JOIN vwSomeData alpha ON beta.id = alpha.id
  </code></pre>
  
  <p><code>alpha.id</code> is a BIGINT type and <code>beta.id</code> is an INT type. <code>dbo.fnGetStuff()</code> is a simple SELECT statement with 2 INNER JOINs on tables in the same DB, using a <code>WHERE id = @id</code>. The function returns approximately 11000 results.</p>
  
  <p>The view <code>vwSomeData</code> is a simple SELECT statement with two INNER JOINs that returns about 590000 results. </p>
  
  <p>Both the view and the function will complete in less than 10 seconds when executed by themselves. Selecting the results of the function into a temporary table first and then joining on that makes the query finish in &lt; 10 seconds. </p>
  
  <p>How do I troubleshoot what's going on? I don't see any locks in the activity manager.</p>