Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:32:32+00:00 2026-05-26T08:32:32+00:00

Anyone know how I can optimise this query? It works but it takes so

  • 0

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.

$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 > 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>'$lastupdate'
)

Thanks a lot 🙂

Update:

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?

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 > xentrytime //<<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 > '1318961057'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:32:33+00:00Added an answer on May 26, 2026 at 8:32 am

    please check for comments.

    if $screenrestrict and page id are same for a page. try to join the inner tables with that page id

    I am not sure of your idea about xentrytime and xexittime columns. because, i see feilds fetched using sub query for the same thing.

    Just remember, for tuning a query try to attain a filter and restrict the number of rows selected in each sub query. Some filters are written based on business logic, so think of including those condition also when u write a query, and see if performance improves in explain plan.

    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 
            , 
            clicktime                        AS xentrytime, //this should work w.r.t your code
            (SELECT MIN(clicktime)             //try to change this logic. The logic written here doesnt look good at all. and try to filter out data by joining with outer table. 
             FROM   clicks 
             WHERE  $screenrestrict 
                    AND islastclick = 1 
                    AND clicktime > xentrytime)                     AS xexittime, 
            (SELECT COUNT(1)   //this will give some performance improvement
             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 > '$lastupdate') 
    

    Updated

    These 2 inner queries need some more of fine tuning

           SELECT MIN(innClick.clicktime)             
             FROM   clicks innClick1
             WHERE  innClick1.screenid =  '2' // when u put it as part of big query use  WHERE  innClick1.screenid = outClick.screenid instead of hard coding it. where outClick is alias for Click table in outside
                    AND innClick1.islastclick = 1 
                    AND innClick1.clicktime > innClick1.xentrytime
    
            SELECT COUNT(1) 
             FROM   clicks innClick2
             WHERE  innClick2.screenid =  '2' 
                    AND innClick2.clicktime BETWEEN innClick2.xentrytime AND innClick2.xexittime
    

    w.r.t explain plan

    • You can try to add more filter to reduce number of rows fetched in inner query.
    • try to reduce any full table scan, and if the scan is based on index scan (full also) can be ignored for time being.
    • try to add filters based on the columns that are already indexed.
    • try to do a lot of trial and error by filtering data further in these sub-queries and –
    • try to reduce the time in each query separately. and then finally join them when u have a better performance

    The clicktime, xentrytime and xexittime if indexed will yeild better performance for the query i guess. you can try it , but still indexing will slow your insert statements as those extra index should be updated each time an insert happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can use Request.Browser.IsMobileDevice . But does anyone know how it works,
Does anyone know where can I enable the bar that let me switch between
Does anyone know a good tool that can be run on a page (for
Does anyone know how can I cut a string that contain map in jquery/javascript
Does anyone know how can I replace this 2 symbol below from the string
Does anyone know of a library (preferably java) that can give me neighboring keys
can anyone can help me optimize this query and help me to tune my
Does anyone know how can I cut a string and then assign into an
Does anyone know how can I detect the browser type in css? What I
Does anyone know where can I find recent data on PHP4 vs PHP5 market

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.