简体   繁体   English

在数据库中存储序列化的ruby对象

[英]Storing serialized ruby object in database

I would like to store very large sets of serialized Ruby objects in db (mysql). 我想在数据库(mysql)中存储非常大的序列化Ruby对象集。

1) What are the cons and pros? 1)优点和缺点是什么?
2) Is there any alternative way? 2)还有其他方法吗?
3) What are technical difficulties if the objects are really big? 3)如果物体很大,会有哪些技术难题?
4) Will I face memory issues while serializing and de-serializing if the objects are really big ? 4)如果对象真的很大,在序列化和反序列化时会遇到内存问题吗?

Pros 优点

  • Allows you to store arbitrary complex objects 允许您存储任意复杂的对象
  • Simplified your db schema (no need to represent those complex objects) 简化了数据库架构(无需表示那些复杂的对象)

Cons 缺点

  • Complicates your models and data layer 使模型和数据层复杂化
  • Potentially need to handle multiple versions of serialized objects (changes to object definition over time) 潜在地需要处理序列化对象的多个版本(随着时间的推移对对象定义的更改)
  • Inability to directly query serialized columns 无法直接查询序列化的列

Alternatives 备择方案

As the previous answer stated, an object database or document oriented database may meet your requirements. 如先前的回答所述,对象数据库或面向文档的数据库可能满足您的要求。

Difficulties 难点

If your objects are quite large you may run into difficulties when moving data between your DBMS and your program. 如果对象很大,则在DBMS和程序之间移动数据时可能会遇到困难。 You could minimize this by separating the storage of the object data and the meta data related to the object. 您可以通过分离存储对象数据和与对象相关的元数据来最大程度地减少这种情况。

Memory Issues 记忆问题

Running out of memory is definitely a possibility with large enough objects. 对于足够大的对象,内存不足肯定是可能的。 It also depends on the type of serialization you use. 它还取决于您使用的序列化类型。 To know how much memory you'd be using, you'd need to profile your app. 要知道您将使用多少内存,您需要分析您的应用程序。 I'd suggest ruby-prof, bleak_house or memprof. 我建议使用ruby-prof,bleak_house或memprof。


I'd suggest using a non-binary serialization wherever possible. 我建议尽可能使用非二进制序列化。 You don't have to use only one type of serialization for your entire database, but that could get complex and messy. 您不必为整个数据库仅使用一种类型的序列化,但这可能会变得复杂且混乱。

If this is how you want to proceed, using an object oriented dbms like ObjectStore or a document oriented dbms like CouchDB would probably be your best option. 如果这是您要继续的方式,则使用ObjectObject等面向对象的dbms或CouchDB等面向文档的dbms可能是您的最佳选择。 They're better designed and targeted for object serialization. 它们针对对象序列化进行了更好的设计和定位。

As an alternative you could use any of the multitude of NoSQL databases. 作为替代方案,您可以使用多种NoSQL数据库中的任何一种。 If you can serialize your object to JSON then it should be easily stored in CouchDB. 如果可以将对象序列化为JSON,则应将其轻松存储在CouchDB中。

You have to bear in mind that the serialized objects in terms of disk space are far larger than if you saved them in your own way, and loaded them in your own way. 您必须牢记,就磁盘空间而言,序列化的对象要比以自己的方式保存并以自己的方式加载它们的对象大得多。 I/O from the hard drive is very slow and if you're looking at complex objects, that take a lot of processing power, it may actually be faster to load the file(s) and process it on each startup; 硬盘驱动器的I / O速度非常慢,如果您要查看复杂的对象,这些对象需要大量的处理能力,则实际上加载文件并在每次启动时对其进行处理可能会更快。 or perhaps saving the data in such a way that's easy to load. 或者以易于加载的方式保存数据。

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

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