使用 Perl 处理 200 万条记录
我的数据库中有 200 万条记录,是否可以将它们全部取出并存储在 Perl 哈希引用上,而不会出现内存不足的问题?
I have 2 million records on the database is it possible to bring them all and store them on perl hash reference without any problem of reaching out of memory ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将它们全部读入内存的原因是什么?编码的速度或简易性(即将整个事物视为哈希引用)。
如果是前者,那么当然,我想,你只需要大量的内存。
如果是后者,那么还有一些有趣的选择。例如,数据库有
绑定
接口,看起来像 Perl 本机哈希,但实际上根据需要查询和返回数据。快速搜索 CPAN 显示Tie::DBI
,Tie::Hash::DBD
以及用于特定数据库、平面文件 DB 和 CSV 文件的几个绑定接口,包括我的Tie::Array::CSV
。What is your reason to read them all into memory? Speed or ease of coding (i.e. treat the whole thing as a hashref).
If its the former, then sure, I think, you just need a ton of ram.
If its the latter, then there are interesting options. For example there are
tie
d interfaces for databases that look like Perl native hashes but in reality query and return data as needed. A quick search of CPAN showsTie::DBI
,Tie::Hash::DBD
and several tied interfaces for specific databases, flat-file DBs, and CSV files, including mineTie::Array::CSV
.一方面,处理哈希中的 200 万个元素并非闻所未闻。但是,我们不知道您的记录有多大。无论如何,这听起来像是一个 XY 问题。对于您所面临的问题,这可能不是最佳解决方案。
为什么不使用 DBIx::Class 这样你的表就可以像 Perl 类一样对待(它们本身就是被美化的数据结构)? DBIx::Class::Manual 中有大量文档::DocMap。这确实是 DBIx::Class 的全部内容;让您抽象出数据库的 SQL 详细信息,并将其视为一系列类。
On the one hand, processing two million elements in a hash isn't unheard of. However, we don't know how big your records are. At any rate, it sounds like an XY problem. It may not be the best solution for the problem you're facing.
Why not use DBIx::Class so that your tables can be treated like Perl classes (which are themselves glorified data-structures)? There's a ton of documentation at DBIx::Class::Manual::DocMap. This is really what DBIx::Class is all about; letting you abstract away the SQL details of the database and treat it like a series of classes.
这完全取决于您的记录有多少数据。 Perl 散列和数组占用的内存比您想象的要多,尽管这并不疯狂。但同样,这完全取决于您的数据是什么样子以及您有多少 RAM。如果你有 RAM,Perl 不会有任何问题。
That completely depends on how much data your records have. Perl hashes and arrays take up more memory than you'd think although it's not crazy. But again, it totally depends on what your data looks like and how much RAM you have. Perl won't have any problems with it if you have the RAM.