内存使用情况从数据库导出到php中的csv

2023-12-01

我需要将数据从 mysql 导出到 csv。我必须从多个表中选择数据,将它们放入数组中,然后处理它们并将它们作为 .csv 返回到浏览器。 我注意到数组消耗了大量的行。 例如,我在数据库中导入了一个1.8M的.csv,然后我尝试从数据库中以.csv导出此数据。 memory_get_peak_usage() 显示超过 128M 来存储带有数据的数组。

例如这个小数组需要超过 700 个字节:

$startMemory = memory_get_usage();  
        //get constant fields of the subscriber
        $data = array(array('subscriber_id' => 1315444, 'email_address' => '[email protected]',
                            'first_name' => 'Michael', 'last_name' => 'Allen'));
        echo memory_get_usage() - $startMemory;

因此,即使导出几兆字节的数据,php 脚本也需要数百兆字节的内存。 有办法解决这个问题吗? 表格:

    CREATE TABLE `subscribers` (
     `subscriber_id` int(10) unsigned NOT NULL auto_increment,
     `list_id` int(10) unsigned NOT NULL,
     `account_id` int(10) unsigned NOT NULL,
     `email_address` varchar(100) collate utf8_unicode_ci NOT NULL,
     `first_name` varchar(50) collate utf8_unicode_ci NOT NULL default '',
     `last_name` varchar(50) collate utf8_unicode_ci NOT NULL default '',
     `ip` int(10) unsigned default NULL COMMENT '\nThe ip address of the subscriber that we can get when he opens the \nthe email or subscribe using subsribe form.\nTheoretically it can be used to segment by Location (which is not correct if someone uses proxy).',
     `preferred_format` tinyint(4) NOT NULL default '0' COMMENT 'Preferred format of \n0 - HTML, \n1 -Text,\n2 - Mobile',
     `state` tinyint(4) NOT NULL default '1' COMMENT '1 - subscribed\n2 - unsubscribed\n3 - cleaned\n4 - not confirmed, it means the user subscribed but has not confirmed it yet.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n',
     `cause_of_cleaning` tinyint(4) NOT NULL default '0' COMMENT '\nThis field is the cause of moving the subscriber to the \n0 - not used\n1 - spam complaint\n2 - hard bounce\n3 - several soft bounces',
     `date_added` datetime NOT NULL COMMENT 'The data when the subscriber was added. I suppose this field can be used in the conditions forming the segment',
     `last_changed` datetime NOT NULL,
     PRIMARY KEY  (`subscriber_id`),
     UNIQUE KEY `email_list_id` (`email_address`,`list_id`),
     KEY `FK_list_id` (`list_id`),
     CONSTRAINT `FK_list_id` FOREIGN KEY (`list_id`) REFERENCES `lists` (`list_id`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB 
    CREATE TABLE `subscribers_multivalued` (
     `id` int(10) unsigned NOT NULL auto_increment,
     `subscriber_id` int(10) unsigned NOT NULL,
     `field_id` int(10) unsigned NOT NULL,
     `value` varchar(100) collate utf8_unicode_ci NOT NULL,
     `account_id` int(10) unsigned NOT NULL COMMENT '\nThe identifier of the account',
     PRIMARY KEY  (`id`),
     KEY `subscriber_fk` (`subscriber_id`),
     KEY `field_fk` (`field_id`),
     CONSTRAINT `field_fk_string_multivalued` FOREIGN KEY (`field_id`) REFERENCES `custom_fields` (`field_id`) ON DELETE CASCADE ON UPDATE CASCADE,
     CONSTRAINT `subscriber_fk_multivalued` FOREIGN KEY (`subscriber_id`) REFERENCES `subscribers` (`subscriber_id`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB
CREATE TABLE `subscribers_custom_data_string` (
 `subscriber_id` int(10) unsigned NOT NULL,
 `field_id` int(10) unsigned NOT NULL,
 `value` varchar(255) collate utf8_unicode_ci NOT NULL,
 `account_id` int(10) unsigned NOT NULL COMMENT '\nThe identifier of the account',
 PRIMARY KEY  (`subscriber_id`,`field_id`),
 KEY `subscriber_fk` (`subscriber_id`),
 KEY `field_fk` (`field_id`),
 CONSTRAINT `field_fk_string` FOREIGN KEY (`field_id`) REFERENCES `custom_fields` (`field_id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `subscriber_fk_string` FOREIGN KEY (`subscriber_id`) REFERENCES `subscribers` (`subscriber_id`) ON DELETE CASCADE ON UPDATE CASCADE
) 

还有其他字段表类似于带有数字、日期字符串的表。对于他们来说,主键是subscriber_id、field_id。

当查询失败时(例如我们有几个自定义字段):

SELECT subscribers.email_address, subscribers.first_name, subscribers.last_name, GROUP_CONCAT(t1.value SEPARATOR '|') AS 颜色,GROUP_CONCAT(t2.value SEPARATOR '|') AS 语言 从subscribers左连接subscribers_multivalued AS t1ON 订阅者.subscriber_id=t1.subscriber_id AND t1.field_id=112 左连接subscribers_multivalued AS t2ON 订阅者.subscriber_id=t2.subscriber_id AND t2.field_id=111 哪里(list_id=40) 通过...分组subscribers.email_address, subscribers.first_name, subscribers.last_name

它会返回这个:

[电子邮件受保护]米歇尔·布什 红色|红色|蓝色|蓝色 英语|西班牙语|英语|西班牙语 而不是 测试[电子邮件受保护]米歇尔·布什 红|蓝 英语|西班牙语

感谢您提供任何信息。


仅使用两个表:

您的原始查询:

SELECT subscribers.email_address, 
       subscribers.first_name, 
       subscribers.last_name, 
       t1.value AS Languages 
  FROM subscribers 
  LEFT JOIN (SELECT subscriber_id, 
                    field_id, 
                    GROUP_CONCAT(value SEPARATOR '|') AS value 
               FROM subscribers_multivalued 
              WHERE field_id=37 
              GROUP BY subscriber_id, field_id
            ) AS t1 
         ON subscribers.subscriber_id=t1.subscriber_id 
        AND t1.field_id=37 
 WHERE (list_id=49) 
   AND (state=1)

给出了一个解释计划:

id  select_type  table                    type  possible_keys  key         key_len  ref    rows  Extra
1   PRIMARY      subscribers              ref   FK_list_id     FK_list_id  4        const  2     Using where
1   PRIMARY      <derived2>               ALL   NULL           NULL        NULL     NULL   5      
2   DERIVED      subscribers_multivalued  ALL   field_fk       field_fk    4               11    Using filesort

我的加盟建议:

SELECT subscribers.email_address, 
       subscribers.first_name, 
       subscribers.last_name, 
       GROUP_CONCAT(t1.value SEPARATOR '|') AS Languages 
  FROM subscribers 
  LEFT JOIN subscribers_multivalued t1 
         ON subscribers.subscriber_id=t1.subscriber_id 
        AND t1.field_id=37 
 WHERE (list_id=49) 
   AND (state=1)
 GROUP BY subscribers.email_address, 
          subscribers.first_name, 
          subscribers.last_name

给出了一个解释计划:

id  select_type  table        type  possible_keys           key            key_len  ref                             rows  Extra
1   SIMPLE       subscribers  ref   FK_list_id              FK_list_id     4        const                           2     Using where; Using filesort
1   SIMPLE       t1           ref   subscriber_fk,field_fk  subscriber_fk  4        test.subscribers.subscriber_id  1      

虽然我只用非常少量的数据填充这两个表,但这表明我的查询版本将更有效地针对数据库执行,因为它不使用查询生成的派生表。

其他表可以以大致相同的方式链接到查询中,并且整个结果直接假脱机到 csv 文件,而不是使用 PHP 进一步解析。

这应该会给你带来更快的运行速度和更高的内存效率。

EDIT

SELECT subscribers.email_address, 
       subscribers.first_name, 
       subscribers.last_name, 
       GROUP_CONCAT(DISTINCT t1.value SEPARATOR '|') AS Colors, 
       GROUP_CONCAT(DISTINCT t2.value SEPARATOR '|') AS Languages 
  FROM subscribers 
  LEFT JOIN subscribers_multivalued AS t1 
         ON subscribers.subscriber_id=t1.subscriber_id 
        AND t1.field_id=112 
  LEFT JOIN subscribers_multivalued AS t2 
    ON subscribers.subscriber_id=t2.subscriber_id 
   AND t2.field_id=37 
 WHERE (list_id=49) 
 GROUP BY subscribers.email_address, 
          subscribers.first_name, 
          subscribers.last_name

注意 GROUP_CONCAT() 函数中 DISTINCT 的使用

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

内存使用情况从数据库导出到php中的csv 的相关文章

  • JS 无法查询 MySQL 数据库。错误:connection.query 不是函数

    尝试查询我的 mySQL 数据库 但我不能 因为我得到了TypeError connection query is not a function错误 有谁知道为什么吗 我不知道为什么会发生这种情况 数据库 js const fs requi
  • PHP 数组按月和总和分组

    我有一个 php 数组 如第一张图片所示 我想要的只是按月获取值的总和 如下所示 我知道这对我们大多数人来说可能很容易 但说实话 我找不到可行的方法来实现它 我尝试在谷歌上搜索 但我找到的所有结果都是关于对数据库查询结果进行分组和求和 而对
  • ios-使用 firebase 发送通知

    我正在尝试创建带有通知的应用程序 通知必须从 php 文件发送到 firebase 然后发送到设备 起初 我尝试使用 firebase 控制台发送 效果完美 但是当我尝试使用 php 发送通知时 我遇到了问题 显示已成功发送 但我没有收到任
  • PHP PDF生成问题

    我使用 FPDF 在 PHP 中创建 pdf 我使用会话变量将变量在一种表单之间传递到另一种表单 当我提供一个值时 Report php
  • 用于提取 FDF 数据的 PHP 正则表达式代码

    我正在尝试使用 PHP 和正则表达式解析 FDF 文件 但我就是无法理解正则表达式 我一直在解析文件以生成数组 FDF 1 2 1 0 obj lt lt FDF lt lt Fields lt lt V email protected c
  • 为什么我的 PHP 字符串比较失败?

    我有以下代码片段 if summary CFD funding Interest Paid summary Commissions summary Closing trades print summary date reference de
  • php洗一副牌

    我想使用 php 创建随机桥手的集合 我认为我可以将有序的卡片包编码为字符串 deal下面 我喜欢它有 52 个字母 同时考虑大小写 我发现了 php 函数str shuffle 所以我想我可以做以下事情 pack abcdefghijkl
  • 如何在 PHP 中识别请求的页面

    有没有简单的方法来识别最初处理请求的文件 忽略获取参数并处理 至少是基本的 映射 例如 to index php 理想情况下 我正在寻找类似的东西 SERVER REQUEST URI 但无论 get 参数如何 它都会返回相同的值 并且该值
  • 将 Zend Framework 最小化为 Zend_Mail? [复制]

    这个问题在这里已经有答案了 可能的重复 在没有实际框架的情况下使用 Zend Framework 组件 https stackoverflow com questions 1402989 use zend framework compone
  • 将父产品名称添加到 WooCommerce 中的每个购物车项目名称中

    我想在购物车页面中显示父产品名称和子产品名称 购物车项目 以供我使用分组产品 我在链接产品 gt 添加子产品的分组产品下选择父产品数据作为分组产品 模板中的代码cart php echo apply filters woocommerce
  • PHP exec() 返回值是什么?

    我正在尝试使用 PHP exec 函数 如果 return var 参数与输出参数一起存在 那么执行命令的返回状态将被写入此 多变的 如果执行成功 则为 0 但是 如果出现错误 则可能是多个其他整数 我似乎无法在任何地方找到这些整数对应的内
  • 在 Entity-Framework Core 中批量插入到 MySQL [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我有一个由约 10 000 个对象组成的列表 比方说类Person 我需要将其插入到 MySQL 表中
  • 浏览器关闭后从数据库中删除

    我正在开发一个电子商务应用程序 但问题是 当用户将产品添加到购物车并在订购前关闭浏览器时 购物车会带走所有产品 所有购物车项目都保存在表中 如果用户关闭浏览器而不订购 我只想刷新购物车 您可以使用 Javascript 事件捕获浏览器关闭并
  • 接口实现:声明必须兼容

    我有界面 interface AbstractMapper public function objectToArray ActiveRecordBase object 和课程 class ActiveRecordBase class Pro
  • 客户端与服务器端图像压缩[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在研究用户可以上传图片的东西 图像大小不受限制 现在我有两个选择使用PHP 服务器端 压缩图像或使用
  • NicEdit 数据不在 POST 中

    我确信我在这里错过了一些非常简单的东西 我已经搜索过 但似乎找不到答案 用这个简单的形式 我如何将 NicEdit 框中的内容发送到我的 HTTP POST 我得到的只是原始文本区域值而不是编辑后的版本 h2 Test Page h2
  • 排除任何字段中具有 NULL 值的行结果?

    我有一个像这样的简单选择 SELECT FROM table WHERE fk id 10020 它可以工作 但有一些字段为 NULL 没有模式所以做了 SELECT FROM table WHERE fk id 10020 AND NOT
  • 如何使用存储过程 SQL SERVER 2008 R2(mssql) 插入 PHP 数组值

    我有这个数组 REV Array 0 gt 240 1 gt 241 2 gt 242 3 gt 243 4 gt 249 我现在使用下面的代码进行插入 将每个数组的元素存储在带有 id userID Type 和 Date 的行中 if
  • 重定向后丢失会话变量

    用户填写用户名和密码 如果正确 页面会加载一些信息 例如user id到会话变量 该脚本制作了一个header Location 重定向 不知何故 下一页无法识别会话 怎么会 重定向到同一个域 并且所有页面都有session start 我
  • 为什么是“<”?不再工作而只有“

    我正在使用xampp http www apachefriends org en xampp html在本地开发 然后我从直接安装程序安装了 PHP 现在 在我的一些 PHP 代码中 只有以以下开头的 PHP 代码

随机推荐