Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server Query Tuning: why CPU Time is higher than Elapsed Time ? Are they relevant to set operation?
    primarykey
    data
    text
    <p>I have two query to filter some userid depend on question and its answers.</p> <h2>Scenario</h2> <p>Query A is (the original version):</p> <pre><code>SELECT userid FROM mem..ProfileResult WHERE ( ( QuestionID = 4 AND QuestionLabelID = 0 AND AnswerGroupID = 4 AND ResultValue = 1 ) OR ( QuestionID = 14 AND QuestionLabelID = 0 AND AnswerGroupID = 19 AND ResultValue = 3 ) OR ( QuestionID = 23 AND QuestionLabelID = 0 AND AnswerGroupID = 28 AND ( ResultValue &amp; 16384 &gt; 0 ) ) OR ( QuestionID = 17 AND QuestionLabelID = 0 AND AnswerGroupID = 22 AND ( ResultValue = 6 OR ResultValue = 19 OR ResultValue = 21 ) ) OR ( QuestionID = 50 AND QuestionLabelID = 0 AND AnswerGroupID = 51 AND ( ResultValue = 10 OR ResultValue = 41 ) ) ) GROUP BY userid HAVING COUNT(*) = 5 </code></pre> <p>I use 'set statistics time on' and 'set statistic io on' to check the cpu time and io performance.</p> <p>the result is:</p> <pre><code>CPU time = 47206 ms, elapsed time = 20655 ms. </code></pre> <p>I rewrote Query A via using Set Operation, let me name it Query B:</p> <pre><code>SELECT userid FROM ( SELECT userid FROM mem..ProfileResult WHERE QuestionID = 4 AND QuestionLabelID = 0 AND AnswerGroupID = 4 AND ResultValue = 1 INTERSECT SELECT userid FROM mem..ProfileResult WHERE QuestionID = 14 AND QuestionLabelID = 0 AND AnswerGroupID = 19 AND ResultValue = 3 INTERSECT SELECT userid FROM mem..ProfileResult WHERE QuestionID = 23 AND QuestionLabelID = 0 AND AnswerGroupID = 28 AND ( ResultValue &amp; 16384 &gt; 0 ) INTERSECT SELECT userid FROM mem..ProfileResult WHERE QuestionID = 17 AND QuestionLabelID = 0 AND AnswerGroupID = 22 AND ( ResultValue = 6 OR ResultValue = 19 OR ResultValue = 21 ) INTERSECT SELECT userid FROM mem..ProfileResult WHERE QuestionID = 50 AND QuestionLabelID = 0 AND AnswerGroupID = 51 AND ( ResultValue = 10 OR ResultValue = 41 ) ) vv; </code></pre> <p>the CPU Time and Elapsed Time is:</p> <pre><code>CPU time = 8480 ms, elapsed time = 18509 ms </code></pre> <h2>My Simple Analysis</h2> <p>As you can see from up result, Query A have CPU Time more than 2 times of Elapsed time</p> <p>I search for this case, mostly people say CPU time should less than Elapsed time, because CPU time is how long the CPU running this task. And the Elapsed time include I/O time and other sort of time cost. But one special case is when the Server has multiple Core CPU. However, I just checked the development db server and it has one single core CPU.</p> <h2>Question 1</h2> <p>How to explain that CPU time more than Elapsed time in Query A in a single core CPU environment?</p> <h2>Question 2</h2> <p>After, using set operation, Is the performance really improved?</p> <p>I have this question because logical reads of Query B is 280627 which is higher than Query A's 241885</p> <p><a href="http://www.bradmcgehee.com/" rel="nofollow">Brad McGehee</a> said in his article that <a href="http://www.sql-server-performance.com/2002/statistics-io-time/3/" rel="nofollow">'The fewer the logical reads performed by a query, the more efficient it is, and the faster it will perform, <strong>assuming all other things are held equal</strong>.'</a></p> <p>Than, does it correctly say that even Query B have higher logical reads than Query A, but CPU time is significantly less than Query A, Query B should have a better performance.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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