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.

Calculating 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

    please help me how do i change the query to reduce the 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
     
    baktha.thalapathy, May 4, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Can you answer these questions?

    What indexes does the table have?
    How many rows are in the table?
    Is this InnoDB or MyISAM or other?

    Also, can you post the output of running EXPLAIN with this query?
     
    jestep, May 7, 2010 IP
  3. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I have post explain query, show create xydata and fm4features

    
    1	PRIMARY	<derived2>	ALL					2749	100	Using temporary; Using filesort
    2	DERIVED	a	range	PRIMARY,id_objid	id_objid	16		33313	100	Using where; Using index
    2	DERIVED	f	ref	ID	ID	4	navl.a.ID	4	100	Using where
    3	DEPENDENT SUBQUERY	b	range	id_objid	id_objid	16		33313	100	Using where; Using index
    
    
    xydata	CREATE TABLE `xydata` (
      `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `Longi` varchar(255) DEFAULT NULL,
      `Lat` varchar(255) DEFAULT NULL,
      `Altitude` int(11) NOT NULL,
      `Angle` int(11) NOT NULL,
      `Satellite` int(11) NOT NULL,
      `Speed` int(11) NOT NULL,
      `ObjectId` int(10) unsigned NOT NULL,
      `ClientId` int(10) unsigned NOT NULL,
      `GsTime` datetime NOT NULL,
      `Location` varchar(255) DEFAULT NULL,
      `City` varchar(255) DEFAULT NULL,
      `State` varchar(255) DEFAULT NULL,
      `Distance` varchar(100) DEFAULT NULL,
      `Times` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`ID`),
      KEY `id_objid` (`ObjectId`,`ClientId`,`GsTime`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=70227 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED
    
    fm4features	CREATE TABLE `fm4features` (
      `ID` int(10) unsigned NOT NULL DEFAULT '0',
      `DataId` int(11) NOT NULL,
      `Value` varchar(20) NOT NULL,
      KEY `ID` (`ID`),
      CONSTRAINT `new_xydata_id` FOREIGN KEY (`ID`) REFERENCES `xydata` (`ID`) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED
    
    
    Code (markup):
     
    baktha.thalapathy, May 7, 2010 IP
  4. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    I have post explain query, show create table xydata and fm4features

    
    1	PRIMARY	<derived2>	ALL					2749	100	Using temporary; Using filesort
    2	DERIVED	a	range	PRIMARY,id_objid	id_objid	16		33313	100	Using where; Using index
    2	DERIVED	f	ref	ID	ID	4	navl.a.ID	4	100	Using where
    3	DEPENDENT SUBQUERY	b	range	id_objid	id_objid	16		33313	100	Using where; Using index
    
    
    xydata	CREATE TABLE `xydata` (
      `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `Longi` varchar(255) DEFAULT NULL,
      `Lat` varchar(255) DEFAULT NULL,
      `Altitude` int(11) NOT NULL,
      `Angle` int(11) NOT NULL,
      `Satellite` int(11) NOT NULL,
      `Speed` int(11) NOT NULL,
      `ObjectId` int(10) unsigned NOT NULL,
      `ClientId` int(10) unsigned NOT NULL,
      `GsTime` datetime NOT NULL,
      `Location` varchar(255) DEFAULT NULL,
      `City` varchar(255) DEFAULT NULL,
      `State` varchar(255) DEFAULT NULL,
      `Distance` varchar(100) DEFAULT NULL,
      `Times` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`ID`),
      KEY `id_objid` (`ObjectId`,`ClientId`,`GsTime`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=70227 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED
    
    fm4features	CREATE TABLE `fm4features` (
      `ID` int(10) unsigned NOT NULL DEFAULT '0',
      `DataId` int(11) NOT NULL,
      `Value` varchar(20) NOT NULL,
      KEY `ID` (`ID`),
      CONSTRAINT `new_xydata_id` FOREIGN KEY (`ID`) REFERENCES `xydata` (`ID`) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED
    
    
    Code (markup):
     
    baktha.thalapathy, May 7, 2010 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    There's so much logic going on that it's a bit tough to determine what is specifically slowing everything down. I would start out by using parenthesis around the specific Indexed items in the where clause. I would also put an index on fm4features .DataId, and fm4features.Value.

    Luckily you don't have many rows, so I think it is more a matter of finding the bottloenecks.

    
    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 (markup):
     
    jestep, May 7, 2010 IP
  6. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    In xydata and fm4features there are 1,00000 rows,

    I put index in fm4features.dadaid, fm4features.value

    then i run your(above) query it's also take 65.978 sec

    please reply there is any other options,
     
    Last edited: May 7, 2010
    baktha.thalapathy, May 7, 2010 IP
  7. adamfoh

    adamfoh Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    divide and conquer

    perhaps you should try removing some functions to see where is the bottleneck.
    eg remove order by, test timing.
    remove sql query in the coalesce leaving only a field

    If timing improve sufficiently, it probably indicate some issue with the part you removed.
    P.S. I know my example of removing order by, etc is not helpful because u need the sorting eventually.
    But at least you can know which parts are causing the slowness
    66 sec for a query sounds extreme though
     
    adamfoh, May 8, 2010 IP
  8. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    starttime takes so much time

    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
     
    baktha.thalapathy, May 8, 2010 IP
  9. adamfoh

    adamfoh Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If you are certain the above sql is causing the slowness, try
    a) objectid, clientid should be indexed
    b) gstime should be indexed with b-tree or some kind of tree structure. Because it does comparison, just make sure it isn't
    some kind of hashing function since it is very inefficient with comparison
     
    adamfoh, May 8, 2010 IP
  10. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #10
    thanks for your reply

    I used gstime index in b-tree but nothing to happen

    it takes same time,

    If any other options is there to reduce the execution time,

    thanks in advance
     
    baktha.thalapathy, May 11, 2010 IP
  11. adamfoh

    adamfoh Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    perhaps try between instead of > < for date comparison.

    if not, divide and conquer, perhaps by removing each where clause till u pinpoint
    which is causing the slowness
     
    adamfoh, May 11, 2010 IP
  12. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #12
    b.gstime < a.gstime

    it's only take 40 sec

    here is the slowness
     
    baktha.thalapathy, May 11, 2010 IP
  13. itsupportservice

    itsupportservice Peon

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Good to read about that b.gstime < a.gstime is taking only 40 sec for updating hours...
    Thanks for sharing this helpful information..!!
     
    itsupportservice, May 12, 2010 IP
  14. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #14
    Please help me how to reduce execution time or any other way is there
     
    baktha.thalapathy, May 12, 2010 IP
  15. mrhelena

    mrhelena Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Do you want to reduce execution time of your query?
     
    mrhelena, May 13, 2010 IP
  16. baktha.thalapathy

    baktha.thalapathy Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #16
    yes the above takes so much time to execute the query,
    please help me how to reduce the time execution ,

    thanks in advance
     
    baktha.thalapathy, May 13, 2010 IP