根据列名确定索引

时间:2013-02-21 14:22:46

标签: sql sql-server indexing

是否有任何方法可以将列名与索引所具有的列名匹配。

例如,假设index1使用列名,年龄,地址和index2使用(在同一个表中)名称,年龄。(一个特定表的全部)

如果我将名称,年龄和表名传递给存储过程,我应该写什么查询,它将返回index2而不是index1。

我遇到了许多如何列出索引和表名列的示例,如:

How can we check that table have index or not?

但是仍然有问题为我的用法编写sql查询。

非常感谢任何帮助,

谢谢

(这适用于Microsoft sql server 2008 - 2012)

这是我写的,它没有正常工作,因此问题

select index1.name,sys.tables.name, Stuff((SELECT ',' + sys.columns.name AS [text()]
FROM
(
     select sys.columns.name
     from sys.columns
     inner join sys.index_columns On sys.index_columns.index_column_id=sys.columns.column_id
     inner join sys.indexes on sys.indexes.index_id=sys.index_columns.index_id 
     where sys.indexes.index_id=index1.index_id
) x
For XML PATH (''), type ).value('(./text())[1]','NVARCHAR(MAX)'),1,1,'') As coLName
from sys.indexes as index1 
inner join sys.tables on sys.tables.object_id=index1.object_id
inner join sys.index_columns On index1.index_id=sys.index_columns.index_id  AND sys.index_columns.object_id = sys.tables.object_id
inner join sys.columns on sys.columns.column_id=sys.index_columns.column_id And sys.columns.object_id=sys.tables.object_id
where sys.tables.name=TABLE_NAME

2 个答案:

答案 0 :(得分:1)

您可以使用CTE

尝试此查询
 DECLARE @searchIndex nvarchar(100) = 'name,age',
         @tableName nvarchar(100) = 'your_tableName'
;WITH cte AS
 (
  SELECT i.name AS index_name, c.name      
  FROM sys.indexes i LEFT JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
                     LEFT JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
  WHERE i.object_id = OBJECT_ID(@tableName) AND ic.is_included_column != 1
  ), cte2 AS
 (
  SELECT c2.index_name, 
    STUFF((SELECT ',' + c.name
           FROM cte c
           WHERE c.index_name = c2.index_name
  FOR XML PATH, TYPE).value('.[1]', 'nvarchar(max)'), 1, 1, '') [columns]
  FROM cte c2
  GROUP BY c2.index_name 
  )
  SELECT *
  FROM cte2
  WHERE [columns] = @searchIndex

在第二种情况下,无论顺序指定列和空格

DECLARE @searchIndex nvarchar(100) = '  age, name' ,
        @tableName nvarchar (100) = 'your_tableName'       
;WITH ParsSearchIndex AS
 (
  SELECT SUBSTRING(@searchIndex , 0 , CHARINDEX ( ',' , @searchIndex )) AS val ,
         CAST(STUFF (@searchIndex + ',' , 1, CHARINDEX( ',', @searchIndex), '') AS nvarchar (100 )) AS stval        
  UNION ALL
  SELECT LTRIM(SUBSTRING (stval , 0, CHARINDEX( ',', stval))),
         CAST(STUFF (stval , 1, CHARINDEX( ',' , stval ), '' ) AS nvarchar(100 ))        
  FROM ParsSearchIndex
  WHERE stval != ''
  ), max_ParsSearchIndex AS
 (
  SELECT val, COUNT(*) OVER() AS cnt
  FROM ParsSearchIndex
  WHERE val != ''
  ), cte AS
 (
  SELECT i.name AS index_name, c.name , ic .is_included_column,
         MAX(ic .index_column_id) OVER( PARTITION BY i. index_id) AS maxIndex_column_id
  FROM sys.indexes i LEFT JOIN sys. index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
                     LEFT JOIN sys. columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
  WHERE i.object_id = OBJECT_ID(@tableName )
  ), cte2 AS
 (
  SELECT c2.index_name , COUNT (*) AS cnt, c2. maxIndex_column_id,
    STUFF((SELECT ',' + c .name
           FROM cte c
           WHERE c .index_name = c2 .index_name        
  FOR XML PATH , TYPE ).value( '.[1]', 'nvarchar(max)'), 1 , 1, '') [columns]
  FROM cte c2
  WHERE c2.is_included_column != 1 AND EXISTS (
                         SELECT 1
                         FROM max_ParsSearchIndex p
                         WHERE c2 .name = p .val AND p .cnt = c2.maxIndex_column_id
                         )
  GROUP BY c2 .index_name, c2. maxIndex_column_id
  )
  SELECT index_name, [columns]
  FROM cte2
  WHERE cnt = maxIndex_column_id

答案 1 :(得分:0)

这对我有用。

DECLARE @searchIndex nvarchar(100) = 'age,name',
        @schemeName nvarchar(100) = 'dbo',
        @tableName nvarchar(100) = 'tableName'

SELECT
    name
FROM (SELECT
    indexes.name,
    (SELECT
            c1.name + ','
    FROM sys.schemas s1
    INNER JOIN sys.tables t1
        ON s1.schema_id = t1.schema_id
        AND tables.object_id = t1.object_id
    INNER JOIN sys.columns c1
        ON t1.object_id = c1.object_id
    INNER JOIN sys.indexes i1
        ON t1.object_id = i1.object_id
        AND indexes.index_id = i1.index_id
    INNER JOIN sys.index_columns ic1
        ON t1.object_id = ic1.object_id
        AND i1.index_id = ic1.index_id
        AND c1.column_id = ic1.column_id
    WHERE schemas.schema_id = s1.schema_id
    ORDER BY c1.name
    FOR xml PATH (''))
    AS colName
FROM sys.schemas
INNER JOIN sys.tables
    ON schemas.schema_id = tables.schema_id
    AND tables.name = @tableName
INNER JOIN sys.indexes
    ON tables.object_id = indexes.object_id
WHERE schemas.name = @schemeName) A
WHERE A.colName = @searchIndex + ','