简体   繁体   English

从MySQL中提取大量数据

[英]Pull huge data from MySQL

Trying to pull huge data [about 200,000 rows] from MySQL using PHP. 尝试使用PHP从MySQL中提取大量数据[大约200,000行]。 Getting the error: PHP Fatal error: Allowed memory size of xxxxx bytes exhausted 获取错误: PHP致命错误:允许的内存大小xxxxx字节耗尽

I have an application which generates almost 80-90 thousand trasnactions every month. 我有一个应用程序,每月产生近80-90万个trasnactions。 This is a growing application, so I can expect the numbers to grow rapidly. 这是一个不断增长的应用程序,因此我可以预期这些数字会快速增长。 While generating the monthly or quaterly report, I am getting the memory size error. 在生成每月或季度报告时,我收到内存大小错误。 I tried to increase the allowed memory and it worked. 我试图增加允许的内存,它工作。 But I know allowing more memory is a temprary fix and is not a good practice in long run. 但我知道允许更多的记忆是一种临时修复,从长远来看并不是一个好习惯。

I need some help in scaling this application. 我需要一些帮助来扩展这个应用程序。

In my opinion, breaking the query result into multiple manageable chunks and join them together should solve the problem. 在我看来,将查询结果分成多个可管理的块并将它们连接在一起应该可以解决问题。

Is there any alternative/better solution? 有没有替代/更好的解决方案?

Does NoSQL helps here? NoSQL有帮助吗? how? 怎么样?

Edit: 编辑:

Here is the way data is used: The transaction table containes all transaction details along with the references to master data like user-id, place/location id, type of transacation. 以下是数据的使用方式:事务表包含所有事务详细信息以及对主数据的引用,如user-id,place / location id,transacation类型。

The report will: 该报告将:

  • Pull the records from transaction table 从事务表中提取记录
  • Replace master references appropriately [replace user-id with name, address, etc] 适当地替换主引用[用户名替换姓名,地址等]
  • Dump the data in csv/xls format 以csv / xls格式转储数据
  • Store/download the file 存储/下载文件

Thanks 谢谢

Could you not use Mysql Aggregate functions? 你能不能使用Mysql Aggregate函数? They should always be quicker than to pull out all records and then process them. 它们应该总是比拔出所有记录然后处理它们更快。 https://dev.mysql.com/doc/refman/8.0/en/group-by-functions-and-modifiers.html https://dev.mysql.com/doc/refman/8.0/en/group-by-functions-and-modifiers.html

Insert this line of code into your script at the top: 将这行代码插入到顶部的脚本中:

ini_set("memory_limit","128M"); // size based on your requirement if you need more than 128M then u can add this value in the place of 128M

Or another option is use Mysql command line if possible: 或者另一个选项是尽可能使用Mysql命令行:

Use yourdatabasename
source  mysqlfile

A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate. 生成器允许您编写使用foreach迭代一组数据的代码,而无需在内存中构建数组,这可能会导致超出内存限制,或者需要相当长的处理时间来生成。 Instead, you can write a generator function, which is the same as a normal function, except that instead of returning once, a generator can yield as many times as it needs to in order to provide the values to be iterated over. 相反,您可以编写一个生成器函数,它与普通函数相同,除了不是返回一次,生成器可以生成所需的次数,以便提供要迭代的值。 for example 例如

function getRecords()
{
  $sql = 'SELECT field1, field2,field3 FROM table';
  $results=$db->query($sql);
  while ($row = $results->fetch_assoc())) {
    yield $row;
 }
}

foreach ($this->getRecords() as $record) {
  //process records here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM