SQL Server 关系隐藏在存储过程而不是模式中

发布于 2024-08-13 11:25:41 字数 272 浏览 3 评论 0原文

目前,我们几乎没有引用完整性,并且拥有许多自连接的表(实际上,最好将其表示为单独的表或连接的视图)。

这些表如何相互关联的知识隐含在存储过程的逻辑中,而不是显式地存在于架构中。我们正在考虑改变这一点。

第一步是真正理解隐含的关系并记录它们。

所以我的问题是...

提取隐式信息的最佳方法是什么,除了关注每个存储过程之外。我会考虑任何工具,编写自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请你别敷衍 2024-08-20 11:25:41

如果关系仅通过 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.

泡沫很甜 2024-08-20 11:25:41

当谈到重构时,我是老派:

  1. 记录你所拥有的,使用可视化工具。
  2. 以书面形式描述该数据库捕获的业务模型。
  3. 从描述名词和现有模式中挑选出实体。
  4. 创建新的 ER 模型;在此期间咨询业务。
  5. 根据 ER
  6. ETL 数据创建一个新数据库并转移到新数据库并进行测试。

When it comes to refactoring, I am the old-school:

  1. Document what you have, use visual tool.
  2. Describe -- in writing -- the business model that this database captures.
  3. Pick-out entities out of the description nouns and the existing schema you have.
  4. Create a new ER model; consult with business while at it.
  5. Create a new DB based on the ER
  6. ETL data over to the new db and test.
嘿嘿嘿 2024-08-20 11:25:41

您可以使用sys.sql_dependency来找出SP依赖的列和表(如果您不在SP中执行SELECT *,则会有所帮助)。这至少可以帮助您获得候选清单:

referenced_major_id == the OBJECT_ID of the table
referenced_minor_id == the column id: COLUMNPROPERTY(referenced_major_id,
                                                       COLUMN_NAME,
                                                       'ColumnId')

您可能必须使用 sp_refreshsqlmodule 来确保依赖项是最新的才能正常工作。即,如果您更改视图,则需要在每个非架构绑定模块上进行 sp_refreshsqlmodule (显然,架构绑定模块首先不允许进行任何底层更改 - 但您会得到一个如果您对依赖于该视图的架构绑定对象调用 sp_refreshsqlmodule ,则会出现错误。您可以通过对这些对象调用 sp_refreshsqlmodule 来自动执行此操作:

SELECT *
FROM    INFORMATION_SCHEMA.ROUTINES
WHERE   OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) + '.'
                                 + QUOTENAME(ROUTINE_NAME)),
                       N'IsSchemaBound') IS NULL
        OR OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) + '.'
                                    + QUOTENAME(ROUTINE_NAME)),
                          N'IsSchemaBound') = 0

You can use sys.sql_dependencies to find out what columns and tables an SP depends on (helps if you don't do SELECT * in your SPs). This will help you get an inventory of candidates at least:

referenced_major_id == the OBJECT_ID of the table
referenced_minor_id == the column id: COLUMNPROPERTY(referenced_major_id,
                                                       COLUMN_NAME,
                                                       'ColumnId')

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 to sp_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 call sp_refreshsqlmodule on a schema-bound object) which depended on that view. You can automate that by calling sp_refreshsqlmodule on these objects:

SELECT *
FROM    INFORMATION_SCHEMA.ROUTINES
WHERE   OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) + '.'
                                 + QUOTENAME(ROUTINE_NAME)),
                       N'IsSchemaBound') IS NULL
        OR OBJECTPROPERTY(OBJECT_ID(QUOTENAME(ROUTINE_SCHEMA) + '.'
                                    + QUOTENAME(ROUTINE_NAME)),
                          N'IsSchemaBound') = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文