什么是序列化/反序列化的DataTable /从Redis的最省时的方式是什么?序列化、省时、方式、Redis

2023-09-04 23:08:09 作者:倾城

我想存储复杂的对象,如数据表数据集等,在Redis的。我试图把它们称为使用 JsonSerialize A BLOB对象序列,但它需要太多的时间。有没有其他办法?

I want to store complex objects such as a DataTable or Dataset, etc, in Redis. I have tried to serialize them as a BLOB object using JsonSerialize, but it takes too much time. Is there any other way?

推荐答案

不幸的是大数据时,将其设置将始终以时间序列化和反序列化的结构。 数据表取值特别是相当复杂的对象,因为它们具有行和列,往往有很多附连到他们的元数据 - 即使它似乎是一个基本表中。

Unfortunately when working with large data sets it will always take time to serialize and deserialize the structure. DataTables in particular are fairly complex objects, as they have rows and columns which often have a lot of meta data attached to them - even when it appears to be a basic table.

考虑是否真的需要被序列化作为数据表。你可以创建一个简单的POCO和序列化名单,其中,YourRecord> ?换句话说,如果你不上的字段和列需要额外的属性,你可以序列化到一个简单的格式,它有可能更快,更节省空间的缓存;然后恢复到数据表如果有必要的。

Consider whether you really need to be serializing as a DataTable. Could you create a simpler POCO and serialize a List<YourRecord>? In other words, if you don't need extra attributes on fields and columns and you can serialize to a simpler format, it's likely quicker, and more space efficient in the cache; and then restore to a DataTable if necessary.

另一种选择是将数据表分裂成更小的组,您序列化并存储在更小的部分。您可能会发现这个更好的性能。 您应该能够标杆这一点。的

Another option is to split the DataTable into smaller sets, that you serialize and store in smaller parts. You may find this more performant. You should be able to benchmark this.

最终的Redis的缓存应该是一个改进,重新查询数据源所花费的时间。您可以使用术语需要太多的时间,但如果需要2秒内从缓存中得到对8秒,查询数据源,然后这是一个显著的推动作用。但可以肯定的唯一途径就是标杆。

Ultimately your Redis cache should be an improvement over the time taken to re-query the data source. You use the term takes too much time, but if it takes 2 seconds to get from the cache vs 8 seconds to query the data source then that is a significant boost. But the only way to be sure is to benchmark.

设置你的环境,所以你只运行必需的工具。 不要同时运行的基准测试执行其他任务,这样你就不会引入任何偏差。的

记录才能序列化数据表的时间。执行此操作很多次的平均水平。

Record the time it takes to serialize a DataTable. Perform this action many times and average.

var start = DateTime.Now;
// Serialize
var duration = DateTime.Now - start;

试验不同的尺寸数据表和看到,如果你找到一个可以接受的时间。

Experiment with different sizes of DataTables and see if you find an acceptable time.

尝试不同的序列化库,如JSON.NET。虽然这是很好的保持它的所有ServiceStack,这可以帮助你确定它是否是ServiceStack.Text的缺点,或只是一个问题的大型数据集。

Try a different serialization library, such as JSON.NET. While it's nice to keep it all ServiceStack, this can help you determine if it's a shortcoming of ServiceStack.Text or just an issue with the large dataset.

重复此过程,反序列化。

Repeat the process for deserialization.

如果您正在使用大型数据集,也无论您的应用程序和缓存有足够的内存?在应用程序内存可能是一个瓶颈;你应该看你的系统的活动监视器在执行操作,确保不会耗尽内存,并让你的系统中执行分页。如果您发现这种情况发生,或者考虑增加RAM,或数据表分割成更小的数据集如前所述。

If you are working with large datasets, does both your application and the cache have sufficient memory? The memory in your application could be a bottleneck; You should watch your system's activity monitor while performing the operations, and ensure you aren't running out of memory and having your system perform paging. If you find this happening, either consider increasing the RAM, or split the DataTable into smaller datasets as mentioned before.

如果要连接到一个Redis的服务器在网络上,而不是在同一台机器上,你检查网络的等待时间?您可能需要您的应用程序服务器和缓存服务器之间的ping你,并确保你确实有一个最低的Ping。特别是如果你发现缓存简单对象是缓慢的。

If you are connecting to a Redis server over a network, and not on the same machine, have you checked the latency of the network? You may want to ping your between your application server and the cache server and ensure you actually have a low ping. Particularly if you find caching simple objects is slow.

如果您发现有没有办法改善缓存和恢复,那么也许使用Redis的是不般配的时间。也许使用静态数据表应用程序存储器中会更适合。换句话说,通过保持高速缓存中的应用程序存储器中,然后就没有序列化和反序列化的后顾之忧。当然,你可能需要小心,以确保你有足够的内存可用于您的应用程序来做到这一点。 不过,我感到惊讶,如果你不得不选择此选项,虽然的。

If you are finding there is no way to improve the time to cache and restore, then maybe using Redis isn't a good fit. Perhaps using a static DataTable within the application memory would be more suitable. In other words, by keep the cache in the application memory and then there is no serialization and deserialization to worry about. Of course you may need to be careful about ensuring you have enough memory available to your application to do this. I would however be surprised if you had to choose this option though.

没有看到你的数据集或你正在建设它的服务的认识是最终只有如何最好地缩小你的问题的原因一般指教。关键的建议是不要用数据表如果一个简单的结构会做的,基准每个操作,以确定任何瓶颈。

Without seeing your dataset or knowledge of the service you are building it's ultimately only generic advise about how to best narrow down the cause of your problem. The key advise is don't use a DataTable if a simpler structure will do, and benchmark each of the operations to determine any bottlenecks.

我希望这有助于。