39
loading...
This website collects cookies to deliver better user experience
The index may also be used even if ORDER BY doesn’t match the index exactly, as long as other columns in ORDER BY are constant.
Sometimes, the optimizer probably may not use Index if it finds indexes expensive as compared to scanning through the table.
SELECT * FROM USER
ORDER BY userId , mobileNumber;
SELECT * FROM users ORDER BY userId ;
SELECT * FROM users ORDER BY userId LIMIT 5;
Ordering by multiple columns does not require scanning the database twice! In MySQL (<v4.1), it used to read the data twice, once to match rows in WHERE clause and one, in the end to prepare the result from row pointers.
With the help of EXPLAIN statement, we can see if query is making use of filesort or indexes.