我们应该在数据库中做多少工作?

发布于 2024-11-23 19:14:08 字数 252 浏览 1 评论 0原文

我们应该在数据库中做多少工作? 好吧,我真的很困惑到底应该在数据库中完成多少“工作”,以及必须在应用程序级别完成多少工作?

我的意思是,我不是在谈论明显的东西,比如我们应该在应用程序级别而不是数据库级别将字符串转换为 SHA2 哈希值。

而是更模糊的东西,包括但不限于“我们应该检索数据吗?” 4 列并在应用程序级别执行大写/串联,或者我们应该在数据库级别执行这些操作并将计算结果发送到应用程序级别?

如果您可以列出更多其他示例,那就太好了。

how much work should we do in the database?
Ok I'm really confused as to exactly how much "work" should be done IN the database, and how much work had to be done instead at the application level?

I mean I'm not talking about obvious stuff like we should convert strings into SHA2 hashes at the application level instead of the database level..

But rather stuff that are more blur, including, but not limited to "should we retrieve the data for 4 column and do a uppercase/concatenation at the application level, or should we do those stuff at the database level and send the calculated result to the application level?

And if you could list any more other examples it would be great.

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

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

发布评论

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

评论(4

停滞 2024-11-30 19:14:08

这实际上取决于您的需要。
我喜欢在数据库中完成我的业务逻辑,其他人强烈反对。

您可以在 SQL 中使用触发器和存储过程/函数。

MySQL 链接:
http://dev.mysql.com/doc/refman/5.5/en /triggers.html
http://www.mysqltutorial.org/introduction-to-sql-stored -procedures.aspx
http://dev.mysql.com/doc/refman/5.5 /en/stored-routines.html

我在触发器和存储过程中执行业务逻辑的原因

请注意,我不是在谈论将数据库结构转向业务逻辑,而是谈论将业务逻辑放入触发器和存储过程。

  1. 它集中了你的逻辑,数据库是一个中心位置,一切都必须经过它。如果您的应用程序中有多个插入/更新/删除点(或者您有多个应用程序),您将需要多次执行检查,如果您在数据库中执行此操作,则只需在一处执行检查。
  2. 它简化了应用程序,例如,您只需添加一个成员,数据库将确定该成员是否已知并采取适当的操作。
  3. 它向应用程序隐藏了数据库的内部结构,如果您在应用程序中执行所有逻辑,您将需要对应用程序中的数据库有复杂的了解。如果您使用数据库代码(触发器/过程)来隐藏它,则无需了解应用程序中的每个数据库详细信息。
  4. 它使重建数据库变得更容易如果您的数据库中有逻辑,您只需更改表布局,用黑洞表替换旧表,在其上放置触发器并让触发器执行对新表的更新,您的应用程序甚至不需要知道数据库已更改,这允许旧应用程序保持不变,而新应用程序可以使用改进的数据库布局。
  5. 有些事情在 SQL 中更容易
  6. 有些事情在 SQL 中运行得更快
  7. 我不喜欢在我的应用程序中使用(大量和/或复杂的)SQL 代码< /strong>,我喜欢将 SQL 代码放入存储过程/函数中,并尝试仅在应用程序代码中放入简单的查询,这样我就可以编写代码来解释我在应用程序中的含义,并让数据库层执行以下操作举重。

有些人强烈反对这一点,但这种方法对我来说效果很好,并且大大简化了我的应用程序的调试和维护。

It really depends on what you need.
I like to do my business logic in the database, other people are religously against that.

You can use triggers and stored procedures/functions in SQL.

Links for MySQL:
http://dev.mysql.com/doc/refman/5.5/en/triggers.html
http://www.mysqltutorial.org/introduction-to-sql-stored-procedures.aspx
http://dev.mysql.com/doc/refman/5.5/en/stored-routines.html

My reasons for doing business logic in triggers and stored proces

Note that I'm not talking about bending the database structure towards the business logic, I'm talking about putting the business logic in triggers and stored procedures.

  1. It centralizes your logic, the database is a central place, everything has to go through it. If you have multiple insert/update/delete points in your app (or you have multiple apps) you'll need to do the checks multiple times, if you do it in the database you only have to do the checks in one place.
  2. It simplifies the application e.g., you can just add a member, the database will figure out if the member is already known and take the appopriate action.
  3. It hides the internals of your database from the application, if you do all your logic in the application you will need intricate knowledge of your database in the application. If you use database code (triggers/procs) to hide that, you don't need to know every database detail in your app.
  4. It makes it easier to restucture your database If you have the logic in your database, you can just change a tablelayout, replace the old table with a blackhole table, put a trigger on that and let the trigger do the updates to the new table, your app does not even need to know the database has changed, this allows legacy apps to keep working unchanged, whilst new apps can use the improved database layout.
  5. Some things are easier in SQL
  6. Some things work faster in SQL
  7. I don't like to use (lots of and/or complicated) SQL code in my application, I like to put SQL code in a stored procedure/function and try to only put simple queries in my application code, that way I can just write code that explains what I mean in my application and let the database layer do the heavy lifting.

Some people disagree strongly with this, but this approach works well for me and has simplified debugging and maintenance of my applications a lot.

静谧 2024-11-30 19:14:08

一般来说,最好只期望数据库中的“数据”。由应用程序来应用业务/域逻辑并理解检索到的数据。强烈建议在应用层执行以下操作:

1)格式化日期
2)应用数学函数,例如插值/外推等
3) 动态排序(基于列)

然而,有时情况下不需要在数据库级别做一些事情。

Generally, its a good practice to expect only "Data" from the Database. Its upto Application(s), to apply Business/Domain Logic and make sense of the data retrieved. Its highly recommended to do the following things in the Application Layer:

1) Formatting Date
2) Applying Math functions, such as interpolation/extrapolation, etc
3) Dynamic sorting (based on columns)

However, situations sometime warrant few things to be done at the database level.

违心° 2024-11-30 19:14:08

在我看来,应用程序应该使用数据,数据库应该提供它们,并且应该明确关注点分离。因此,数据库根据请求的条件对记录进行排序、排序和过滤,但由应用程序对这些记录应用一些业务逻辑并将它们“转换”为对用户有意义的内容。

例如,在我之前的公司中,我们致力于工作时间计算的大型应用程序。此类应用程序的一个明显功能是跟踪员工的假期天数 - 员工每年有多少天、他使用了多少天、还剩下多少天等。基本上,我们可以编写一些触发器和过程来自动更新这些列。因此,当员工的假期得到批准时,他申请的天数将从他的“假期池”中取出并添加到“已使用的假期”中。非常简单的事情,但我们决定在应用程序级别上明确它,天哪,很快我们就很高兴我们这样做了。申请必须符合劳动法,但很快就发现,并不是所有员工的假期都是平等计算的,有时假期可能根本不是假期,但这不是重点。如果我们将这个“简单”的操作放入数据库中,我们就必须对数据库进行版本控制,并对假期相关逻辑进行每一个微小的更改,这将导致我们在客户支持领域直接陷入困境,因为事实上可以只更新应用程序无需更新数据库(当然,除了改变数据库结构的明显“突破”时刻)。

In my opinion application should use data and database should provide them and that should be clear separation of concerns. So database gives records sorted, ordered and filtered according to requested conditions but it is up to application to apply some business logic to that records and "convert" them into something meaningful to the user.

For example, in my previous company we worked on big application for work time calculations. One of obvious functionalities in this kind of application is tracking vacation days of employees - how many days employee has per year, how many he used, how many left, etc. Basically we could write some triggers and procedures that would update those columns automatically. So when employee had his vacation days approved amount of days he applied for is taken from his "vacation pool" and added to "vacation days used". Pretty easy stuff but we decided to make it explicit on application level and boy, very soon we were happy we did it that way. Application had to be labor law compliant and it quickly turned out that not for all employees vacation days are calculated equally and sometimes vacation day can be not so vacation day at all but that is beside the point. Had we put this "easy" operation in database we had to version our database with every little change to a vacation days related logic and that would lead us straight to hell in customer support field due to a fact that it was possible to update only application without a need to update database (except clear "breakthrough" moments where database structure was changed of course).

盗心人 2024-11-30 19:14:08

根据我的经验,我发现许多应用程序都是从一组简单的表开始,然后是一些存储过程来提供基本功能。这非常有效;它通常会产生高性能并且易于理解,并且还减轻了对复杂中间层的任何需求。

然而,应用程序不断增长。具有数千个存储过程的大型数据驱动应用程序并不罕见。将触发器加入其中,您就拥有了一个应用程序,对于原始开发人员(如果他们仍在开发)之外的任何人来说,该应用程序都很难维护。

我将介绍一下将大部分逻辑放在数据库中的应用程序 - 当您拥有一些优秀的数据库开发人员和/或您拥有无法更改的遗留模式时,它们可以很好地工作。我这样说的原因是,当您让 ORM 控制架构时,ORM 可以减轻应用程序开发这一部分的痛苦(如果没有,您通常需要做大量的调整才能使其正常工作)。

如果我正在设计一个新的应用程序,那么我通常会选择由我的应用程序域决定的模式(其设计将在代码中)。我通常会让 ORM 处理对象和数据库之间的映射。当涉及到数据访问时,我会将存储过程视为规则的例外(存储过程中的报告比试图哄骗 ORM 有效地生成复杂的输出要容易得多)。

但要记住的最重要的事情是,在设计方面不存在“最佳实践”。作为开发人员,您可以根据自己的设计情况权衡每个选项的利弊。

In my experience I've found that many applications start with a straight-forward set of tables and then and handful of stored procedures to provide basic functionality. This works very well; it usually yields high performance and is simple to understand, it also mitigates any need for a complex middle-tier.

However, applications grow. It's not unusual to see large data-driven applications with thousands of stored procedures. Throw triggers into the mix and you have an application which, for anybody other than the original developers (if they're still working on it), is very difficult to maintain.

I will put a word in for applications which place most logic in the database - they can work well when you have some good database developers and/or you have a legacy schema which cannot be changed. The reason I say this is that ORMs take much of the pain out of this part of application development when you let them control the schema (if not, you often need to do a lot of fiddling to get it working).

If I was designing a new application then I would usually opt for a schema which is dictated by my application domain (the design of which will be in code). I would normally let an ORM handle the mapping between the objects and the database. I would treat stored procedures as exceptions to the rule when it came to data access (reporting can be much easier in sprocs than trying to coax an ORM into producing a complex output efficiently).

The most important thing to remember though, is that there are no "best practices" when it comes to design. It is up to you the developer to weigh up the pros and cons of each option in the context of your design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文