I always thought that it was effective for mysql to create an index in the order condition, and it was ineffective.

Source: Internet
Author: User
I always thought that it was effective for mysql to create an index in the order condition, but it didn't work. EXPLAIN SELECT *
FROM 'order2'
WHERE 1
Order by 'create _ time' DESC

Full table scan...


Reply to discussion (solution)

The index in order is not so used.

Your current condition is the full table. no matter what index you have, it will certainly be the whole table again.

I understand that order indexes should be used in two ways.
1. when there is a limit
2. in the select return value of the order field, this is complicated, that is, only the order field is returned; or several fields are returned. in this case, the index must be a joint index in the order of these fields.

The index in order is not so used.

Your current condition is the full table. no matter what index you have, it will certainly be the whole table again.

I understand that order indexes should be used in two ways.
1. when there is a limit
2. in the select return value of the order field, this is complicated, that is, only the order field is returned; or several fields are returned. in this case, the index must be a joint index in the order of these fields.



I just used the order index again. it seems that it is still valid. Is there any junk data that affects the index of the entire table? (Different databases and tables)

Add an index condition such as explain select *
FROM 'order2'
WHERE 1 and create_time> 0
Order by 'create _ time' DESC

If the data base in your table is too small or the data distribution is not large, the mysql Query optimizer will remove the index and perform a full table scan.
This kind of automatic optimization behavior is not predictable. when the data volume is small, sometimes indexes are not used.

If the data base in your table is too small or the data distribution is not large, the mysql Query optimizer will remove the index and perform a full table scan.
This kind of automatic optimization behavior is not predictable. when the data volume is small, sometimes indexes are not used.


Yes, I found the problem.
In addition, if the search condition detects too much data, the entire table will be scanned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.