Calculate Run time Query not Working

Posted on 2010-01-08
Last Modified: 2012-08-13
hi experts

I m working at GPS project and i want to calculate the total vehicle working time . The time selected by client or user.

I m using below query and its working but its very slow it is taking 3-4 minutes to calculate only 1 day vehicle driven time....

please help me to improve this query performance and time ...
SELECT SQL_CALC_FOUND_ROWS Lat,Longi, ID, Speed,ObjectId,GpsTime   , MAX(StopTime) as mx, MIN(StartTime) AS mn, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(StopTime, StartTime)))) AS TotalWorkTime  FROM ( sELECT Lat,Longi, ID, Speed,ObjectId,GpsTime    , GpsTime AS StopTime    , COALESCE((  SELECT MAX(b.GpsTime)   FROM xydata b   WHERE ObjectId='" + Objectid + "' and ClientId='" + id + "'  AND GpsTime > '" + from+"  " +result + "' AND  GpsTime < '" + to+"  " +result1 + "' AND  b.ObjectId = a.ObjectId  AND b.GpsTime < a.GpsTime  ),GpsTime) AS StartTime  fROM xydata a where ObjectId='" + Objectid + "' and ClientId='" + id + "' AND GpsTime > '01-08-2010 00:00:01' AND  GpsTime < '01-08-2010 11:59:59' AND Speed >  '3' ) derived Group by Lat  order by GpsTime asc  limit 1,700

Open in new window

Question by:Sachin_Ajmani
    LVL 3

    Accepted Solution


    This should work:

    Add an index on xydata table, on these fields:

    Please preserve the column ordering in the index.
    Please let me know...

    Note: If such index is already present, please add column: GpsTime. last to the above.


    Author Comment

    hi ZLATKO:

    ObjectId and GpsTime is already indexes and they both present in  xydata table but ClientId is not index ...

    i will check my query after changes ClientId into Index and i will tell you and please reply me about your note...

    Note: If such index is already present, please add column: GpsTime. last to the above.

    I m not getting this...

    Thanks for reply....
    LVL 3

    Assisted Solution


    In order to have your query fastened, multi-column index must be present, not separate index on ObjectId, and/or GpsTime. The index should include the two (or three) columns in itself.


    Author Comment

    hi ZLATKO:

    Thank you so much...
    now my query is executing very fast ....


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
    This article describes some very basic things about SQL Server filegroups.
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now