This article mainly introduces the simple example of Php+mysql query optimization, analyzes the technique of optimizing query of SQL statement in Php+mysql program design, and has some reference value for improving query efficiency, which can be consulted by friends.
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:
The code is 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:
The code is 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.