[Solved] Convert duration from milliseconds to hh:mm:ss format in SQL

EverSQL Database Performance Knowledge Base

Convert duration from milliseconds to hh:mm:ss format in SQL

Database type:

i am usinga MySql database and i am trying to get the total duration (which is in milliseconds format in the database) from the "builds" table, i need it in this format (hh:mm:ss).

this is my query :

SELECT
SUM(Builds.Duration) as duration , DATE_FORMAT(Builds.date, "%Y-%c-%d") as date
FROM Builds
JOIN Labels ON Builds.Labels_label_id = Labels.label_id 
JOIN CITools ON CITools.tool_id = Labels.CITools_tool_id 
WHERE Labels.label_name LIKE '%SPOT%' AND CITools.tool_name='jenkins' AND Builds.date >= 
'2022-03-01' AND Builds.date <= '2022-04-01'
GROUP BY MONTH(Builds.date)

And this is the result:

| duration   |    date    |
__________________________
| 5634635345 | 2022-03-01 |
| 5491338343 | 2022-04-01 |
|     .      |     .      |
|     .      |     .      |

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid LIKE Searches With Leading Wildcard (query line: 14): The database will not use an index when using like searches with a leading wildcard (e.g. '%SPOT%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  4. Index Function Calls Using Generated Columns (modified query below): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index to optimize the search. Creating and indexing a generated column (supported in MySQL 5.7) will allow MySQL to optimize the search.
Optimal indexes for this query:
ALTER TABLE `Builds` ADD INDEX `builds_idx_date` (`date`);
ALTER TABLE `Builds` ADD INDEX `builds_idx_month_date` (`month_date`);
ALTER TABLE `CITools` ADD INDEX `citools_idx_tool_name_tool_id` (`tool_name`,`tool_id`);
ALTER TABLE `Labels` ADD INDEX `labels_idx_label_id` (`label_id`);
The optimized query:
SELECT
        SUM(Builds.Duration) AS duration,
        DATE_FORMAT(Builds.date,
        "%Y-%c-%d") AS date 
    FROM
        Builds 
    JOIN
        Labels 
            ON Builds.Labels_label_id = Labels.label_id 
    JOIN
        CITools 
            ON CITools.tool_id = Labels.CITools_tool_id 
    WHERE
        Labels.label_name LIKE '%SPOT%' 
        AND CITools.tool_name = 'jenkins' 
        AND Builds.date >= '2022-03-01' 
        AND Builds.date <= '2022-04-01' 
    GROUP BY
        Builds.month_date 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.