简体   繁体   English

将模式存储为数组或哈希字段有什么好处?

[英]What are the advantages of storing your schema as arrays or hash fields?

This applies to rethink DB. 这适用于重新考虑数据库。 Is it better to have a schema with embedded arrays or hash fields as arrays? 具有嵌入式数组或哈希字段作为数组的架构更好吗? Keeping my intentions simple.. I am trying to keep track of daily statistics. 保持意图简单..我试图保持日常统计。 But I am in between deciding what schema structure is better. 但我介于两者之间,以决定哪种架构结构更好。 Let me elaborate.. 让我详细说明

Pure array schema: 纯数组架构:

schema = [
  {
    title: 'foobar',
    dates: [
      {
        date: 20130926,
        views_count: 10,
        click_count: 10
      },
      {
        date: 20130927,
        views_count: 20,
        click_count: 20
      },
      {
        date: 20130928,
        views_count: 30,
        click_count: 30
      }
    ]
  }
]

Hash field array schema: 哈希字段数组架构:

schema = [
  {
    title: 'foobar',
    dates: [
      '20130926' => {
        views_count: 10,
        click_count: 10
      },
      '20130927' => {
        views_count: 20,
        click_count: 20
      },
      '20130928' => {
        views_count: 30,
        click_count: 30
      }
    ]
  }
]

One that I can think of is.. It's easier to prevent duplicity of dates with the latter. 我能想到的是。.防止日期重复与后者比较容易。 Any other advantages? 还有其他优势吗? Or, is there a common convention that developers prefer? 或者,是否有开发人员喜欢的通用约定?

IMHO, your application trumps any DBMS. 恕我直言,您的应用程序胜过任何DBMS。 Instead of focusing on your DB storage choices, focus on your application needs, programming and performance. 与其关注您的数据库存储选择,不如关注您的应用程序需求,编程和性能。 Persist your program data, then iteratively benchmark and optimize for the dominant cases with schema changes only when necessary. 持久存储程序数据,然后迭代基准测试并仅在必要时通过架构更改针对主要情况进行优化。 Your application will answer your storage questions. 您的应用程序将回答您的存储问题。 For example, in general: 例如,通常:

(1) if you need predominantly ordered access to dates, then use array (2) if you need fast random access to many dates, then use hash (1)如果需要对日期进行优先排序,则使用数组(2)如果需要对许多日期进行快速随机访问,则使用哈希

Both the programming language and DBMS semantics matter. 编程语言和DBMS语义都很重要。 Even if the DB has ordered hashes, you language could loose this, eg, Hashes in Ruby 1.9 and newer are ordered, but were unordered previously. 即使数据库已排序哈希,您也可以使用该语言来松散该哈希,例如,Ruby 1.9和更高版本中的哈希已排序,但以前未排序。

Of course your schema choices are VERY important. 当然,您的架构选择非常重要。 But IMHO, a primary strength of document (non-columnar object data NoSQL) DBMSs is the ability to match natural data structures in programming languages. 但是恕我直言,文档(非列对象数据NoSQL)DBMS的主要优势是能够以编程语言匹配自然数据结构。 So I gently encourage you to go back to your application/program as the focus for both questions and answers. 因此,我鼓励您回到您的应用程序/程序作为问题和答案的重点。

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

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