php查询mysql大量数据造成内存不足的解决方法_php技巧
本文实例分析了php查询mysql大量数据造成内存不足的解决方法。分享给大家供大家参考。具体分析如下:
一、问题
使用php查询mysql大数据量的时候,程序尚未执行完毕,跳出警告:
Fatal error: Allowed memory size of 100663296 bytes exhausted (tried to allocate 103 bytes)
错误提示:php所分配到的100M内存被占用完毕。
二、解决方法:
最简单的解决办法是:在执行文件的头部增加:
1 |
|
把内存增加到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程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

The Sesame Open Door Web3 registration portal is located on the "Register" button on the homepage of its official website. The registration steps include: 1. Visit the official website, 2. Click the "Register" button, 3. Fill in the registration information, 4. Verify the email, 5. Set up and connect to the digital wallet, 6. Complete the registration.
