I believe I a fairly simple question that I can not locate here on Stack or out in Google land. I have a fairly basic select statment that’s something like this:
SELECT
itemid,
itemdiscription,
SUM(quantity)
FROM mytable
GROUP BY itemid, itemdescription
ORDER BY itemid
LIMIT 250
OFFSET 0;
Basicly this is pulling from a table of over 100k + records or so, or less depending, it’s a temporary table. So what I’m trying to figure out is the SUM function providing reliable data, because there could 30k records between similar item ids. My initial thought was this would preform the query and then return only the first 250 results, but then I got to thinking maybe not, so I thought I’d ask the community for comfirmation on how this works.
The main reason I’m using LIMIT/OFFSET is because I’m preforming the query from PHP and those values are variables that are iterated for preformance so I’m not dealing with an Array that uses mass amounts of memory.
Thanks!
As the docs state, the limit (or offset) apply to “the rows that are generated by the rest of the query”, so consider the result of the query prior to the application of limit or offset. Applying these clauses then affects these results.
http://www.postgresql.org/docs/9.2/static/queries-limit.html
Here’s a SQL Fiddle of a couple of ways of using LIMIT
http://www.sqlfiddle.com/#!12/08fa0