Regarding the maximum data amount supported by THINKPHP's addAll, thinkphpaddall_PHP tutorial

WBOY
Release: 2016-07-13 10:08:21
Original
821 people have browsed it

Regarding the maximum amount of data supported by THINKPHP's addAll, thinkphpaddall

The Model operation in Thinkphp has two methods: add() and addAll

<span><span>1</span> <span>$User</span> = M("User"); <span>//</span><span> 实例化User对象</span>
<span>2</span> <span>$data</span>['name'] = 'ThinkPHP'<span>;
</span><span>3</span> <span>$data</span>['email'] = 'ThinkPHP@gmail.com'<span>;
</span><span>4</span> <span>$User</span>->add(<span>$data</span><span>);
</span><span>5</span> 
<span>6</span> <span>$dataList</span>[] = <span>array</span>('name'=>'thinkphp','email'=>'thinkphp@gamil.com'<span>);
</span><span>7</span> <span>$dataList</span>[] = <span>array</span>('name'=>'onethink','email'=>'onethink@gamil.com'<span>);
</span><span>8</span> <span>$User</span>->addAll(<span>$dataList</span>);</span>
Copy after login

The addAll method can add data in batches, which is how MySQL is used:

<pre name="code" class="sql"><span>INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);</span>
Copy after login

When there is a lot of data, try to insert in batches rather than inserting items one by one in a loop, otherwise your database will be overwhelmed and hang up.

However, if you take it for granted and store all the data into an array and perform addAll, you will also face a hang-up situation. Why is this?

The reason is that the configuration of the max_allowed_packet variable in mysql limits the length of the upload sql statement. Just configure it to be larger in the mysql configuration

<pre name="code" class="plain"><span>max_allowed_packet = 100M</span>
Copy after login


At the same time, when inserting data, you should also limit the length of batch insertion. After all, you never know when the data will become millions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/951419.htmlTechArticle Regarding the maximum amount of data supported by THINKPHP's addAll, thinkphpaddall There are two methods for Model operation in Thinkphp: add() and addAll 1 $User = M("User"); // Instantiate User object 2 $data ['name'...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!