Exercise caution when operating the database during loop iteration of PHP programs

Source: Internet
Author: User

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.

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.