优化系统视图

时间:2020-04-16 10:01:53

标签: sql sql-server database

我有一个查询,用于从Microsoft SQL Server数据库获取列数据。我需要加快查询速度,因为我将每分钟运行多次。目前,我的查询大约需要60毫秒才能在SQL Server上执行。将它缩短到30或40ms也很棒。

执行计划:plan

有没有可能的优化方法?

SELECT 
    c.name as 'columnName',
    t.name as 'dataType',
    t.name + '(' + cast(c.max_length as varchar(50)) +')' AS 'dataTypeFull', 
    ISNULL(sep.value,'') AS 'columnDescription',
    CASE 
      WHEN fk.object_id is not null THEN fk.name ELSE NULL 
    END AS 'foreignKey',
    pk_tab.*,
    ISNULL(i.is_primary_key,0) AS 'primaryKey'
  FROM
    sys.tables tab
  INNER JOIN 
    sys.columns c ON c.object_id = tab.object_id
  LEFT OUTER JOIN 
    sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
  LEFT OUTER JOIN 
    sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
  LEFT OUTER JOIN 
    sys.foreign_key_columns fk_cols ON fk_cols.parent_object_id = tab.object_id
                AND fk_cols.parent_column_id = c.column_id
  LEFT OUTER JOIN 
    sys.foreign_keys fk ON fk.object_id = fk_cols.constraint_object_id
  LEFT OUTER JOIN 
    sys.tables pk_tab ON pk_tab.object_id = fk_cols.referenced_object_id
  LEFT JOIN 
    sys.extended_properties sep ON c.object_id = sep.major_id 
                AND c.column_id = sep.minor_id 
                AND sep.name = 'MS_Description'
  JOIN 
    sys.types t ON c.user_type_id = t.user_type_id
  WHERE 
    c.object_id = Object_id('some-table-name')

0 个答案:

没有答案