Assuming that both the MySQL server and the PHP server are configured in the most appropriate way, the system's scalability (Scalability) and user-perceived performance (user-perceived performance) are the main goals we pursue. In the actual operation, the data in MYSQL is often stored in memory in the form of HASH tables, BTREE and so on, the operation speed is very fast, and index has been pre-sorted; In many applications, MYSQL sequencing is preferred.
PHP has the following advantages over MySQL:
1, consider the entire Web site scalability and overall performance, in the Application layer (PHP) sorting will significantly reduce the load of the database, thereby enhancing the expansion of the entire site. And the database sort, actually the cost is very high, consumes the memory, the CPU, if the concurrency sorts many, the DB is easy to the bottleneck.
2, if there is a data middle tier between the application layer (PHP) and MySQL, reasonable utilization, PHP will have better benefits.
3, PHP in-memory data structure specifically for specific applications to design, more concise and efficient than the database;
4, PHP does not consider the data disaster recovery problem, can reduce this part of the operating loss;
5, PHP does not exist the table lock problem;
6, MySQL sorting, request and result return also need to be done through the network connection, and in PHP after sorting can be returned directly, reduce the network IO.
As for execution speed, the difference should not be great unless the application design is problematic, resulting in a large amount of unnecessary network IO. In addition, the application layer should be aware of the PHP Cache settings, if the outside will report internal errors, at this point to be evaluated according to the application, or adjust the Cache. The specific choice will depend on the specific application.
List some of the more excellent things to do in PHP:
1, the data source is not in MySQL, there is hard disk, memory or requests from the network, etc.;
2, the data exists in MySQL, the amount is small, and there is no corresponding index, at this time the data out to use PHP sorting faster;
3, the data source from the multiple MySQL server, at this time to extract data from multiple MySQL, and then in PHP to sort faster;
4, in addition to MySQL, there are other data sources, such as hard disk, memory or requests from the network, and so on, it is not appropriate to put these data into MySQL after sorting;
List some instances that must be sorted in MySQL:
1. This sort index already exists in MySQL;
2, MySQL data volume is large, and the result set needs a small subset of them, such as 1000000 rows of data, take top 10;
3, for a single order, multiple calls, such as statistical aggregation of the situation, can be provided to different services to use, then in MySQL sorting is preferred. In addition, for data depth mining, it is usually done in the application layer of sorting and other complex operations, the results are stored in MySQL, easy to use multiple times.
4, no matter where the data source from, when the amount of data large to a certain scale, due to the memory/cache relationship, no longer suitable for PHP sorting, the data copied, imported or existing MySQL, and index optimization, is better than PHP. However, it is better to use Java or even C + + to handle this kind of operation. Some data, such as big data aggregation or aggregation, are not worth the hassle of client sequencing. Of course, it is also useful to use a search engine-like approach to solve similar applications.
From the overall consideration of the website, it is necessary to add manpower and cost considerations. If the site size and load is small, and the manpower is limited (the number and capacity may be limited), at this time in the application layer (PHP) to do a lot of development and debugging work, time-consuming, not worth the candle; For large-scale website, power, server cost is very high, in the system architecture of budget, can save a lot of money, is the company's sustainable development is necessary; If you can sort on the application layer (PHP) and meet the business requirements, try to do it at the application level.
About the implementation of the sort in PHP and MySQL in order to perform the relevant knowledge of the introduction here, I hope this introduction can be a harvest for you!
The above describes the use of DataReader in the database and the comparison of the use of conditions, including the use of DataReader content, I hope that the PHP tutorial interested in a friend helpful.