问题描述
我正在尝试向优化 90GB 以上的表迈出一步:
I am trying to take one step towards optimizing a 90GB+ table:
旧表
桌子每天大约抢到.来自外部源的 750,000 条记录,并将它们添加到具有新日期的表中.据我所知,这种情况已经持续了三年.97% 的记录从一天到第二天都不会改变.
Every day the table grabs approx. 750,000 records from an external source and adds them to the table with the new date. This has been going on for three years from what I understand. 97% of the records don't change from one day to the next.
新表格
我正在尝试检查旧表(数百万条记录)并消除冗余,这可能会大大减少表的大小.
I am trying to go through old table (millions and millions of records) and eliminate redundancy which will likely reduce the table size quite dramatically.
old_table
- 日期
- record_id
- data_field(确实有很多字段,但为了示例)
new_table_index
- 日期
- index_id
new_table
- index_id
- record_id
- 数据字段
我们遍历 old_table 中每条记录的逻辑
if (record_id is not in new_table) or (record_id is in new_table, but the latest entry has different data_field)
if (record_id is not in new_table) or (record_id is in new_table, but the latest entry of it has a different data_field)
将其插入到 new_table 中并获取 index_id
其他
从 new_table_index 中获取该 record_id 的最新条目 index_id
总是
将 index_id 和日期插入 new_table_index
对实现此操作的最佳方法有什么想法吗?我对 MySQL 的了解不够先进,无法将所有这些放在一起.当我尝试用 PHP 编写脚本时,它用了 3GB 的内存,然后失败了.其他建议或疑问???非常感谢!
Any thoughts on optimal ways to do this? I am not advanced enough with MySQL to put this all together. When I tried writing a script in PHP it used up 3GB of memory and then failed. Other suggestions or queries??? Thanks so much!
推荐答案
我最终使用了 PHP 和 MySQL 的混合体(一开始在各方面都摇摆不定):
I ended up using a hybrid of PHP and MySQL (after swinging too far each way at first):
- 为所有前一天 PR 插入前一天的链接(使用 INSERT - SELECT)
- 将 PR 与前一天进行比较,如果更改则插入(使用 INSERT - SELECT)
- 为新更新的 PR 插入链接(使用 SELECT - php foreach - UPDATE)
- 每天添加新 PR(使用 INSERT - SELECT)
- 为新 PR 插入链接(使用 INSERT - SELECT)
仍然需要用 php foreach 循环来完善这个循环,但在大多数情况下,这成功了!感谢您的帮助!
Still need to perfect the one with the php foreach loop, but for the most part this did the trick! Thanks for all your help!
这篇关于需要 MySQL INSERT - SELECT 查询数百万条记录的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!