Comparison of sorting in PHP and MySQL databases and detailed usage condition _ MySQL

Source: Internet
Author: User
Comparison of sorting in the PHP and MySQL databases and detailed description of the usage conditions the comparison and use conditions of sorting in the PHP and MySQL databases are the main content we will introduce in this article. generally, the CPU, memory, and hard disk loads must be considered for execution efficiency. if the MySQL server and PHP server have been configured in the most appropriate way, the Scalability of the system will be applied) and User-perceived Performance are our main goals. In actual operation, data in MYSQL is often stored in memory in HASH tables, BTREE, and other ways, and the operation speed is very fast. at the same time, indexes have been pre-sorted. in many applications, mySQL sorting is the first choice.

Compared with MySQL, PHP has the following advantages:

1. Considering the scalability and overall performance of the entire website, sorting at the application layer (PHP) will significantly reduce the database load and thus improve the scalability of the entire website. In fact, the cost of database sorting is very high, which consumes memory and CPU. if there is a lot of concurrent sorting, it is easy for DB to reach the bottleneck.

2. if there is still a data middle layer between the application layer (PHP) and MYSQL, which can be used properly, PHP will have better benefits.

3. the data structure of PHP in the memory is specially designed for specific applications, which is more concise and efficient than the database;

4. PHP does not need to consider data disaster recovery issues, which can reduce operation loss;

5. PHP does not have the table lock problem;

6. sorting in MySQL, requests and returned results still need to be performed through a network connection. after sorting in PHP, you can directly return results, reducing network I/O.

As for the execution speed, there should be no big difference, unless there is a problem with the application design, resulting in a large number of unnecessary network IO. In addition, the application layer should pay attention to the PHP Cache settings. if it exceeds the threshold, an internal error will be reported. in this case, make an evaluation based on the application or adjust the Cache. The specific selection depends on the specific application.

Better sorting in PHP:

1. the data source is not in MySQL, and there are hard disks, memory, or network requests;

2. data is stored in MySQL, with a small amount of data and no corresponding indexes. in this case, the data is retrieved and sorted in PHP faster;

3. data sources come from multiple MySQL servers. in this case, data is retrieved from multiple MySQL servers and sorted in PHP faster;

4. in addition to MySQL, there are other data sources, such as hard disk, memory, or network requests. it is not suitable to store the data in MySQL and sort it again;

List some instances that must be sorted in MySQL:

1. the sorting index already exists in MySQL;

2. the data volume in MySQL is large, and the result set requires a small subset. for example, for the first 1000000 rows of data, TOP 10 is used;

3. for one-time sorting and multiple calls, such as statistical aggregation, which can be used by different services, sorting in MySQL is the first choice. In addition, for deep data mining, complex operations such as sorting are usually performed at the application layer, and the results can be saved to MySQL for multiple use.

4. no matter where the data source comes from, when the data volume reaches a certain scale, it is no longer suitable for sorting in PHP due to the memory/Cache usage; in this case, data is copied, imported, or MySQL exists, and optimized with INDEX, which is better than PHP. However, it is better to use Java or even C ++ to handle such operations. Some data similar to aggregate or aggregate large datasets is not worth the cost of sorting on the client. Of course, similar search engines can also be used to solve similar applications.

From the overall perspective of the website, you must consider manpower and costs. If the website size and load are small, and the manpower is limited (the number and capability may be limited), at this time, the application layer (PHP) should do a lot of development and debugging work, which takes time and outweighs the loss; it is better to process in DB, which is simple and fast. For large-scale websites, the cost of electricity and servers is very high, and the careful calculation of the system architecture can save a lot of cost, which is necessary for the company's sustainable development; at this time, if the application layer (PHP) sort and meet business requirements, and try to do so at the application layer.

The above is the comparison of sorting in PHP and MySQL databases and detailed description of usage conditions _ MySQL content. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.