Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql big query performance improvement
    primarykey
    data
    text
    <p>Anyone know how I can optimise this query? It works but it takes so long that it causes server timeouts when there's a heavy load.</p> <pre><code>$screenRestrict="clientID='1' AND screenID='1'"; $lastupdate="1318515710"; INSERT INTO allvisits (clientID, screenID, hitID, entryPageID, entryPageName, xentryTime, xexitTime, pagecount, minsonsite)( SELECT clientID, screenID, id AS hitID, pageID AS entryPageID, (SELECT name FROM pages WHERE id=entryPageID) AS entryPageName, (SELECT clicktime FROM clicks WHERE id = hitID AND isFirstClick=1 ) AS xentryTime, (SELECT MIN(clicktime) FROM clicks WHERE $screenRestrict AND isLastClick=1 AND clicktime &gt; xentryTime) AS xexitTime, (SELECT COUNT(*) FROM clicks WHERE $screenRestrict AND clicktime BETWEEN xentryTime AND xexitTime) AS pagecount, (SELECT (xexitTime-xentryTime)/60) AS minsonsite FROM clicks WHERE $screenRestrict AND isFirstClick=1 AND clicktime&gt;'$lastupdate' ) </code></pre> <p>Thanks a lot :)</p> <p><strong>Update:</strong></p> <p>Thanks to all for the tips. I've added an index to isLastClick and managed to speed it up a good deal but it still takes +10 seconds on a low server load. I've identified the last bottleneck and marked it below. Is there any better way to select the first "isLastClick" record that is later than xentrytime?</p> <pre><code>SELECT clientid, screenid, id AS hitid, pageid AS entrypageid, clicktime AS xentrytime, (SELECT name FROM pages WHERE id = entrypageid) AS entrypagename, (SELECT clicktime FROM clicks WHERE clicktime &gt; xentrytime //&lt;&lt;removing this cuts 8.5 seconds!! AND screenid = '2' AND islastclick = 1 LIMIT 1) AS xexittime, (SELECT COUNT(1) FROM clicks WHERE screenid = '2' AND clicktime BETWEEN xentrytime AND xexittime) AS pagecount, (SELECT ( xexittime - xentrytime ) / 60) AS minsonsite FROM clicks WHERE screenid = '2' AND isfirstclick = 1 AND clicktime &gt; '1318961057' </code></pre>
    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.
 

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