thinkphp Infinite classification and reconstruction speed is very slow

Source: Internet
Author: User
I know it's slow to write.
I know the idea, but I can't do it.
All data should be taken out at once.
And then sort out the enlighten

        function getBookmarkcategory($pid = 0, &$result = array(), $spac = 0)        {            if(MODULE_NAME=="Home"){                $ispublic=0;                $uid=session('uid');                $ispublic+="And bookmarkcategory_staffid = $uid";            }else if(MODULE_NAME=="Admin"){                $ispublic=1;            }            $Model = new Model();            $spac = $spac + 8;            $res=$Model->query( "SELECT * FROM x_bookmarkcategory WHERE bookmarkcategory_pid=$pid And bookmarkcategory_ispublic = $ispublic order by bookmarkcategory_sort");            foreach($res as $key=>$value){                $result[]=$res[$key];                getBookmarkcategory($value['bookmarkcategory_id'],$result,$spac);            }            return $result;        }        $list = getBookmarkcategory();        return $list;

Reply content:

I know it's slow to write.
I know the idea, but I can't do it.
All data should be taken out at once.
And then sort out the enlighten

        function getBookmarkcategory($pid = 0, &$result = array(), $spac = 0)        {            if(MODULE_NAME=="Home"){                $ispublic=0;                $uid=session('uid');                $ispublic+="And bookmarkcategory_staffid = $uid";            }else if(MODULE_NAME=="Admin"){                $ispublic=1;            }            $Model = new Model();            $spac = $spac + 8;            $res=$Model->query( "SELECT * FROM x_bookmarkcategory WHERE bookmarkcategory_pid=$pid And bookmarkcategory_ispublic = $ispublic order by bookmarkcategory_sort");            foreach($res as $key=>$value){                $result[]=$res[$key];                getBookmarkcategory($value['bookmarkcategory_id'],$result,$spac);            }            return $result;        }        $list = getBookmarkcategory();        return $list;

Don't say demand, table structure, who knows what a piece of code is used for

PHP Infinite Classification An algorithm can refer to the PHPCMS category table structure. It is to put all the parent (tree) nodes and the sub (tree) nodes are stored recursively into the database. You can memorize all the data to the array and then recursively.

You can refer to the answers to the following questions, "classification" and the Department of Rights Management similar
http://segmentfault.com/q/1010000004365843/a-1020000004365952

            $res=$Model->query( "SELECT * FROM x_bookmarkcategory WHERE bookmarkcategory_pid=$pid And bookmarkcategory_ispublic = $ispublic order by bookmarkcategory_sort");

Should be slow here, completely can find a way to do not put into the recursive inside query, first-time query out, and then recursion also faster than this n times ah

 function getbookmarkcategory ($pid = 0, & $result = Array (), $spac = 0) {if (module_name== "Home" ) {$ispublic = 0; $uid =session (' uid '); $ispublic + = "and Bookmarkcategory_staffid = $uid"; }else if (module_name== "Admin") {$ispublic = 1; } $Model = new Model ();//Unexpectedly, one of the reasons for recursive each seek is to have the instance model slow to the outside of the function $spac = $spac + 8; $res = $Model->query ("select * from X_bookmarkcategory WHERE bookmarkcategory_pid= $pid and Bookmarkcategory_ispublic = $ispublic ORDER by Bookmarkcategory_sort "); SELECT * This writing performance is low bookmarkcategory_pid= $pid--bookmarkcategory_pid whether to index foreach ($res as $key = = $value) { $result []= $res [$key]; Getbookmarkcategory ($value [' bookmarkcategory_id '], $result, $SPAC); } return $result; }//The best thing is that the data is removed from the outside and then recursively quickly MySQL operation is very slow 

Once the query finds all, then loops, uses the value reference to adjust the data structure, and then it comes out.

can use left and right tree
Http://wenku.baidu.com/view/cb2bb3eeaeaad1f346933ffd.html

Very high efficiency

You can do it with a data dictionary, take out all the data at once, and then cycle it once.

Because you are constantly reading data from external storage. If the amount of data is small, you can read all the required data at once and then construct the tree in memory. What is needed data can be saved when the data is generated. If the amount of data is very large, consider storing the associated data at the tree level in several reads.
PS: Look in your code, be careful with SQL injection

  • Related Article

    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.