2013-04-09 58 views
0

考慮,定義如下表:如何優化這個SQL Server系統表查詢

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間歇性崩潰。我的問題不是關於崩潰,而是看是否有人可以推薦我可以對我的查詢進行的任何優化。

回答

0

在我的機器我得到的0.0100221估計子樹成本VS 0.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