社交网站的 ORM 有意义吗?

发布于 2024-08-19 00:22:02 字数 421 浏览 9 评论 0 原文

我之所以问这个问题是因为我需要知道在社交网站上不使用 ORM 是否有任何意义。

我的理由是 ORM 不适合社交网站:

  • 社交网站不是产品,因此不需要支持多个数据库。您知道要使用什么数据库,并且很可能不会时不时地更改它。
  • 社交网站需要用户之间存在多对多关系,最终有时您需要编写简单的 SQL 来获取这些关系。 ORM的价值因此再次降低。
  • 与上一点相关,ORM 有时会在后端执行多个查询来获取其记录,这有时可能效率低下,并可能导致数据库瓶颈。最后你必须写下简单的 SQL 查询。如果我们知道无论如何我们都会编写纯 SQL,那么使用 ORM 有何意义?

这是我基于有限经验的有限理解。您在建立社交网站方面有哪些经验?我的积分有效吗?使用裸 SQL 而不担心使用 ORM 是不是很蹩脚? ORM 在构建社交网站时可以帮助哪些点?

The reason why I ask this is because I need to know whether not using ORM for a social networking site makes any sense at all.

My argument why ORM does not fit into social networking sites are:

  • Social networking sites are not a product, thus you don't need to support multiple database. You know what database to use, and you most likely won't change it every now and then.
  • Social networking sites requires many-to-many relationship between users, and in the end sometimes you will need to write plain SQL to get those relations. The value of ORM is thus decreased again.
  • Related to the previous point, ORM sometimes do multiple queries in the backend to fetch its record, which sometimes may be inefficient and may cause bottleneck in the database. In the end you have to write down plain SQL query. If we know we are going to write plain SQL anyway, what is the point using ORM?

This is my limited understanding based on my limited experience. What are you're experience with building a social networking sites? Are my points valid? Is it lame to use bare SQL without worrying about using ORM? What are the points where ORM may help in building a social networking sites?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

∝单色的世界 2024-08-26 00:22:02

使用 ORM 的价值在于通过自动执行将查询结果分配给对象字段的繁琐工作以及跟踪对象字段的更改以便将它们保存到数据库来帮助加快开发。因此出现了术语“对象关系映射”。

在数据库可移植性方面,ORM 对您来说没有什么价值,因为您只使用部署的一个数据库。

ORM 的运行时性能并不比您自己编写纯 SQL 更好,而且通常更差。正如您所提到的,查询生成的通用方法经常会犯幼稚的错误并导致冗余查询。同样,好处在于开发时间,而不是运行时效率。

使用 ORM 与不使用 ORM 似乎对可扩展性没有太大影响。其他更划算的可扩展性技术包括:

  • 管理 RDBMS 中的索引。改进尽可能多的算法,从 O(n) 到 O(log2n)。
  • 智能缓存架构。
  • 通过数据库分区/分片进行水平扩展。
  • 数据库负载平衡和复制。尽可能从从属数据库读取数据,然后写入单个主数据库。索引从站和主站的方式不同。
  • 使用 Sphinx 搜索等补充技术来补充 RDBMS。
  • 通过使用硬件来解决问题的垂直扩展。 Jeff Atwood 在 StackOverflow 播客上对此发表了评论。

有些人主张使用云计算或分布式非关系数据库将数据管理转移到分布式架构。在您获得大量用户之前,这可能是不必要的。一旦规模增长到一定程度,所有规则都会发生变化,并且您可能无法使用 RDBMS。但除非您是雅虎、Facebook 或 LinkedIn 的数据架构师,否则不要担心——云计算被过度炒作了。

人们普遍认为数据库始终是 Web 应用程序的瓶颈,但也有一种情况认为提高前端效率至少同样重要。比照。 Steve Souders 的书籍。


Julia Lerman 在编程实体框架 (2009),第 503 页中表明,直接使用 DataReader 与使用 Microsoft 的 LINQ to Entities 相比,查询执行成本增加了 220%。

另请参阅 Jeff Atwood 的帖子 所有抽象都是失败的抽象,其中他表明,使用 LINQ 的成本至少是使用普通 SQL 的两倍,即使是以简单的方式也是如此。

The value of using an ORM is to help speed up development, by automating the tedious work of assigning query results to object fields, and tracking changes to object fields so you can save them to the database. Hence the term Object-Relational Mapping.

An ORM has little value for you regarding database portability, since you only use the one database you deploy on.

The runtime performance aspect of an ORM is no better than, and typically much worse than writing plain SQL yourself. The generic methods of query generation often make naive mistakes and result in redundant queries, as you have mentioned. Again, the benefit is in development time, not runtime efficiency.

Using an ORM versus not using an ORM doesn't seem to make a huge difference for scalability. Other techniques with more bang-for-the-buck for scalability include:

  • Managing indexes in the RDBMS. Improve as many algorithms as possible from O(n) to O(log2n).
  • Intelligent caching architecture.
  • Horizontal scaling by database partitioning/sharding.
  • Database load-balancing and replication. Read from slave databases where possible, and write to a single master database. Index slaves and masters differently.
  • Supplement the RDBMS with complementary technology, such as Sphinx Search.
  • Vertical scaling by throwing hardware at the problem. Jeff Atwood has commented about this on the StackOverflow podcast.

Some people advocate moving your data management to a distributed architecture using cloud computing or distributed non-relational databases. This is probably not necessary until you get a very large number of users. Once you grow to a certain level of magnitude, all the rules change and you probably can't use an RDBMS anyway. But unless you are the data architect at Yahoo or Facebook or LinkedIn, don't worry about it -- cloud computing is over-hyped.

There's a common wisdom that the database is always the bottleneck in web apps, but there's also a case that improving efficiency on the front-end is at least as important. Cf. books by Steve Souders.


Julia Lerman in Programming Entity Framework (2009), p.503 shows that there's a 220% increase in query execution cost between using a DataReader directly and using Microsoft’s LINQ to Entities.

Also see Jeff Atwood's post on All Abstractions are Failed Abstractions, where he shows that using LINQ is at least double the cost of using plain SQL even in a naive way.

兰花执着 2024-08-26 00:22:02

以下是我对您的观点的回应:

  • ORM不需要多个数据库才能有效,事实上大多数情况下ORM的使用并不是因为能够适应不同的数据库。
  • 大多数现代 ORM 框架都足够灵活,可以获取映射类的“轻量级”变体,这实际上取决于您如何实现它们。
  • 如果确实需要,您可以在 ORM 框架内编写本机 SQL 查询。请注意,缓存和性能相关的算法通常是这些框架的一部分。

Here's my response to your points:

  • ORM does not need multiple database to be effective, in fact most cases of ORM usage are not due to the ability to adapt to different databases.
  • Most modern ORM frameworks are flexible enough to fetch 'lightweight' variants of mapped classes, it really depends on how you implement them.
  • If really required to, you can write native SQL queries within the ORM frameworks. Do note that caching and performance related algorithms are often part of the these frameworks.
℡Ms空城旧梦 2024-08-26 00:22:02

在我看来,ORM 可以帮助您编写更干净、更清晰的代码。如果你不小心使用它,可能会导致过多的查询,但这无论如何都不是规则。如果我是你,我会开始使用 ORM 和框架的最佳实践,并且只有在你发现自己需要 ORM 不提供的功能时才会使用 SQL。

IMO, an ORM helps you write cleaner, clearer code. If you use it sloppily you can cause excessive queries, but that isn't a rule by any means. If I were you I would start using the ORM and best practices of a framework, and only drop to SQL if you find yourself needing functionality that the ORM does not provide.

七秒鱼° 2024-08-26 00:22:02

您的网站足够大而导致扩展成为问题的可能性非常小,那么为什么要通过原始 SQL 而不是 ORM 来过早地进行优化呢?假设数据库和应用程序设计都不错,那么通过在数据库上投入更好的硬件,您可以取得相当大的成果。虽然您可能需要为创建好友图表之类的事情编写原始 SQL,但当有人更改电子邮件、发送私人消息、上传照片等时更新数据库等所有小事情又如何呢?使用 ORM 可以简化您必须执行的所有简单数据库任务,同时仍然允许您在绝对必要的地方手动编写代码。

The odds of your site being big enough that scaling becomes an issue are quite small so why prematurely optimize by doing everything in raw SQL instead of an ORM? You can get fairly far by throwing better hardware at a database assuming the database and application design are decent. While you may need to write raw SQL for things like creating friend graphs what about all the little things like updating the database when someone changes there email, sends a private message, uploads a photo, etc? Using an ORM can simplify all the simple database tasks you will have to do while still allowing you to hand code where absolutely necessary.

且行且努力 2024-08-26 00:22:02

另请注意,在 Web 应用程序中,许多人正在放弃 SQL 数据库。 ORM 可能会帮助您迁移到非关系数据库(正是因为您的应用程序代码中没有 SQL)。看看Google App Engine 中JDO 和JPA 的使用。

Also note that in web applications, many people are moving away from SQL databases. An ORM might help you to migrate to a non-relational database (precisely because you do not have SQL in your application code). Look at the use of JDO and JPA in Google's App Engine.

怪异←思 2024-08-26 00:22:02

恕我直言。 ORM 是需要的。

  1. 它允许您以OOP方式访问数据库,无论是否多个数据库。

  2. 代码更简洁,您可以在表类文件中定义与特定表相关的所有方法,如果您需要原始sql连接查询,没问题,在那里定义。它遵循 DRY 和 KISS。这比你一次又一次地编写类似的原始sql查询要好得多。

IMHO. ORM is need.

  1. It allow you to access database in OOP way, no matter multiple database or not.

    1. Cleaner code, you can define all method related to a particular table in the table class file, if you need raw sql join query, no problem, define there. it follows DRY and KISS. It is much better than you write similar raw sql query again and again.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文