0

I am facing sorting issue in mysql

See the output of below query:

select astrologers.id,astrologers.name,chat_online,online,experience from `astrologers` 
where `astrologers`.`status` = '1' 
order by experience asc limit 10;
id name chat_online online experience
15 Astro Anoop 0 0 3
20 Test Astro2 0 0 3
3 Test anoop 0 0 5
4 Anoop Kumar trivedi 0 0 5
7 Test 0 0 5
58 Neeraj yadav 1 0 5
45 Satish Kumar Gupta 1 1 10
56 AP Sharma 1 0 15
40 VG Astrologer App 1 0 55

In above result id 58 (Neeraj yadav) is at 6th position but when I run the same query with limit 3, same id 58 (Neeraj yadav) is at 3rd position:

select astrologers.id,astrologers.name,chat_online,online,experience 
from `astrologers` 
where `astrologers`.`status` = '1' 
order by experience asc limit 3;
id name chat_online online experience
20 Test Astro2 0 0 3
15 Astro Anoop 0 0 3
58 Neeraj yadav 1 0 5

The 3rd row in above result should be id 3 (Test anoop) but it gives id 58 (Neeraj yadav)

Is this bug in mysql?

1
  • Why should it be 3 and not 58? They both have the exact same experience so any one of the three is correct. Is there another column that you are wanting to sort by in addition to experience? Perhaps you want ORDER BY experience, id? Commented Nov 2, 2022 at 18:01

1 Answer 1

3

Is this a bug in MySQL?

No. The problem is that your sort is not deterministic, and gives ties in the third position:

|  3 | Test anoop          |           0 |      0 |          5 |
|  4 | Anoop Kumar trivedi |           0 |      0 |          5 |
|  7 | Test                |           0 |      0 |          5 |
| 58 | Neeraj yadav        |           1 |      0 |          5 |

All 4 users have the same experience, hence leaving the database to figure out how they should be sorted.

When asked to return to top 3 rows, the database picks the first two, and then one of the 4 ties. The result that you get might not be consistent over consequent executions of the same query, as you are starting to see.

Bottom line: know you data; if you want a deterministic result, then use a deterministic sort. We could, for example, use id to break the ties, hence making the result predictable:

order by experience, id limit 3
Sign up to request clarification or add additional context in comments.

3 Comments

MySQL first fetches the result and then applies the limit na ?, in this case, it result should be the same irrespective of the limit and offset
work with this but I don't understand the logic. Mysql always gives priority to the first created row if the sort is tied. @gmb
@SatishGupta: Mysql always gives priority to the first created row if the sort is tied.: that's a common misunderstanding. Your experience might trick you into thinking that the database follows a certain rule, but the database actually does not guarantee anything in that case. This is not a documented behavior, and it may or may not work as you expect without qualifying as a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.