I have a table containing runtimes for generators on different sites, and I want to select the most recent entry for each site. Each generator starts once or twice a week.
I have a request that will do this, but I wonder if it is the best. I cannot help but think that using WHERE x IN (SELECT ...) is lazy and not the best way to formulate a query - any query.
The table is as follows:
CREATE TABLE generator_logs (
id integer NOT NULL,
site_id character varying(4) NOT NULL,
start timestamp without time zone NOT NULL,
"end" timestamp without time zone NOT NULL,
duration integer NOT NULL
);
And request:
SELECT id, site_id, start, "end", duration
FROM generator_logs
WHERE start IN (SELECT MAX(start) AS start
FROM generator_logs
GROUP BY site_id)
ORDER BY start DESC
There is not a lot of data, so I'm not worried about query optimization. However, I have to do similar things on tables with 10 million rows (large tables, as far as I know!), And optimization is more important.
So, is there a better query for this, and inline queries are a bad idea?