0

I want to find out the total amount of time the queries are running in Redshift.

Is there any query with which I can get this information?.

I tried to get this data using stl_query but because there would be many queries running at the same point there could be overlapping execution times as well. So I cannot just aggregate based on date and sum up the execution time, So I have written the following query

WITH query_times AS (
    SELECT
        DATE_TRUNC('day', starttime) AS query_date,
        starttime,
        endtime
    FROM
        stl_query
    WHERE
        userid > 1  -- Exclude system queries
        AND starttime >= DATEADD(day, -7, CURRENT_DATE)
),
distinct_intervals AS (
    SELECT
        query_date,
        starttime AS period_start,
        LEAD(starttime) OVER (PARTITION BY query_date ORDER BY starttime) AS next_starttime,
        endtime
    FROM
        query_times
),
non_overlapping_intervals AS (
    SELECT
        query_date,
        period_start,
        CASE
            WHEN next_starttime IS NULL OR next_starttime > endtime THEN endtime
            ELSE next_starttime
        END AS period_end
    FROM
        distinct_intervals
)
SELECT
    query_date,
    SUM(DATEDIFF(seconds, period_start, period_end))/3600 AS total_running_time_hours
FROM
    non_overlapping_intervals
GROUP BY
    query_date
ORDER BY
    query_date;

This query seems to give me somewhat accurate data but I am not sure if this is the correct approach

If anyone can help me with this it would be much appreciated. Surprisingly there is not much data online about this kind of stats.

Again thank you before hand!

3
  • I guess this comes down to what you really mean by "execution time". If you mean the amount of time the query is in the execution state (vs. compile or return), this should be doable. If you mean the sum of all the time that all the threads are active on CPU cores, then I think you will be out of luck. Or is it something else that you are looking for? You may find what you are looking for in STV_WLM_QUERY_TASK_STATE , STL_WLM_QUERY, and/or STL_RETURN Commented Nov 12, 2024 at 16:12
  • Thank you for the comment @BillWeiner. We are planning to move to Redshift Serverless from RA3 clusters. In serverless we will be billed based on how many hours we are actually executing the queries. So we are trying to get the metrics of exactly how many hours we are using our cluster a day. Commented Nov 13, 2024 at 7:04
  • A few things: In serverless you are billed on transaction time, not execution time. So it is important to END your transactions. While a transaction cannot end until the execution and return are complete, the billing isn't just for execution time. Execution time on serverless will depend on what cap on RPUs you decide for each work group. See: docs.aws.amazon.com/redshift/latest/mgmt/… Commented Nov 13, 2024 at 16:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.