简体   繁体   English

什么是最好的灵活性,为什么? 使用db视图,db表,存储过程。 和表中的对象

[英]What is best flexibility and why? Using db views, db tables, stored proc. and objects in tables

I want to know what is best practice for using db views, db tables, stored proc. 我想知道使用db视图,db表,存储过程的最佳实践。 and objects in tables... Which of these is more flexible and why, can You explain? 表格中的对象......其中哪一个更灵活,为什么,你能解释一下吗?

Each tool has its uses . 每个工具都有其用途 Your choices will depend on the nature of the application, and its security, performance, and agility requirements. 您的选择取决于应用程序的性质,安全性,性能和敏捷性要求。

Nowadays many programmers use Data Access Layers ( DALs ) for this sort of thing. 如今,许多程序员使用数据访问层( DAL来做这类事情。 Many DALs allow you to specify views and stored procedures to call. 许多DAL允许您指定要调用的视图和存储过程。 But you can also run queries against the tables directly, without the need for stored procedures or views. 但您也可以直接对表运行查询,而无需存储过程或视图。

Unless you are using an object database, you will be dealing with tables rather than objects. 除非您使用对象数据库,否则您将处理而不是对象。 Most applications nowadays use table-based database systems, because they are so common , and you can use DALs to manage the object-relational impedance mismatch . 现在大多数应用程序都使用基于表的数据库系统, 因为它们非常常见 ,您可以使用DAL来管理对象 - 关系阻抗不匹配

Stored procedures are used when high-performance is needed, and programmatic things need to be accomplished on the database itself (the addition of a timestamp value perhaps, or the addition/subtraction of child records). 当需要高性能时使用存储过程 ,并且需要在数据库本身上完成编程事务(可能添加时间戳值,或者添加/减少子记录)。 A good DAL will provide high performance without necessarily requiring the use of stored procedures. 良好的DAL将提供高性能,而不需要使用存储过程。

Views are used to manage the interface between the database and the consumer of the data. 视图用于管理数据库与数据使用者之间的接口。 In particular, the data can be filtered for security purposes. 特别是,出于安全目的,可以过滤数据。 In large database scenarios, a DBA designs and creates the tables, and manages the views and stored procedures that the user is allows to use to access the data. 在大型数据库方案中,DBA设计并创建表,并管理用户允许用于访问数据的视图和存储过程。

If you are looking for ultimate flexibility, most of what you need to do can be accomplished in the DAL, without the need for views or stored procedures. 如果您正在寻求最大的灵活性,那么您需要做的大部分工作都可以在DAL中完成,而无需视图或存储过程。 But again, it depends on what your application's requirements are. 但同样,这取决于您的应用程序的要求。 I would say that the larger your application and user base is, the more likely you are to use views and stored procedures in your application. 我会说,您的应用程序和用户群越大,您​​在应用程序中使用视图和存储过程的可能性就越大。

I would say that, for the most part, stored procedures are a relic of the '90s: 我会说,在大多数情况下,存储过程是90年代的遗留物:

  • completely database-dependent 完全依赖于数据库
  • bad language choice for general purpose programming, regardless if it's plpgsql, t-sql or something else 通用编程的糟糕语言选择,无论是plpgsql,t-sql还是别的
  • hard to debug 很难调试
  • low code scalability, a problem shared with any procedural programming language 低代码可伸缩性,与任何过程编程语言共享的问题
  • code versioning issues 代码版本问题

That's not to say that they (like triggers, views and rules) don't have anything to give: large scale reporting and data aggregation is one example of tasks at which they handle fairly well. 这并不是说它们(如触发器,视图和规则)没有任何东西可以给出:大规模报告和数据聚合是它们处理得相当好的任务的一个例子。 For the rest, logic is better placed in the business logic layer (a service, domain entities...whatever) where a variety of tools and more advanced programming paradigms are available. 对于其余部分,逻辑更好地放置在业务逻辑层(服务,域实体......等等)中,其中有各种工具和更高级的编程范例可用。

Ditto for views and triggers. 同样适用于观点和触发器。

In eg a Java environment, JPA does much better 90+% of the time: 在例如Java环境中,JPA在90%以上的时间内做得更好:

  • learn one query language and apply it to any database 学习一种查询语言并将其应用于任何数据库
  • the business logic is more focused, in one place in the application, the BLL 业务逻辑更集中于应用程序中的一个位置BLL
  • the code is easier to read and write and it's easier to find people who understand it 代码更易于阅读和编写,并且更容易找到理解它的人
  • it's possible to express logic spanning multiple databases in a single unit of code 可以在单个代码单元中表达跨越多个数据库的逻辑

...and the list goes on. ...而这样的例子不胜枚举。

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

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