简体   繁体   English

将NoSQL数据库用于关系目的

[英]Using NoSQL database for relational purpose

Non-relational databases are attracting more attention day by day. 非关系数据库正日益吸引着更多的关注。 The main limitation is that today's complicated data are indeed connected. 主要限制是,当今复杂的数据确实已连接在一起。 Isn't it convenient to connect databases as we connect tables in RDBMS? 在RDBMS中连接表时,连接数据库是否方便? Of course, I just mean simple cases. 当然,我只是说简单的情况。 Imagine three tables of Articles, Tags, Relationships. 想象一下文章,标签,关系的三个表格。 In a RDBMS like Mysql, we can run three queries to 在像Mysql这样的RDBMS中,我们可以运行三个查询来

1. Find ID of a given tag
2. Find Articles connected with the captured Tag ID
3. Fetch the contents of Articles tagged with the term

Instead of three queries, we perform a single query by JOIN. 代替三个查询,我们通过JOIN执行单个查询。 I think three queries in a key/value database like BerkeleyDB is faster than a JOIN query in Mysql. 我认为像BerkeleyDB这样的键/值数据库中的三个查询比Mysql中的JOIN查询要快。

Is this idea practical? 这个想法可行吗? Or other issues are involved to ignore this approach? 还是涉及其他问题以忽略此方法?

NoSQL databases can support relational data models just fine. NoSQL数据库可以很好地支持关系数据模型。 You're just left to implement the relational mapping yourself in your application, and that effort is typically not insignificant. 您只需要在应用程序中自己实现关系映射即可,这种工作通常并不重要。

In some applications this extra effort will be worthwhile. 在某些应用中,这种额外的努力将是值得的。 Perhaps you only have a small number of tables and the joins you need are very simple. 也许您只有少量的表,并且所需的联接非常简单。 Or perhaps you've done some performance evaluation between a traditional relational DBMS and a NoSQL alternative and found that the NoSQL option is more appropriate for your needs for any number of reasons (performance, scalability, flexibility, whatever). 或者,也许您已经在传统的关系型DBMS和NoSQL替代方案之间进行了一些性能评估,发现NoSQL选项由于多种原因(性能,可伸缩性,灵活性等)而更适合您的需求。

You should keep one thing in mind, however. 但是,您应该记住一件事。 A typical SQL DBMS is basically a NoSQL DB with an optimized, well-built relational engine in front of it. 典型的SQL DBMS基本上是NoSQL DB,其前面是经过优化的,精心构建的关系引擎。 Some databases even let you bypass the relational layer and treat their system like a pure NoSQL DB . 某些数据库甚至允许您绕过关系层, 将它们的系统视为纯NoSQL DB

Therefore, the moment you start to build your own relational mappings and joins on top of a NoSQL DB you should ask yourself, "Didn't someone build this for me already?" 因此,当您开始建立自己的关系映射并加入NoSQL DB的那一刻时,您应该问自己:“有人已经为我建立了这个关系吗?” The answer may well be "yes", and the solution might be to go with a traditional SQL DBMS. 答案很可能是“是”,解决方案可能是使用传统的SQL DBMS。

To answer the "3 query" part of your question specifically, the answer is "maybe". 要专门回答问题的“ 3个查询”部分,答案是“也许”。 You certainly might be able to make such a query run faster in a NoSQL DB than in an RDBMS, but you need to keep in mind that there are more things to consider here than just the raw speed of your query: 您肯定可以使NoSQL DB中的查询运行速度比RDBMS中的查询快,但是您需要记住,这里要考虑的不仅仅是原始查询速度,还有更多的事情要考虑:

  1. The technical debt you will incur as you build join-like functionality that you wouldn't have had to build otherwise 在构建类似连接的功能时将产生的技术债务,否则就无需构建
  2. The time it will take you to build, test and optimize your query code which will likely be more significant than writing a simple SQL query 建立,测试和优化查询代码所花费的时间可能比编写简单的SQL查询更重要
  3. Any difference in transactional guarantees or other typical product features (replication, management tools, etc) which you may lose or gain depending on the NoSQL option you choose 根据选择的NoSQL选项,您可能失去或获得的交易担保或其他典型产品功能(复制,管理工具等)的任何差异
  4. The ability to hire DBMs who know how to run your database from an operational perspective 可以聘请知道如何从运营角度运行数据库的DBM

You might review that list and say to yourself, "No big deal, I'm running a simple app with only a few thousand DB entries and I'll maintain it myself" . 您可能会查看该列表并对自己说: “没什么大不了的,我正在运行一个只有几千个数据库条目的简单应用程序,我会自己维护它” If so, knock yourself out - Berkeley (and other NoSQL options) would work fine. 如果是这样,请把自己淘汰掉-Berkeley(和其他NoSQL选项)会很好用。 I've used Berkeley many times for those kinds of applications. 在这类应用程序中,我已经多次使用Berkeley。 But you may have a different answer if you are building the back-end for a significantly-sized SaaS product which might soon have millions of users and very complex queries. 但是,如果您要为大型SaaS产品构建后端,则可能会有不同的答案,该产品可能很快就会有数百万的用户和非常复杂的查询。

We can't give a one-size-fits-all answer, unfortunately. 不幸的是,我们无法给出一刀切的答案。 You'll have to make the judgement call yourself based on the needs of you application and organization. 您必须根据应用程序和组织的需求做出判断。

Sure, a single record join is pretty speedy in either solution, but that's not the big advantage of joins. 当然,在任何一种解决方案中,单个记录联接都非常快,但这并不是联接的最大优势。 Joins are useful when you're joining many, many rows with many, many other rows. 当您将许多行与许多其他行联接在一起时,联接很有用。 Imagine if, in your example, you wanted to do that for 100 different tags. 想象一下,如果在您的示例中,您想针对100个不同的标签执行此操作。 Without joins, you're talking 300 queries to SQL's one. 没有联接,您正在说300个查询与SQL的查询。

Another solution on noSql systems is playOrm. noSql系统上的另一个解决方案是playOrm。 It does Joins BUT only in partitions so the table can be infinite size, but the partitions have to be on par with the size of RDBMS tables. 它仅在分区中执行Join BUT,因此表可以是无限大小,但是分区必须与RDBMS表的大小相等。 It does all the fancy hibernate stuff as well for you with all the related annotations though it has some differences and will be adding Embedded for use when you denormalize. 尽管它有一些差异,但它也会为您提供所有相关的注释,从而为您完成所有花式的休眠操作,并将在您进行非规范化时添加Embedded以供使用。 It makes things much easier. 它使事情变得容易得多。 Typically dealing with nosql is kind of a pain in all the translation logic you have to do and all the manual indexing and updates and removes from the index....playOrm does all this for you instead. 通常,在必须执行的所有翻译逻辑以及所有手动索引编制和更新以及从索引中删除的过程中,使用nosql会有些痛苦。...playOrm会替您完成所有这一切。

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

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