简体   繁体   English

处理大量永久数据的最佳方法

[英]Best way to handle large amount of permanent data

I'm developing a PC app in Visual Studio where I'm showing the status of hundreds of sensors that are connected via WiFi. 我正在Visual Studio中开发PC应用程序,其中显示了通过WiFi连接的数百个传感器的状态。 The thing is that I need to hold on to the sensor data even after I close the app, so I'm considering some form of permanent storage. 问题是即使关闭应用程序后也需要保留传感器数据,因此我正在考虑某种形式的永久存储。 These are the options I've considered: 这些是我考虑过的选项:

1) My Sensor object is relatively compact with only a few properties. 1)我的Sensor对象相对紧凑,只有几个属性。 I could serialize all the objects before closing the app and load them every time the app starts anew. 我可以在关闭应用程序之前序列化所有对象,并在每次重新启动应用程序时加载它们。

2) I could throw all the properties (which are mostly strings and doubles) into a simple text file and create a custom protocol for storage and retrieval. 2)我可以将所有属性(主要是字符串和双精度)放入一个简单的文本文件中,并创建用于存储和检索的自定义协议。

3) I could integrate a database with my app. 3)我可以将数据库与我的应用程序集成。 Someone told me this is the best way to go about it, but I'm a bit hesitant seeing as I'm not familiar with DBs. 有人告诉我这是最好的解决方法,但是由于我对DB不熟悉,所以我有点犹豫。

Which method would yield the best results in terms of resource usage and speed? 哪种方法在资源使用和速度方面会产生最好的结果? Or is there some other, better way to go about this? 还是有其他更好的方法可以解决此问题?

First thing you need is to understand is your problem. 您需要了解的第一件事就是您的问题。 For example, when the program is running do you need to have everything in memory at the same time or do you work with your sensors one at a time? 例如,在程序运行时,您是否需要同时将所有内容存储在内存中?还是一次使用一个传感器?

What is a "large amount of data"? 什么是“大量数据”? For example, to me that will never be less than million (or billion in some cases). 例如,对我来说,它永远不会少于百万(在某些情况下甚至是十亿)。

Once you know that you shouldn't be scared of using something just because you are not familiar to it. 一旦您知道您不应该因为不熟悉而害怕使用某些东西。 Otherwise you are not looking for the best solution for your problem, you are just hacking around it in a way that you feel comfortable. 否则,您不是在寻找解决问题的最佳解决方案,而是以一种自在的方式来解决它。

This being said, you have several ways of doing this. 话虽这么说,您有几种方法可以做到这一点。 Like you said you can serialize data, using json to store and a few other alternatives but if we are talking about a "large amount of data that we want to persist" I would always call for the use of Databases (the name says a lot). 就像您说的那样,您可以序列化数据,使用json进行存储和其他一些选择,但是如果我们谈论的是“我们要保留的大量数据”,我总是会呼吁使用数据库(名称说了很多)。 If you don't need to have everything in memory at the same time then I believe that this is you best option. 如果您不需要同时存储所有内容,那么我相信这是您的最佳选择。

I personally don't like them (again, personal choice) but one way of not learning SQL (a lot) while you still use your objects is to use an ORM like NHibernate (you will also need to learn how to use it so you don't get things a slower). 我个人不喜欢它们(再次是个人选择),但是在您仍然使用对象时不学习SQL(很多)的一种方法是使用像NHibernate这样的ORM(您还需要学习如何使用它,这样您才能不要让事情变慢)。

If you need to have everything loaded at the same time (most often that is not the case so be sure of this) you need to know what you want to keep and serialize it. 如果您需要同时加载所有内容(通常不是这样,请确保对此有所了解),则需要知道要保留的内容并对其进行序列化。 If you want that data to be readable by another tool or organize in a given way consider a data format like XML or JSON. 如果您希望其他工具可以读取该数据或以给定的方式组织该数据,请考虑使用XML或JSON之类的数据格式。

Also, you can use mmap-file. 另外,您可以使用mmap文件。 File is permanent, and keep data between program run. 文件是永久的,并在程序运行之间保持数据。 So, you just keep your data structs in the mmap-ed area, and no more. 因此,您只需将数据结构保留在mmap-ed区域即可,仅此而已。

MSDN manual here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366556%28v=vs.85%29.aspx 此处的MSDN手册: https : //msdn.microsoft.com/zh-CN/library/windows/desktop/aa366556%28v=vs.85%29.aspx

Since you need to load all the data once at the start of the program, the database case seems doubtful. 由于您需要在程序启动时一次加载所有数据,因此数据库的情况似乎令人怀疑。 The DB necessary when you need to load a bit of data many times. 需要多次加载少量数据时所需的DB。

So first two cases seem more preferred. 因此,前两种情况似乎更可取。 I would advice to hide a specific solution behind an interface, then you'll can change it later. 我建议将特定的解决方案隐藏在界面后面,然后您可以稍后进行更改。

Standard .NET serialization of sensors' array is more simple probably, and it will be easier to expand. 传感器阵列的标准.NET序列化可能更简单,并且更易于扩展。

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

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