Home Backend Development PHP Tutorial php查询mysql大量数据造成内存不足的解决方法_PHP

php查询mysql大量数据造成内存不足的解决方法_PHP

May 31, 2016 pm 01:16 PM
mysql php Large amounts of data Inquire

本文实例分析了php查询mysql大量数据造成内存不足的解决方法。分享给大家供大家参考。具体分析如下:

一、问题

使用php查询mysql大数据量的时候,程序尚未执行完毕,跳出警告:
Fatal error:  Allowed memory size of 100663296 bytes exhausted (tried to allocate 103 bytes)
错误提示:php所分配到的100M内存被占用完毕。

二、解决方法:

最简单的解决办法是:在执行文件的头部增加:

ini_set('memory_limit','256M'); 
Copy after login

把内存增加到256M或者更多,可以增大php所使用的内存空间
但是下次若要读取更多的数据该怎么办呢,总不能一次次的增加,导致服务器的内存都被php吃光。

这里介绍一个函数:

使用memory_get_usage()方法获得php使用的内存量。发现随着读取数据条数的增加,php使用的内存在一步步增加。
难道php在查询mysql时的数据是存在内存中的?搜索了一下,发现果真大概就是这个意思。
mysql的C API函数有mysql_use_result()和mysql_store_result()
mysql_store_result()会把结果集从mysqlServer读到客户端,而 mysql_use_result()只是读取了结果集的元信息

1、php的mysql_query调用的是mysql_store_result(),自动获取并缓存结果集
2、而php的另一个函数mysql_unbuffered_query()则是调用的 mysql_use_result(),一方面,这在处理很大的结果集时会节省可观的内存。另一方面,可以在获取第一行后立即对结果集进行操作,而不用等到整个 SQL 语句都执行完毕。

所以我们在读取大量数据的时候,可以使用mysql_unbuffered_query()来替代mysql_query()。经测试,的确如此。而且相当给力,导完所有数据内存一直保持在1MB以内,没有增长过
mysql_unbuffered_query() 向 MySQL 发送一条 SQL 查询 query,但不像 mysql_query()那样自动获取并缓存结果集。一方面,这在处理很大的结果集时会节省可观的内存。另一方面,可以在获取第一行后立即对结果集进行操作,而不用等到整个 SQL 语句都执行完毕。当使用多个数据库连接时,必须指定可选参数 link_identifier。
mysql_unbuffered_query()的好处是有代价的:在 mysql_unbuffered_query()返回的结果集之上不能使用 mysql_num_rows() 和 mysql_data_seek()。此外在向 MySQL 发送一条新的 SQL 查询之前,必须提取掉所有未缓存的 SQL 查询所产生的结果行。
所以一定要结合自己的业务需求适当的选取函数

希望本文所述对大家的php程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

See all articles