我必须提取 800 000 行(这是开发环境,实际上它可能是 1,甚至 150 万行)。 第二步是将该内容作为序列化数据插入到 Redis 缓存中。
我的问题是:
X 字节的允许内存大小已用完,但是...关键是我不能更改它。
我发现我可以从 MySQL 数据库中逐个获取数据。但是第二步就有问题了。到目前为止,我习惯于将数据作为 String 类型存储在 Redis 中(使用 MySQL 数据序列化 php 数组数组)。到目前为止,我还不知道如何正确地附加所获取数据的每一部分。 (获取序列化数据,反序列化,然后附加新的数据部分,然后再次序列化是……愚蠢的)。
您有大数据运营经验吗?我不知道如何处理它。
请问? :)
最好的问候。
最佳答案
在你的情况下,我会将你生成的内容作为 redis SET 命令放入文件中,然后使用 redis-cli --pipe 执行文件,在以下链接中阅读你的 redis mess 插入数据文件应该是什么样子:
<?php
while ($line = mysqli_fetch_assoc($result)) {
$data['data'] = $line['somekey']; // here generate the desired data
$data['key'] = 'thekey'; // make the data key
$redisCommnand = 'SET '.$data['key'].' '.$data['data']; // here you create the redis insert command line someDataKey
file_put_contents('/tmp/data.txt', $redisCommand, FILE_APPEND);// appending the command to redis command file
}
// exec command to run the file
exec('cat data.txt | redis-cli --pipe');
// consider removing the data.txt file
// unlink('/tmp/data.txt');
关于php - 从 MySQL 中获取大量数据。将其插入 Redis 缓存。无法增加 PHP 内存限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242362/