About the PHP query database caused by insufficient memory solution

Source: Internet
Author: User
Tags mysql insert mysql query
Querying the database causes memory shortage how to solve? This paper mainly introduces PHP query MySQL a large amount of data caused by the memory shortage of the solution, the case of MySQL query the reason for the lack of memory and the corresponding solution, and a more detailed analysis of the use of mysql_unbuffered_query function skills, We hope to help you.

In this paper, we analyze the problem that PHP query MySQL large amount of data caused memory shortage. Share to everyone for your reference. The specific analysis is as follows:

First, the question

When using PHP to query MySQL large data volume, the program has not been completed, out of warning:
Fatal error:allowed memory size of 100663296 bytes exhausted (tried to allocate 103 bytes)
Error: The 100M memory allocated by PHP is occupied.

Second, the solution:

The simplest solution is to increase the head of the execution file:


Ini_set (' Memory_limit ', ' 256M ');


Increase memory to 256M or more to increase the amount of memory used by PHP
But the next time to read more data what to do, can not be increased again and again, resulting in the server's memory is eaten by PHP.

A function is described here:

Use the Memory_get_usage () method to get the amount of memory used by PHP. It is found that as the number of read data increases, PHP uses more memory in one step.
Does PHP have data in memory when querying MySQL? Search, and found that it is probably the meaning.
MySQL's C API functions have Mysql_use_result () and Mysql_store_result ()
Mysql_store_result () reads the result set from MySQLServer to the client, and Mysql_use_result () simply reads the meta information of the result set

1, PHP mysql_query call is Mysql_store_result (), automatically get and cache the result set
2, while the other PHP function Mysql_unbuffered_query () is called the Mysql_use_result (), on the one hand, this in the processing of a large result set will save considerable memory. On the other hand, you can manipulate the result set immediately after getting the first row, rather than waiting for the entire SQL statement to complete.

So when we are reading a lot of data, we can use Mysql_unbuffered_query () instead of mysql_query (). It's been tested, indeed. And quite to the force, after all the data memory has been kept within 1MB, no growth
Mysql_unbuffered_query () sends a SQL query to MySQL, but does not automatically get and cache the result set like mysql_query (). On the one hand, this can save considerable memory when dealing with very large result sets. On the other hand, you can manipulate the result set immediately after getting the first row, rather than waiting for the entire SQL statement to complete. When you use multiple database connections, you must specify an optional parameter, Link_identifier.
The benefits of Mysql_unbuffered_query () are at a cost: mysql_num_rows () and Mysql_data_seek () cannot be used on the result set returned by Mysql_unbuffered_query (). In addition, before sending a new SQL query to MySQL, you must extract the resulting rows from all non-cached SQL queries.
So be sure to combine your business needs with proper selection of functions

Related recommendations:

How php resolves and avoids duplicate submissions of form forms

PHP Troubleshooting Session Deadlock Method _php tutorial

php resolves MySQL insert data appears incorrect String value: & #39 #39;xf0x9fx92x8bti...&; 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.