SQL Server 关系隐藏在存储过程而不是模式中
目前,我们几乎没有引用完整性,并且拥有许多自连接的表(实际上,最好将其表示为单独的表或连接的视图)。
这些表如何相互关联的知识隐含在存储过程的逻辑中,而不是显式地存在于架构中。我们正在考虑改变这一点。
第一步是真正理解隐含的关系并记录它们。
所以我的问题是...
提取隐式信息的最佳方法是什么,除了关注每个存储过程之外。我会考虑任何工具,编写自己的 SQL 来查询系统表,或者利用 SQL-DMO 模型 - 或者事实上任何可以让计算机做更多工作而我做更少工作的工具。
At present we have very little referential integrity, as well as having a number of tables that self-join (and indeed would perhaps better be represented as separate tables or views that joined).
The knowledge of how these tables relate to each other is implicit in the logic of the stored procedures rather than explicit in the schema. We are considering changing this.
The first step is to actually understand the implicit relationships and document them.
So my question is...
What is the best way to extract that implicit information, short of eyeballing every stored procedure. I will consider any tools, writing my own SQL to interrogate the system tables, or utilising the SQL-DMO model - or in fact anything under the sun that lets the computer do more work and me do less.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果关系仅通过 SP 中的联接来识别,那么您将不会有太多运气将其自动化。
使用探查器捕获查询以首先找到最频繁的连接可能是值得的。
If the relationships are only identified by joins in the SPs, then you're not going to have a lot of luck automating it.
It might be worthwhile capturing queries using the profiler to find the most frequent joins first.
当谈到重构时,我是老派:
When it comes to refactoring, I am the old-school:
您可以使用
sys.sql_dependency
来找出SP依赖的列和表(如果您不在SP中执行SELECT *
,则会有所帮助)。这至少可以帮助您获得候选清单:您可能必须使用 sp_refreshsqlmodule 来确保依赖项是最新的才能正常工作。即,如果您更改视图,则需要在每个非架构绑定模块上进行
sp_refreshsqlmodule
(显然,架构绑定模块首先不允许进行任何底层更改 - 但您会得到一个如果您对依赖于该视图的架构绑定对象调用sp_refreshsqlmodule
,则会出现错误。您可以通过对这些对象调用sp_refreshsqlmodule
来自动执行此操作:You can use
sys.sql_dependencies
to find out what columns and tables an SP depends on (helps if you don't doSELECT *
in your SPs). This will help you get an inventory of candidates at least:You might have to use
sp_refreshsqlmodule
to ensure that the dependencies are up to date for that to work. i.e. if you change a view, you need tosp_refreshsqlmodule
on each non-schema-bound module (obviously schema-bound modules don't allow any underlying changes changes in the first place - but you will get an error if you callsp_refreshsqlmodule
on a schema-bound object) which depended on that view. You can automate that by callingsp_refreshsqlmodule
on these objects: