MySQL knows only one query result, using limit 1 in SQL statements can improve query efficiency

Source: Internet
Author: User

Users use email as a user name landing, you need to check out an email corresponding to a record. Each user's email is unique.

SELECT * from T_user WHERE email=?;

The above statement implements a query email corresponding to a user information, but because the email column is not indexed , will result in a full table scan , the efficiency will be very low.

SELECT * from T_user WHERE email=? LIMIT 1;

Plus limit 1, as long as a corresponding record is found, it will not continue to scan downward , the efficiency will be greatly improved.

If the email is an index, you do not need to add the limit 1, if it is based on the primary key to query a record also does not need limit 1, the primary key is also an index.

For example:

SELECT * from T_user WHERE id=?;

There is no need to write:

SELECT * from T_user WHERE id=? LIMIT 1;

There is no difference in efficiency.

MySQL knows only one query result, using limit 1 in SQL statements can improve query efficiency

Related Article

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.