Performance question for a query calculating maximum people in a building
Hello everybody,
I have a query that is pretty slow and was trying to figure out how to compute the data I want.
I have a table that lists every visits of people into a building:
table visits(date_in timestamp, date_out timestamp, name varchar)
I want for each day of a month the maximum number of people into the building.
My request below:
SELECT date_slice, max(people) as measure FROM (
SELECT TIME_SLICE(date, 86400) as date_slice, SUM(val) OVER (ORDER BY date) AS people FROM
( WITH subvisits AS (SELECT * from visits where date_in<'2013-12-01' and date_out > '2013-11-01')
SELECT date_in as date, 1 as val FROM subvisits
UNION ALL SELECT date_out as date, -1 FROM subvisits
) AS in_out
) AS sub
GROUP BY date_slice ORDER BY date_slice;
First the subtable subvisits selects relevant data (the current month), then in_out computes a list of date with 1 for enter, -1 for exit. This data is then aggregated with the sum over date to compute the number of people inside the building. At the end, the max number for each day is computed.
The issue is that when the visits table is big, request takes very long to execute.
Is there a simpler or faster way to compute this data ?
I have a query that is pretty slow and was trying to figure out how to compute the data I want.
I have a table that lists every visits of people into a building:
table visits(date_in timestamp, date_out timestamp, name varchar)
I want for each day of a month the maximum number of people into the building.
My request below:
SELECT date_slice, max(people) as measure FROM (
SELECT TIME_SLICE(date, 86400) as date_slice, SUM(val) OVER (ORDER BY date) AS people FROM
( WITH subvisits AS (SELECT * from visits where date_in<'2013-12-01' and date_out > '2013-11-01')
SELECT date_in as date, 1 as val FROM subvisits
UNION ALL SELECT date_out as date, -1 FROM subvisits
) AS in_out
) AS sub
GROUP BY date_slice ORDER BY date_slice;
First the subtable subvisits selects relevant data (the current month), then in_out computes a list of date with 1 for enter, -1 for exit. This data is then aggregated with the sum over date to compute the number of people inside the building. At the end, the max number for each day is computed.
The issue is that when the visits table is big, request takes very long to execute.
Is there a simpler or faster way to compute this data ?
0
Comments
EXPLAIN <query>;
How big is the table visits?