Emi*_*lás 65 mysql database performance database-design stored-procedures
我们正处于一个新项目的开始阶段,我们真的想知道是否应该在MySQL中使用存储过程.
我们仅使用存储过程来插入和更新业务模型实体.有几个表代表模型实体,我们将在那些存储过程insert/update中抽象它.
另一方面,我们可以从Model层调用insert和update,但不能在MySQL中调用,而是在PHP中调用.
根据您的经验,哪个是最佳选择?两种方法的优点和缺点.哪个是高性能最快的?
PS:这是一个大多数阅读和高性能的网络项目是最重要的必要条件.
Boh*_*ian 72
与实际的编程语言代码不同,它们:
如果您有一个特定于数据库的操作(例如,用于维护数据库完整性的事务内操作),或者保持您的过程非常原子和简单,那么您可能会考虑它们.
在预先指定"高性能"时,建议小心.它往往会导致糟糕的选择而牺牲良好的设计,它会比你想象的更快地咬你.
使用存储过程是您自己的危险(来自那里的人,从不想回去).我的建议是像瘟疫一样避免它们.
dav*_*vek 30
与编程代码不同,它们:
但是,正如波西米亚人所说的,还有很多缺点(这只是提供另一种观点).在决定什么对你最有利之前,你可能需要进行基准测试.
Seb*_*bas 14
As for performances, they have the potential to be really performant in a future MySQL version (under SQL Server or Oracle, they are a real treat!). Yet, for all the rest... They totally blow up competition. I'll summarize:
Security: You can give your app the EXECUTE right only, everything is fine. Your SP will insert update select ..., with no possible leak of any sort. It means global control over your model, and an enforced data security.
Security 2: I know it's rare, but sometimes php code leaks out from the server (i.e. becomes visible to public). If it includes your queries, possible attackers know your model. This is pretty odd but I wanted to signal it anyway
Task force: yes, creating efficient SQL SPs requires some specific resources, sometimes more expensive. But if you think you don't need these resources just because you're integrating your queries in your client... you're going to have serious problems. I'd mention the analogy of web development: it's good to separate the view from the rest because your designer can work on their own technology while the programmers can focus on programming the business layer.
Encapsulating business layer: using stored procedures totally isolates the business where it belongs: the damn database.
Quickly testable: one command line under your shell and your code is tested.
Independence from the client technology: if tomorrow you'd like to switch from php to something else, no problem. Ok, just storing these SQL in a separate file would do the trick too, that's right. Also, good point in the comments about if you decide to switch sql engines, you'd have a lot of work to do. You have to have a good reason to do that anyway, because for big projects and big companies, that rarely happens (due to the cost and HR management mostly)
Enforcing agile 3+-tier developments: if your database is not on the same server than your client code, you may have different servers but only one for the database. In that case, you don't have to upgrade any of your php servers when you need to change the SQL related code.
好吧,我认为这是我在这个问题上最重要的事情.我开发了两种精神(SP与客户端),我真的非常喜欢SP风格.我只是希望Mysql有一个真正的IDE为他们,因为现在它是有限的痛苦.
存储过程很好用,因为它们可以使您的查询井井有条,并允许您一次执行批处理.存储过程通常可以快速执行,因为它们是预编译的,与每次运行时编译的查询不同.这对数据库位于远程服务器上的情况有很大影响; 如果查询在PHP脚本中,则应用程序和数据库服务器之间存在多个通信 - 查询将被发送,执行并返回结果.但是,如果使用存储过程,则只需要发送一个小的CALL语句而不是大而复杂的查询.
可能需要一段时间才能适应编程存储过程,因为它们有自己的语言和语法.但是一旦你习惯了它,你会发现你的代码非常干净.
在性能方面,如果您使用存储过程,它可能不会有任何重大收益.
我会让我知道我的意见,尽管我的强硬可能与这个问题没有直接关系:
与许多问题一样,关于使用存储过程或应用程序层驱动的解决方案的回复依赖于将推动整体工作的问题:
您是在尝试批量操作还是在线操作?他们是完全交易的吗?这些行动有多复发?等待数据库的工作量有多重?
你有什么样的数据库技术?什么样的基础设施?您的团队是否接受过数据库技术的全面培训?您的团队是否能够更好地构建数据库诊断解决方案?
没有秘密.
您的解决方案是否需要分发到多个位置?是您使用远程通信所需的解决方案吗?您的解决方案是在多个数据库服务器上工作,还是可能使用基于群集的架构?
需要多少申请才能更改?你有经过专门训练的个人维护解决方案吗?
您是否看到您的数据库技术将在短期,中期,长时间内发生变化?您是否经常需要迁移解决方案?
使用一种或另一种策略实施该解决方案需要多少费用?
这些点的总体将推动答案.因此,在决定是否使用任何策略时,您必须关注每一点.有些情况下,存储过程的使用优于应用程序层管理的查询,而其他情况下,执行查询和使用基于应用程序层的解决方案时最好.
在以下情况下,使用存储过程往往更加充分:
在以下情况下,使用应用层驱动的解决方案往往更加充分:
希望这可能有助于任何人问自己什么是更好用.
归档时间: |
|
查看次数: |
23268 次 |
最近记录: |