PHP handles a large amount of data that does not exceed memory limits

Source: Internet
Author: User

For example, from two tables to isolate the millions data, now need to assemble the data together and then inserted into another table, in addition to the use of array assembly, what can be handled, if the use of the array of words how to ensure that the memory limit is not exceeded.


mysql_query function Query is the way to query all results after the cache into memory, so that the phenomenon of hyper-memory, the use of another function mysql_unbuffered_query can solve this problem, mysql_unbuffered_ Instead of caching the result set, query will immediately manipulate the result set after querying the data, which means that the query side returns, so that there is no excess memory, but using mysql_unbuffered_query is not a time to use mysql_num_rows () and Mysql_data_seek (). And before sending a new SQL query to MySQL, you must extract the result rows from all the SQL queries that were not cached. For example:

code that uses the cached result set:

function Selecttest () {
try {
$pdo = new PDO ("Mysql:host=localhost;dbname=test", ' Root ', ' 123456 ');//Do not use cached result set method
$pdo->setattribute (Pdo::mysql_attr_use_buffered_query, false); $sth = $pdo->prepare (' SELECT * from Test ');
$sth->execute ();
Echo ' initially occupies memory size: '. Memory_get_usage (). "\ n";
$i = 0;
while ($result = $sth->fetch (PDO::FETCH_ASSOC)) {
$i + 1;
if ($i > 10) {
Break        Sleep (1); Print_r ($result);
Echo ' takes up memory size: '. Memory_get_usage ().    "\ n"; } catch (Exception $e) {
echo $e->getmessage (); } }

errors that exceed memory will be reported at execution time:



1. {main} () e:\programdevelopment\runtimeenvironment\xampp\htdocs\test\test.php:0 0.0005 135568 2. Test->s Electtest () e:\programdevelopment\runtimeenvironment\xampp\htdocs\test\test.php:85 0.0050 142528 3. Pdostatement->execute () e:\programdevelopment\runtimeenvironment\xampp\htdocs\test\test.php:56

when the $pdo->setattribute (Pdo::mysql_attr_use_buffered_query, False) in the above code is removed and the result set is not cached, the result of running the function is as follows:

Initial Memory Size: 144808Array (
 [id] => 1
 [a] => v
 [b] => w
 [c] => i
)
footprint: 145544    Array (
 [id] => 2
 [a] => b
 [b] => L
 [c] => Q
)
occupied memory size: 145544Array (
 [ID] => 3
 [a] => m
 [b] => P
 [c] => H
)
occupied memory size: 145536Array (
 [id] = > 4
 [a] => J
 [b] => i
 [c] => b
)
occupied memory size: 145536

you can see that when you return a data memory footprint is very small, it is more than 700 bytes, so there will be no more than memory error.

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.