使用 ORM 还是普通 SQL? [英] Using an ORM or plain SQL?

查看:38
本文介绍了使用 ORM 还是普通 SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我开发的一些应用程序(然后忘记了),我一直在编写简单的 SQL,主要用于 MySQL.虽然我在 python 中使用过 ORM,比如 SQLAlchemy,但我并没有坚持使用它们很长时间.通常是文档或复杂性(从我的角度来看)阻碍了我.

For some of the apps I've developed (then proceeded to forget about), I've been writing plain SQL, primarily for MySQL. Though I have used ORMs in python like SQLAlchemy, I didn't stick with them for long. Usually it was either the documentation or complexity (from my point of view) holding me back.

我是这样看的:使用 ORM 来实现可移植性,如果只使用一种类型的数据库,则使用纯 SQL.在开发需要数据库支持的应用程序时,我真的在寻求有关何时使用 ORM 或 SQL 的建议.

I see it like this: use an ORM for portability, plain SQL if it's just going to be using one type of database. I'm really looking for advice on when to use an ORM or SQL when developing an app that needs database support.

仔细想想,与使用 ORM 相比,使用轻量级包装器来处理数据库不一致会好得多.

Thinking about it, it would be far better to just use a lightweight wrapper to handle database inconsistencies vs. using an ORM.

推荐答案

ORM 有一些不错的特性.他们可以处理将数据库列复制到对象字段的大部分工作.它们通常处理将语言的日期和时间类型转换为适当的数据库类型.它们通常也通过实例化嵌套对象来非常优雅地处理一对多关系.我发现如果您在设计数据库时考虑到 ORM 的优点和缺点,则可以节省大量将数据输入和输出数据库的工作.(如果你需要映射它们,你会想知道它如何处理多态性和多对多关系.正是这两个域提供了大部分的阻抗失配",这使得一些人称 ORM 为计算机科学的越南".)

ORMs have some nice features. They can handle much of the dog-work of copying database columns to object fields. They usually handle converting the language's date and time types to the appropriate database type. They generally handle one-to-many relationships pretty elegantly as well by instantiating nested objects. I've found if you design your database with the strengths and weaknesses of the ORM in mind, it saves a lot of work in getting data in and out of the database. (You'll want to know how it handles polymorphism and many-to-many relationships if you need to map those. It's these two domains that provide most of the 'impedance mismatch' that makes some call ORM the 'vietnam of computer science'.)

对于事务性的应用程序,即您发出请求,获取一些对象,遍历它们以获取一些数据并将其呈现在网页上,性能负担很小,并且在许多情况下 ORM 可以更快,因为它会缓存它以前见过的对象,否则会多次查询数据库.

For applications that are transactional, i.e. you make a request, get some objects, traverse them to get some data and render it on a Web page, the performance tax is small, and in many cases ORM can be faster because it will cache objects it's seen before, that otherwise would have queried the database multiple times.

对于报告繁重或每个请求处理大量数据库行的应用程序,ORM 负担要重得多,它们所做的缓存变成了一个巨大的、无用的内存占用负担.在这种情况下,简单的 SQL 映射(LinQ 或 iBatis)或在瘦 DAL 中手动编码的 SQL 查询是可行的方法.

For applications that are reporting-heavy, or deal with a large number of database rows per request, the ORM tax is much heavier, and the caching that they do turns into a big, useless memory-hogging burden. In that case, simple SQL mapping (LinQ or iBatis) or hand-coded SQL queries in a thin DAL is the way to go.

我发现对于任何大型应用程序,您都会发现自己同时使用这两种方法.(ORM 用于简单的 CRUD 和 SQL/thin DAL 用于报告).

I've found for any large-scale application you'll find yourself using both approaches. (ORM for straightforward CRUD and SQL/thin DAL for reporting).

这篇关于使用 ORM 还是普通 SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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