1

I have a table which have 31M data,when I try like this:

select id from class limit 20 offset 0;

I got the result is not ordered,the result like this "13,18273785,29046580...",why it's not "1,2,3,4,...,20"?

Btw,at another small table,I got the correct result which like "1,2,2,4,...,20",The normal table has less than 10,000 rows of data,The incorrect table has more than 30000,000 rows of data.Any idea? Because mysql transaction isolation mechanism? thanks

I know I can use the order by to get the data I expected,why in the table which have a bit of data like 3000 rows,the limit and offset can get the data I expected,but the another table 30,000,000 can not get ordered
5
  • mysql version is 5.7.19
    – Anteoy
    Oct 27, 2017 at 9:05
  • The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result set - not from the db - you need an order by or the outcome will be indeterminate.
    – P.Salmon
    Oct 27, 2017 at 9:09
  • @P.Salmon Thanks ,But why the another small table which used limit offset can get the ordered data?
    – Anteoy
    Oct 27, 2017 at 9:29
  • Indeterminate ie lucky - all data is stored as unordered sets , you need an order by to impose order (irrespective of any indexes)
    – P.Salmon
    Oct 27, 2017 at 9:35
  • maybe the question I describe the question is not clear,I know I can use the order by to get the data I expected,why in the table which have a bit of data like 3000 rows,the limit and offset can get the data I expected,but the another table 30,000,000 can not get ordered
    – Anteoy
    Oct 27, 2017 at 9:51

1 Answer 1

1

You need to add ORDER BY clause e,g,:

SELECT id 
FROM class 
ORDER BY id
LIMIT 20 OFFSET 0;
13
  • Yes, I have tried, I can get the correct results, but after adding order by id after spending too much time
    – Anteoy
    Oct 27, 2017 at 9:08
  • @Anteoy Do you have any index on id column? If not, you can create one Oct 27, 2017 at 9:11
  • If order by id takes too many seconds, perhaps your ID column is not primary key or is not indexed. @Anteoy Oct 27, 2017 at 9:11
  • 1
    @Anteoy by the way, this is another question. If you think this answer has solved your problem, you can accept it and ask a new question. Oct 27, 2017 at 9:14
  • @Darshan Mehta Thanks for your answers,I have the index for my id ,And maybe the question I describe the question is not clear,I know I can use the order by to get the data I expected,but the why? I just have more than 30,000,000 rows for this table ,So it's very slow,by the way ,why in the table which have a bit of data like 3000 rows,the limit and offset can get the data I expected.
    – Anteoy
    Oct 27, 2017 at 9:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.