sql server 视图

We’ve recently had production failures because our developers changed an important reference. In this case, we had a view which several procedures and views referenced. A developer made a change to the referenced view by removing columns, which caused several procedures and a view that referenced it to fail. We’re considering whether we should stop this practice, or if there are other ways we can prevent changes to an object that’s being referenced by other objects (in our case, a view).

我们最近发生了生产故障,因为我们的开发人员更改了重要参考。 在这种情况下,我们有一个引用了多个过程和视图的视图。 开发人员通过删除列对引用的视图进行了更改,这导致了多个过程以及引用该视图的视图失败。 我们正在考虑是否应该停止这种做法,或者是否有其他方法可以防止对其他对象(在本例中为视图)引用的对象进行更改。

总览 (Overview)

In some cases, a view or procedure – which other objects reference – can be a convenient development style as it allows for re-use of code over redevelopment. We’ll look at a contrived example of using a referenced view, by creating one with a view and procedure that both reference it to simulate an example of how a change to it may affect references.

在某些情况下,其他对象引用的视图或过程可能是一种方便的开发样式,因为它允许在重新开发时重新使用代码。 我们将通过使用一个被引用的视图创建一个人为的示例,方法是创建一个都带有被引用的视图和过程的程序,以模拟一个示例,说明对其进行更改可能如何影响引用。

创建一个参考对象 (Creating a referenced object)

Our first step will involve creating a referenced object, which in this case will be a view. In the below code we create the view, vw_ReferencedView.

我们的第一步将涉及创建一个引用的对象,在这种情况下将是一个视图。 在下面的代码中,我们创建视图vw_ReferencedView。

CREATE VIEW vw_ReferencedView
ASSELECT tt.Id LoadId, tt.LdDate LoadDate, t.TformDate TransformDateFROM tbTform tINNER JOIN tbInitLdr tt ON t.LdId = tt.Id

Next, we will create two references: a view that references our referenced view vw_ReferencesOtherView and a procedure stp_ReferencesView. In both of these objects, we refer to a column that’s returned from the referenced view, vw_ReferencedView.

接下来,我们将创建两个引用:一个引用我们引用的视图vw_ReferencesOtherView的视图和一个stp_ReferencesView过程。 在这两个对象中,我们都引用从参考视图vw_ReferencedView返回的列。

CREATE VIEW vw_ReferencesOtherView
ASSELECT vr.*, p.PartitionDateFROM vw_ReferencedView vrINNER JOIN tbPartition p ON vr.LoadId = p.LdIdCREATE PROCEDURE stp_ReferencesView
@date DATETIME
AS
BEGINSELECT r.*, p.PartitionDateFROM vw_ReferencedView rINNER JOIN tbPartition p ON r.LoadId = p.LdIdWHERE r.LoadDate > @date
END

What if we removed a column from our referenced view? It’s possible that it may not matter with selecting all columns from the view reference, but it may impact the columns that we use. What if we changed the name of a column? We’ll simulate this by changing LoadId to LdId in the vw_ReferencedView. We can see in the below examples when we try to execute the procedure or select from the second view that both fail:

如果我们从引用的视图中删除了列怎么办? 从视图引用中选择所有列可能没有关系,但可能会影响我们使用的列。 如果我们更改列名怎么办? 我们将通过在vw_ReferencedView中将LoadId更改为LdId进行模拟。 当我们尝试执行该过程或从第二个视图中选择均失败时,可以在下面的示例中看到:

ALTER VIEW vw_ReferencedView
ASSELECT tt.Id LdId, tt.LdDate LoadDate, t.TformDate TransformDateFROM tbTform tINNER JOIN tbInitLdr tt ON t.LdId = tt.Id---- Error 1:
SELECT * FROM vw_ReferencesOtherView
---- Error 2:
EXEC stp_ReferencesView '2017-01-01'

While we use names for clarity in our example, we should also be careful about names. Some testing software that creates a new test database for the purpose of running unit tests may build objects by alphabetic order. This means that a referenced object with a name starting with “v” will come after an object with a starting letter of “a”, and that will cause issues if the object starting with “a” references the object starting with “v.” If you’re using testing software that does this, any referenced object will need to be named appropriately.

虽然在示例中使用名称是为了清楚起见,但我们也应该注意名称。 一些为了运行单元测试而创建新测试数据库的测试软件可能会按字母顺序构建对象。 这意味着名称以“ v”开头的引用对象将出现在以“ a”开头的对象之后,如果以“ a”开头的对象引用以“ v”开头的对象,则将引起问题。 如果您正在使用执行此操作的测试软件,则任何引用的对象都需要适当地命名。

使用参考对象进行开发 (Development with referenced objects)

As we can observe, creating a referenced object, such as our example of a view being referenced by other objects, does allow for code re-use and can be a convenient way of development since we don’t have to add an entire block of code to new objects. But we should consider a few scenarios where this is not appropriate.

正如我们所观察到的,创建引用的对象(例如我们的视图被其他对象引用的示例)确实允许代码的重用,并且由于无需添加完整的代码块,因此它是一种便捷的开发方式。编码到新对象。 但是我们应该考虑一些不合适的方案。

We want to be sure that our underlying reference, such as a view, in this case, isn’t a query we’ll be changing frequently (if at all). If I’m creating a view that selects some columns from a table along with another joined table, this is seldom an appropriate use case, as the list of columns can change. In addition, unless there’s a performance improvement of an indexed view, there’s little benefit to this design from a code perspective. By contrast, if I’m using a view to order or partition data by a primary key with a result set intended for re-use, this may be more appropriate. In the same manner, with some remote queries from one server to another, I may use a view on one server because the second server’s query will determine what’s needed in that view – thus a change in the query needs will directly affect the view on the other server.

我们希望确保在这种情况下,我们的基础引用(例如视图)不是我们会经常更改的查询(如果有的话)。 如果我要创建一个从表中选择一些列以及另一个联接表的视图,那么这很少是一个合适的用例,因为列的列表可以更改。 另外,除非索引索引的性能得到改善,否则从代码角度来看,此设计几乎没有好处。 相比之下,如果我使用视图通过主键对数据进行排序或分区,而结果集打算重用,则此方法可能更合适。 同样,对于从一台服务器到另一台服务器的一些远程查询,我可能会在一台服务器上使用一个视图,因为第二台服务器的查询将确定该视图中所需的内容,因此查询需求的变化将直接影响该服务器上的视图。其他服务器。

These design considerations demarcate a referenced object that either seldom changes or must change relative to its reference (one reference versus many) against a design where we’re simply re-using a view for convenience without thought to how often we may need to change the referenced view

这些设计注意事项将参照对象划分为相对于其参照(一个参照与多个参照)很少更改或必须更改的参照对象,而在设计中我们只是重复使用视图以方便使用,而不考虑我们可能需要多久更改一次参考视图

In the case of multiple object references, as long as we don’t change the referenced view’s definition (vw_ReferencedView in our contrived example), changes to the objects referencing it won’t affect the referenced view. By adding notes about which objects reference it, we can review these objects if we ever need to make changes to our referenced view. These comments can easily be added to the referenced view when we create a new object to reference it. To see an example with this from our above code, in vw_ReferencedView, we would add a comment each time we added another reference:

对于多个对象引用,只要我们不更改引用视图的定义(在我们人为的示例中为vw_ReferencedView),对引用它的对象的更改将不会影响引用视图。 通过添加有关哪些对象引用它的注释,如果我们需要对引用的视图进行更改,我们可以查看这些对象。 当我们创建一个新对象来引用它们时,可以轻松地将这些注释添加到引用视图中。 为了从上面的代码中看到一个示例,在vw_ReferencedView中,每次添加另一个引用时,我们都会添加一个注释:

ALTER VIEW vw_ReferencedView
AS---- References view: vw_ReferencesOtherView---- References view: stp_ReferencesViewSELECT tt.Id LoadId, tt.LdDate LoadDate, t.TformDate TransformDateFROM tbTform tINNER JOIN tbInitLdr tt ON t.LdId = tt.Id

The cost to this approach is training new team members to add a comment with the new reference when they create one along with checking if a view has references.

这种方法的成本是培训新的团队成员在创建新的参考时添加评论,并检查视图是否包含参考。

We can also use object metadata from syscomments, which allows us to search for names of objects with objects. In the below code, we look for objects where the text of the object, such as stored procedure or view text, have the words vw_referencedview in it.

我们还可以使用syscomments中的对象元数据,这使我们可以搜索带有对象的对象名称。 在下面的代码中,我们查找对象,其中对象的文本(例如存储过程或视图文本)中包含单词vw_referencedview。

SELECT OBJECT_NAME(id) ObjectName, [text] ObjectText
FROM sys.syscomments
WHERE LOWER([text]) LIKE '%vw_referencedview%'

While this can provide a helpful shortcut, remember that this will still include objects that may have the name in a comment as a part of the object, not a query that references it. This will not assist if other databases reference our view – such as three remote database servers with a database that references our view. We would have to check every database on every server, which may be expensive relative to how many servers and databases we manage.

尽管这可以提供有用的快捷方式,但请记住,该对象仍将包括在注释中具有名称的对象作为对象的一部分,而不是引用该对象的查询。 如果其他数据库引用了我们的视图,这将无济于事–例如,三台远程数据库服务器的数据库引用了我们的视图。 我们将不得不检查每台服务器上的每个数据库,这相对于我们管理的服务器和数据库的数量而言可能是昂贵的。

In the syscomments query, I use the LOWER function with this query while keeping the object name in lower case. Per Microsoft, the column text is a nvarchar 4000, which means that a procedure or view that exceeds this length will get another entry. If you see duplicate objects, this may be why. In the below image, we see several entries for one procedure because the procedure text of this object exceeds the 4000 nvarchar limit.

在syscomments查询中,我将LOWER函数与此查询一起使用,同时将对象名称保持为小写。 对于Microsoft,列文本为nvarchar 4000,这意味着超出此长度的过程或视图将获得另一个条目。 如果看到重复的对象,这可能就是原因。 在下图中,我们看到一个过程的多个条目,因为此对象的过程文本超出了4000 nvarchar限制。

Unfortunately, as of the latest update from Microsoft, this is not available in AzureSQL (see below references).

不幸的是,从Microsoft的最新更新开始,此功能在AzureSQL中不可用(请参阅以下参考资料)。

Finally consider that with some changes, it’s less convenient to use a referenced view. If I have 3 procedures and 2 views that reference a view, and I need a column change for one procedure, I change both the procedure and the referenced view, provided it doesn’t impact the other references. If the referenced view is only one query, it would have been faster to have the query directly in the objects over the reference as I would only change one object. Assuming that the performance impact doesn’t change with this design, this scenario favors each object having the query directly.

最后考虑一下,通过一些更改,使用引用视图不太方便。 如果我有3个过程和2个引用一个视图的视图,并且我需要为一个过程更改列, 则只要不影响其他引用就可以同时更改该过程和被引用的视图。 如果引用的视图仅是一个查询,则直接在引用中的对象中进行查询会更快,因为我只会更改一个对象。 假设此设计不会影响性能,则此方案将直接使用每个具有查询的对象。

结论 (Conclusion)

Designing objects with re-usable queries that other objects can reference may save us significant development time. As long as we consider whether we’ll need to re-develop the referenced object or how we’ll track its references, from comments to metadata, when we do make changes, the references may introduce few problems. If there are situations where we may not need a reference due to a query being temporary or being easy to develop, we may want to consider it within an object over using a reference.

使用其他对象可以引用的可重用查询来设计对象可以节省大量的开发时间。 只要考虑是否需要重新开发引用的对象或如何跟踪它的引用(从注释到元数据),当我们进行更改时,引用可能不会带来什么问题。 如果在某些情况下由于查询是临时的或易于开发而可能不需要引用,则可能需要在对象内考虑使用引用。

翻译自: https://www.sqlshack.com/sql-server-development-practices-with-referenced-views/

sql server 视图

sql server 视图_SQL Server –具有引用视图的开发实践相关推荐

  1. java查看sql视图_SQL Server查看视图定义总结

    在SQL Server中如何查看数据库视图的定义呢? 其实官方文档已经有一个较详细的总结了,这里在官方文档的基础上,我们再深入展开分析一下,例如如何获取系统视图的定义.知其然知其所以然吗. 1:使用S ...

  2. python 查询sqlserver 视图_SQL Server 2017 数据库教与学(教学大纲,含Python+SQL Server案例)...

    原标题:SQL Server 2017 数据库教与学(教学大纲,含Python+SQL Server案例) 本书提供Python+SQL Server案例 SQL Server教学大纲 一.课程的性质 ...

  3. python 查询sqlserver 视图_SQL Server查看视图定义总结

    在SQL Server中如何查看数据库视图的定义呢? 其实官方文档已经有一个较详细的总结了,这里在官方文档的基础上,我们再深入展开分析一下,例如如何获取系统视图的定义.知其然知其所以然吗. 1:使用S ...

  4. sql server 跟踪_SQL Server跟踪标志指南; 从-1到840

    sql server 跟踪 SQL Server trace flags are configuration handles that can be used to enable or disable ...

  5. sql server 关联_SQL Server中的关联规则挖掘

    sql server 关联 Association Rule Mining in SQL Server is the next article in our data mining article s ...

  6. sql数据透视_SQL Server中的数据科学:取消数据透视

    sql数据透视 In this article, in the series, we'll discuss understanding and preparing data by using SQL ...

  7. 2008 r2 server sql 中文版补丁_SQL Server 2008 SP4 补丁

    SQL Server 2008 SP4 补丁对于客户而言,Microsoft SQL Server 2008 Service Pack 4 中的几个关键改进如下所示: 改进了从 SQL Server ...

  8. sql server 别名_SQL Server别名概述

    sql server 别名 This article gives an overview of SQL Server Alias and its usage for connecting with S ...

  9. sql server 面试_SQL Server审核面试问题

    sql server 面试 In this article, we will discuss a number of common and important SQL Server Audit que ...

最新文章

  1. java 16 binary_【图片】【困扰】java(tm) platform se binary 已停止工作该如何是好【minecraft吧】_百度贴吧...
  2. Objective-C Runtime 运行时之四:Method Swizzling
  3. 分布式SOA基础架构崭露头角
  4. 如何使用ES6在JavaScript中有条件地构建对象
  5. SQL:select case when(转)
  6. linux shell 多线程执行程序
  7. 没有什么不可能!郴州女孩江梦南,从双耳失聪到清华博士!
  8. 恐怖logo效果展示AE模板
  9. 强化学习_Deep Q Learning(DQN)_代码解析
  10. Snowflake id生成器
  11. 游戏框架(框架入门篇)
  12. NBT:根际微生物组抗番茄枯萎病(IF:35.724)
  13. 英语绕口令大全 练习你的口语
  14. (Mac) Mac上如何修改本地的hostname
  15. 面对妖艳的配置文件,python小技巧来帮你!
  16. python语言开发什么_python语言是什么语言开发的_Python语言的由来,编程语言简史...
  17. Flutter—— 仿自如APP裸眼3D效果
  18. 什么是敏捷管理 常用的敏捷Scrum会议有哪些
  19. 【终结扩散模型】Consistency Models.OpenAI开源新模型代码,一步成图,1秒18张
  20. 2021.11.16【读书笔记】丨宏基因组分析流程

热门文章

  1. 在线修改Schema
  2. 非printf形式的十六进制和二进制打印(雅虎面试题)
  3. c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
  4. cocos2d-x 3.1 编译脚本android-build.py
  5. Jaxb2 转换XML文档
  6. MVC如何分离Controller与View在不同的项目?
  7. MySQL---分组查询
  8. 动态图制作软件设计(二)
  9. java8 自动关闭资源_java9系列第二篇-资源自动关闭的语法增强
  10. π型滤波频率计算_一文看懂π型滤波电路原理