0

给定一个具有以下定义的表:

create table consTest(c1 integer null constraint consTest1 check(c1 < 100),
                      c2 integer not null constraint consTestU1 unique,
                      c3 integer not null,
                      c4 integer null,
                      constraint consTest2 check(c2 > c1),
                      constraint consTestU2 unique(c3))

我的应用程序对系统表执行以下查询,以描述表中列的约束关系:

SELECT object_schema_name(t17.referencing_id), object_name(t17.referencing_id),
       coalesce(col_name(t17.referenced_id, t17.referenced_minor_id), NULL),
       t16.definition, t16.type
FROM   { oj sys.check_constraints t16 INNER JOIN  sys.sql_expression_dependencies t17 ON ( t17.referencing_id = t16.object_id )  }  
WHERE t17.referenced_id = object_id('consTest') AND
      t16.type = 'C' AND
      object_name(t17.referencing_id) = 'consTest2'
ORDER BY 1 ASC, 2 ASC, 3 ASC

执行此查询时,我看到 SQL Server 偶尔出现间歇性崩溃。我的问题不是关于崩溃,而是看看是否有人可以推荐我可以对我的查询进行的任何优化。

4

1 回答 1

0

在我的机器上,原始查询的估计子树成本为 .0100221 与 .0213887:

SELECT object_schema_name(t17.referencing_id)
    , object_name(t17.referencing_id)
    , coalesce(col_name(t17.referenced_id, t17.referenced_minor_id), NULL)
    , t16.[definition]
    , t16.[type]
FROM sys.sql_expression_dependencies t17
JOIN sys.check_constraints t16
    ON t17.referencing_id = t16.object_id
    AND t16.[type] = 'C'
    AND object_name(t17.referencing_id) = 'consTest2'
    AND t17.referenced_id = object_id('consTest');
GO
于 2013-04-10T00:50:17.787 回答