简体   繁体   English

PHP或MySQL中的业务逻辑?

[英]Business Logic in PHP or MySQL?

On a site with a reasonable amount of traffic , would it matter if the application/business logic is written as stored procedures ,triggers and views , instead of inside the PHP code itself? 在具有合理流量的站点上,将应用程序/业务逻辑写为存储过程,触发器和视图而不是PHP代码本身内部是否重要?

What would be the best way to go keeping scalability in mind. 什么是保持可扩展性的最佳方式。

I can't provide you statistics, but unless you plan to change PHP for another language in the future, i can say keeping the business logic in PHP is more "scalability friendly". 我无法为您提供统计信息,但除非您计划在将来将PHP更改为其他语言,我可以说保持PHP中的业务逻辑更具“可扩展性友好性”。

Its always easier and cheaper to solve web server load problems than having them in the database. 解决Web服务器负载问题比在数据库中使用它们总是更容易和更便宜。 Your database will always need to be lighting quick and just throwing mirrors at it won't solve the problem. 您的数据库将始终需要快速点亮,只是向它投掷镜像将无法解决问题。 The more database slaves you have, the more writes you have to do. 您拥有的数据库从站越多,您需要执行的写入越多。

In my experience, you should put business logic in PHP code rather than move it onto the database. 根据我的经验,您应该将业务逻辑放在PHP代码中,而不是将其移动到数据库中。 Assuming your database is on a separate server, you don't want your database to be busy calculating formulas when requests come in. 假设您的数据库位于单独的服务器上,您不希望数据库在请求进入时忙于计算公式。

Keep your database lightning fast to handle selects, inserts and updates. 快速保持数据库处理选择,插入和更新。

A well done PHP application should be enought, but keep in mind that it also requires you to do the less calls to the database you can. 应该使用做得很好的PHP应用程序,但请记住,它还要求您尽可能少地调用数据库。 Store values you'll need later in PHP, shorten queries, cache, etc. 稍后在PHP中存储您需要的值,缩短查询,缓存等。

MySQL optimization is always a must, as it will also decrease the amount of databse calls by PHP, and thus getting a better performance. MySQL优化始终是必须的,因为它还会减少PHP的数据库调用量,从而获得更好的性能。 Therefore, there's no way you can't think of stored procedures, etc, if your aim is to increase performance. 因此,如果您的目标是提高性能,则无法想到存储过程等。 But MySQL by itself would't be enought if your PHP code isn't well done (lots of unecessary database calls), that's why I think PHP must be well coded, keeping in mind the hole process while developing it, so that unecessary stuff doesn't get in the way. 但是如果你的PHP代码做得不好(很多不必要的数据库调用),那么MySQL本身就不会有用,这就是为什么我认为PHP必须编码良好,在开发过程中记住漏洞过程,以便不必要的东西不会妨碍。 Cache for instance, in "duet" with proper MySQL, is a great boost on performance. 例如,在具有适当MySQL的“duet”中,缓存可以极大地提升性能。

I think you will have far better scalibility keeping database code in the database where it can be performance tuned as the number of records gets larger. 我认为你可以将数据库代码保存在数据库中,并且随着记录数量的增加,可以对性能进行调整。 You will also have better data integrity which is critical to the data even being useful. 您还将拥有更好的数据完整性,这对数据甚至有用至关重要。 You don't see a lot of terrabyte sized relational dbs with all their code in the application. 你没有在应用程序中看到很多具有terrabyte大小的关系dbs及其所有代码。

Read some books on database performance tuning and then decide if you want to risk your company's data on application code. 阅读一些有关数据库性能调优的书籍,然后决定是否要冒公司的应用程序代码数据。

There are several things to consider when trying to decide whether to place the business logic in the database or in the application code. 在尝试决定是将业务逻辑放在数据库中还是应用程序代码中时,需要考虑几件事情。

Will the same database be accessed from different websites / web applications? 是否可以从不同的网站/ Web应用程序访问同一个数据库? Will the sites / applications be written in the same language or in a different language? 网站/应用程序是用同一种语言还是用不同的语言编写的?

If the database will be used from a single site, and the site is written in a single language then this becomes a non-issue. 如果数据库将在单个站点中使用,并且该站点使用单一语言编写,那么这将成为一个非问题。 Otherwise, you'll need to consider the added complexity of stored procedures, triggers, etc vs trying to maintain database access logic etc in multiple code bases. 否则,您需要考虑存储过程,触发器等增加的复杂性,以及尝试在多个代码库中维护数据库访问逻辑等。

What are relational databases in general good for and what is MySQL good for specifically? 什么是关系数据库一般有用,什么是MySQL特别适合? What is PHP best at? 什么是PHP最好的?

This consideration is fairly straight-forward. 这种考虑是相当直接的。 Relational databases across the board and specifically in any variant of SQL are going to do a great job at inserting, updating, and deleting data. 全面的关系数据库,特别是SQL的任何变体,都可以很好地插入,更新和删除数据。 Generally they also handle ATOMIC transactions well. 通常他们也很好地处理ATOMIC交易。 However, most variants of SQL (including MySQL) are not good at complex calculations, on-the-fly date handling, file system access etc. 但是,SQL的大多数变种(包括MySQL)都不擅长复杂的计算,即时日期处理,文件系统访问等。

PHP on the other hand is very fast at handling calculations, dates, file system accesses. 另一方面,PHP在处理计算,日期,文件系统访问方面非常快。 By taking a little time you can even design your PHP code to work in such a way that records are only retrieved once and then stored when necessary. 通过花一点时间,你甚至可以设计你的PHP代码,使记录只被检索一次,然后在必要时存储。

What are you most familiar / comfortable with using? 您最熟悉/最熟悉的是什么?

Obviously it tends to make more sense to use the tool with which you are most familiar. 显然,使用您最熟悉的工具往往更有意义。

As a last point consider that just because a drill can be used to cut sheet rock or because a hammer can be used to drive a screw doesn't mean that they should be used for these things. 最后一点考虑,仅仅因为钻头可以用来切割板岩,或者因为锤子可以用来驱动螺钉并不意味着它们应该用于这些东西。 Sometimes I think that programmers do more potential damage by trying to make more powerful tools that do everything rather than making simpler tools that do one thing really, really well. 有时我认为程序员通过尝试制作更强大的工具来做更多的事情,而不是制作更简单的工具来做一件事,真的很好。

My POV, even not having much experience in developing large applications is to write business logic in the DB for some reasons: 我的POV,即使在开发大型应用程序方面没有太多经验,也是出于某些原因在DB中编写业务逻辑:

1 - Maintainability, I think that languages deprecate functions and changes many other things in a short time period, so if PHP changes version, you'll need to adapt your code to the new version 1 - 可维护性,我认为语言会在短时间内弃用函数并更改许多其他内容,因此如果PHP更改版本,则需要调整代码以适应新版本

2 - DBs tends to be more language stable, so when a new version of a RDBMS comes out, it usually doesn't change many things in the way you write your queries or SPs, or it even doesn't change. 2 - DB往往更加语言稳定,因此当新版本的RDBMS出现时,它通常不会改变您编写查询或SP的方式,甚至不会改变。 Writing your logic in DB will reduce code adaptation because of a new DB version 在DB中编写逻辑会因为新的DB版本而减少代码调整

3 - A RDBMS is more likely to be alive for a long period rather than a programming language. 3 - RDBMS更有可能长期存在而不是编程语言。 Also, as your data is critical, there is a big worry from the RDBMS developers for automatic migration of your whole data to the new RDBMS version, including your SPs. 此外,由于您的数据至关重要,RDBMS开发人员担心将您的整个数据自动迁移到新的RDBMS版本(包括SP)。 When clipper died, there were no ways to migrate systems to a new programming language, they had to be completely rewritten. 当clipper死亡时,没有办法将系统迁移到新的编程语言,它们必须被完全重写。

4 - If you think someday to change completely the language you are writing the application for some reason(language death, for example), the only thing to be rewritten will be the presentation and the SP calls, not business logic. 4 - 如果您认为有一天要完全改变您正在编写应用程序的语言(例如语言死亡),唯一需要重写的是演示和SP调用,而不是业务逻辑。

I'd like to know from other people here if what I pointed out makes sense, and if not, why. 我想知道其他人在这里是否有意义,如果没有,为什么。 I'm on the same situation as Sabeen Malik, I'm thinking to begin my first huge project and I'm tending towards SPs because of what I wrote. 我和Sabeen Malik的情况相同,我正在考虑开始我的第一个大项目,因为我所写的内容,我正在倾向于SP。 So it's time to correct my POV if it's not so correct. 所以现在是时候纠正我的POV,如果它不是那么正确的话。

MySQL sucks at using advanced DB techniques, it's simple and fast. MySQL很擅长使用高级数据库技术,它简单而快速。 PHP, being a dynamic language, makes processing data very easy. PHP是一种动态语言,使处理数据变得非常容易。 Therefore, it usually makes sense to use PHP. 因此,使用PHP通常是有意义的。

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

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