I understand that Postgresql may return results in non-deterministic order when the query parameters are non-unique. This is especially a problem when we use LIMIT
since this may return entirely different results. So the suggestion for safety is to use ORDER BY
with LIMIT
.
However, what if we use ORDER BY
on an non-unique column name? Will this produce a deterministic result or no? Let me demonstrate with an example:
We have the following data:
id | name | index
1 foo 5
1 bar 2
1 test 5
If I query SELECT * FROM <table> WHERE id = 1 ORDER BY index LIMIT 1;
. Will this produce the same result every time?
ORDER BY
will produce a deterministic result.ORDER BY index
is not deterministic.