PHP has always believed that the database is omnipotent, but efficiency and database bottlenecks are seldom considered for the purpose of implementing functions. For example, when you query a database in a loop, it is very undesirable to query the database in an iteration method, especially the front-end program! When the traffic volume is small and the concurrency is low, no problem can be seen! Once the access volume suddenly increases, the load on the database server is too heavy when there are too many concurrent access volumes. In severe cases, the downtime may lead to unimaginable consequences. In this case, it is difficult for PHP programmers to find out the specific causes.
Let's take a look at an iterative case code. When there are more than 1000 drops in the database classification, we need to query more than 1000 drops of database queries every time we call the code. What is the fear of dropping the code? No more details are needed!
The code is as follows: |
Copy code |
/** * Recursively retrieve a category * @ Author: xxx * @ Param $ tree_id */ Function get_child_tree ($ tree_id = 0) { $ Three_arr = array (); $ SQL = "SELECT count (*) FROM TABLE WHERE parent_id =" $ tree_id "AND is_show = 1 "; If ($ GLOBALS [db]-> getOne ($ SQL) | $ tree_id = 0) { $ Child_ SQL = "SELECT ...... "; $ Res = $ GLOBALS [db]-> getAll ($ child_ SQL ); Foreach ($ res AS $ row) { If ($ row [is_show]) { $ Arr [id] = $ row [cat_id]; ...... } If (intval ($ row [cat_id])! = 0 ){ $ Three_arr [$ row [cat_id] [cat_id] = get_child_tree ($ row [cat_id]); ...... } } } Return $ three_arr; } |
There are some examples. Some people like to use the while, for, and other loops to perform database query operations. It is also not advisable, or when the traffic is small, the load on the database server is also heavy, so be careful when operating. Especially in the encapsulation of the drop function, because you will accidentally call this function several times after dropping the program, the server's drop overhead is immeasurable.