incorrect set of records returned when using order on a field with little variation
-
Mislav
- State changed from new to invalid
What you said there --
This works fine if the field you are sorting on is unique for each record, but falls down if you have a lot of records with the same field value. SQL seems to be inconsistent in the ordering of individual records within one unique field value.
Exactly. It's inconsistent. MS SQL and Postgres exhibit this behavior quite nicely. MySQL is consistent in most cases because they aren't following the spec closely and have implemented some sort of implicit ordering by ID.
Still, the user decides on the ordering, not this library. I understand what you're suggesting, but will_paginate has this rule of never changing your queries (apart from inserting LIMIT and OFFSET), so I will continue with this philosophy.
The user decides.
-
ReggieB
My argument would be that it isn't the user who has chosen to flip the data back and forth. It is the way will_paginate processes the data that makes this an issue.
However, a simple option would be to update the RDoc comments:
http://mislav.uniqpath.com/stati...
And add a comment to the "The importance of the :order parameter" section along the lines of:
"Ordering by a field for which many records have the same value (e.g. status) can result in incorrect selection of records for each page with some databases (MS SQL and Postgres for instance). With these databases it is recommend that you also order by id. That is, instead of ordering by "status DESC", use the alternative "status DESC, id DESC" and this will give better results."
-
Mislav
I think what is there in the docs now is quite fine:
Without the ORDER BY clause, databases aren‘t required to do consistent ordering when performing SELECT queries
-
ReggieB
That just says you need an ORDER BY clause, whereas in fact you need more than that - you need an ORDER BY clause that uniquely identifies and orders each record.
'ORDER BY status' is an ORDER BY statement and therefore would satisfy "Without the ORDER BY clause, databases aren‘t required to do consistent ordering when performing SELECT queries" but if there are far more records than there are statuses, this can result in incorrect pagination.
Looks like we'll have to agree to disagree, and as its your code - you win.
-
Mislav
- State changed from invalid to resolved
Well, it can't hurt http://github.com/mislav/will_pa...
:P
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.