1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Calculate Working hours

Discussion in 'Databases' started by baktha.thalapathy, May 4, 2010.

  1. #1
    Hello,

    I am using the below sql query to calculate working hours. The problem which i am facing is that query is taking lot of time to calculate the working hours. Please help to reduce the execution time of this query or if there is any other way to calculate working hours

    The following query take 63.499 sec

    
    SELECT sql_calc_found_rows                                          gstime, 
           MAX(stoptime)                                                AS mx, 
           MIN(starttime)                                               AS mn, 
           Sec_to_time(SUM(Time_to_sec(Timediff(stoptime, starttime)))) AS totalworktime 
    FROM   (SELECT gstime, 
                   gstime                                               AS stoptime, 
                   Coalesce((SELECT MAX(b.gstime) 
                             FROM   xydata b 
                             WHERE  objectid = '17' 
                                    AND clientid = '1' 
                                    AND gstime > '2010-04-20 08:22:27' 
                                    AND gstime < '2010-04-26 10:22:27' 
                                    AND b.objectid = a.objectid 
                                    AND b.gstime < a.gstime), gstime) AS starttime 
            FROM   xydata a 
                   INNER JOIN fm4features f 
                     ON f.id = a.id 
            WHERE  objectid = '17' 
                   AND clientid = '1' 
                   AND gstime > '2010-04-20 08:22:27' 
                   AND gstime < '2010-04-26 10:22:27' 
                   AND f.dataid = '1' 
                   AND f.VALUE = '1') derived 
    GROUP  BY Date_format(gstime, '%Y-%m-%d') 
    ORDER  BY gstime ASC
    [code]
    please help me how do i change the query to reduce the sec.
    Code (markup):
     
    baktha.thalapathy, May 4, 2010 IP
  2. islandhopper8

    islandhopper8 Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #2
    Make sure that the fields you search for have an index that will speed up the query.
     
    islandhopper8, May 24, 2010 IP
  3. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    i have used index also

    In xydata->gstime, objectid, clientid are index
    In fm4features->Id, dataid, value are index

    but it will take the more execution time compare with previous query
     
    baktha.thalapathy, May 24, 2010 IP