将静态数据存储在数组中或数据库中? [英] Store static data in an array, or in a database?

查看:160
本文介绍了将静态数据存储在数组中或数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们总是有一些静态数据,可以作为数组存储在文件中或存储在基于Web的项目中的数据库表中。



在我看来,数组有一些优点:


  1. 更灵活(可以是任何结构,指定一个真正复杂的关系)

  2. 更好的性能(它将被加载到内存中,数据库的I / O操作)

但我的同事认为他更喜欢DB方法,因为它可以保持统一的数据持久化接口,并且更灵活。



那么哪个应该优先?或者我们如何选择?或者我们应该在某些情况下选择一个,而在其他情况下选择另一个?什么是场景?



编辑:



让我澄清一下。正如Benjamin对标题所做的改变一样,我们希望存储在数组(文件)中的数据不会变化太频繁,这意味着代码不会在运行时改变数组的值。如果数据变化非常频繁我会使用DB毫无疑问。



有时很难存储一些非常复杂的关系,例如:

  Task = {
1:{
name:xx,
requirement:{
level 5,
money:100,
}
...
}

$ b b

就像上面的代码示例(一个python dict或者你可以认为它是一个数组),需求字段很难存储在DB(直接存储在DB中的结构像pickled对象吗?不是这样好我想)。所以在这种情况下,我会喜欢数组。



那么你的想法是什么?在这种情况下,我们应该更喜欢数组到DB,对吗?



回答。

解决方案

让实用/ objetive:




  • 你在运行时写数据吗?是:Db,否:文件

  • 您每周更新数据的次数是否超过一次?是:Db,否:文件

  • 发布更新的数据文件很痛苦?是:Db,否:文件,

  • 你经常读这些数据吗?是:文件/缓存,否:Db

  • 更新该数据文件需要额外的工具吗?是:db,否:文件



确定我已经忘记了其他点,但我想基础知识。 >

We always have some static data which can be stored in a file as an array or stored in a database table in our web based project. So which one should be preferred?

In my opinion, arrays have some advantages:

  1. More flexible (it can be any structure, which specifies a really complex relation)
  2. Better performance (it will be loaded in memory, which will have better read/write performance compared with a database's I/O operations)

But my colleague argued that he preferred DB approach, since it can keep a uniform data persistence interface, and be more flexible.

So which should be preferred? Or how can we choose? Or we should prefer one in some scenario and another in other scenarios? what are the scenarios?

EDIT:

Let me clarify something. Truly just as Benjamin made the change to the title, the data we want to store in an array(file) won't change so frequently, which means the code won't change the value of the array in the runtime. If the data change very frequently I will use DB undoubtedly. That's why I made such a post.

And sometimes it's hard to store some really complex relations like:

Task = {
  "1" : {
    "name" : "xx",
    "requirement" : {
          "level" : 5,
          "money" : 100,
     }
   ...
 }

Just like the above code sample(a python dict or you can think it as an array), the requirement field is hard to store in DB(store a structure like pickled object directly in DB? not so good I think). So in such condition, I will prefer arrays.

So what's your idea? In such scenario, we should prefer arrays to DB, right?

Regards.

解决方案

Lets be pragmatic/objetive:

  • Do you write to your data on runtime? Yes: Db, No: File
  • Do you update your data more than once per week? Yes: Db, No: File
  • It's a pain to release an updated data file? Yes: Db, No: File,
  • Do you read that data often? Yes: File/Cache, No: Db
  • It is a pain to update that data file and you need extra tools? Yes: db, No: File

For sure I've forgotten other points, but I guess the basics are there.

这篇关于将静态数据存储在数组中或数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆