title: Running an SQL query faster
body: <pre><code>   SELECT projectID, urlID, COUNT(1) AS totalClicks, projectPage,
     (SELECT COUNT(1)
       FROM   tblStatSessionRoutes, tblStatSessions
       WHERE  tblStatSessionRoutes.statSessionID = tblStatSessions.ID
       AND    tblStatSessions.projectID = tblAdClicks.projectID
       AND    (tblStatSessionRoutes.leftPageID = tblAdClicks.projectPage OR
             tblStatSessionRoutes.rightPageID = tblAdClicks.projectPage)) AS totalViews
     FROM   tblAdClicks
     WHERE  projectID IN (SELECT projectID FROM tblProjects WHERE userID = 5)
     GROUP  BY projectID, urlID, projectPage
     ORDER  BY CASE projectID
            WHEN 170 THEN
             1
            ELSE
             0
        END, projectID
  </code></pre>
  
  <p>This is by no means an especially complex query, but because the database is normalised to a good level, and we are dealing with a significant amount of data, this query can be quite slow for the user.</p>
  
  <p>Does anyone have tips on how to improve the speed of it?  If I strategically denormalise parts of the database would this help?  Will running it in a stored proc offer significant improvements?</p>
  
  <p>The way I handle the data is efficient in my code, the bottleneck really is with this query.</p>
  
  <p>Thanks!</p>