Closed thatnerd closed 3 years ago
Good find, @thatnerd!
@ericharmeling, @rmloveland, for consideration.
I think this somehow got fixed on the Selection queries page:
SELECT * FROM employees AS OF SYSTEM TIME '-1m' WHERE emp_no > 10000 ORDER BY emp_no LIMIT 25;
But I have not looked into the other pages mentioned.
Our docs include uses of
LIMIT
that aren't paired withORDER BY
. This is an anti-pattern, and shouldn't be present, but it seems to be widespread.In cases that lack an
ORDER BY
, we don't guarantee ordering of results, even by primary key, and when usingLIMIT
, subsequent identical queries could return different results even in the absence of writes. While each leaseholder should return results ordered by the primary key, the gateway wouldn't necessarily run a merge sort on the results it gets from each node unless explicitly told to.A really clear example is in our docs on pagination of results, where we recommend using a trick called the "seek method" to paginate results by using a combination of
ORDER BY
on an indexed field, plus aLIMIT
clause, in order to avoid using the less efficientOFFSET
clause, but neither example usesORDER BY
.You can see that the example,
SELECT * FROM employees WHERE emp_no > 10000 LIMIT 25;
doesn't useORDER BY
at all.A possible (and relatively simple) solution just for this section for would be to add
ORDER BY emp_no
to each query that uses this data set. Other queries in the page, such asSELECT id, name FROM accounts LIMIT 5
, shouldORDER BY
the primary key if nothing else is available.I looked in
docs/v19.2
to find other potential cases of aLIMIT
without anORDER BY
on the same line:Each of these has at least one use of
LIMIT
without a correspondingORDER BY
in that line, though it might miss anORDER BY
in a previous line for multi-line SQL statements.