``` -- Take a sample CREATE TABLE IF NOT EXISTS te...
# general
j
Copy code
-- Take a sample
CREATE TABLE IF NOT EXISTS temp_sample AS
SELECT 
    unix_time * 1000 AS unix_time_millis,
    SUM(user + system) * 10 AS cpu_time_millis -- CPU time is in ticks = 1/100 sec
FROM time, cpu_time;

-- Bootstrap the "current" table; the first time this query runs it will get deltas of 0
CREATE TABLE IF NOT EXISTS current_sample AS
SELECT * from temp_sample;

-- Rotate tables
DROP TABLE IF EXISTS last_sample;
ALTER TABLE current_sample
RENAME TO last_sample;
ALTER TABLE temp_sample
RENAME TO current_sample;

-- Query the difference
SELECT 
    current.cpu_time_millis - last.cpu_time_millis AS delta_cpu_time_millis,
    current.unix_time_millis - last.unix_time_millis AS elapsed_millis
FROM current_sample AS current, last_sample AS last;
👍 2
💯 1
metal 2