Skip to content

Instantly share code, notes, and snippets.

@phillip-haydon
Created November 24, 2016 01:56
Show Gist options
  • Save phillip-haydon/3441d1df78853160c611d2bdfba620db to your computer and use it in GitHub Desktop.
Save phillip-haydon/3441d1df78853160c611d2bdfba620db to your computer and use it in GitHub Desktop.
Rearranged slow query
sql> SELECT lab_name, COUNT(*)
FROM (
SELECT (record ->> 'scientist_id')::BIGINT AS scientist_id
FROM measurements
WHERE
(record ->> 'value_1')::INTEGER = 0 AND
(record ->> 'value_2')::INTEGER = 0 AND
(record ->> 'value_3')::INTEGER = 0
) m
JOIN scientist_labs AS s
ON (m.scientist_id = s.scientist_id)
GROUP BY lab_name
[2016-11-24 00:59:16] 3 rows retrieved starting from 1 in 6m 15s 346ms (execution: 6m 15s 338ms, fetching: 8ms)
sql> SELECT lab_name, SUM(counter)
FROM (
SELECT COUNT(*) counter, (record ->> 'scientist_id')::BIGINT AS scientist_id
FROM measurements
WHERE
(record ->> 'value_1')::INTEGER = 0 AND
(record ->> 'value_2')::INTEGER = 0 AND
(record ->> 'value_3')::INTEGER = 0
GROUP BY (record ->> 'scientist_id')
) m
JOIN scientist_labs AS s
ON (m.scientist_id = s.scientist_id)
GROUP BY lab_name
[2016-11-24 00:52:23] 3 rows retrieved starting from 1 in 1s 30ms (execution: 1s 20ms, fetching: 10ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment