Php+mysql Query Optimization Simple example _php skill

Source: Internet
Author: User
Tags mysql query mysql query optimization

The method of Php+mysql query optimization is analyzed in this paper. Share to everyone for your reference. The specific analysis is as follows:

Php+mysql is one of the most frequently used gold partners, they are used together, can play the best performance, of course, if with the use of Apache, it is more perfect.

Therefore, you need to do a good job of MySQL query optimization, the following a simple example, showing the impact of different SQL statements on query speed.

There is a table test that has a self increasing ID as the primary index, and now to query for records where the ID number is in a range, you can use the following SQL statement:

Copy Code code as follows:
SELECT *
From ' Test '
ORDER BY ID ASC
Limit 208888,50

The meaning of this SQL statement is to take 50 records back from the record with ID number 208888, testing in a 300,000-record database, with the primary index already set up, the time to execute this statement is 40-50 seconds, so are there any faster SQL statements to execute? Obviously, look at the following SQL statement:
Copy Code code as follows:
SELECT *
From ' Test '
WHERE ID
BETWEEN 208838
and 208888

This statement uses a condition for filtering, and in practice the test execution time is about 0.06 seconds.

The reason is because, although the id attribute is already indexed, but the sort is still a very high cost operation, be careful to use, and the second statement, you can let MySQL take full advantage of the database has been established in the B + Tree index, so the search up very fast, is hundreds of times times the original.

This shows that the Web site developers in the use of SQL statements, must be cautious, because an inadvertent SQL statement, may make your site access to a sharp decline in the background database is under great pressure, and quickly fall into the dilemma of unable to open the page.

I hope this article will help you with the Php+mysql program design.

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.