Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you list all the indexed views in SQL Server?

Tags:

How can you get a list of the views in a SQL server database that have indexes (i.e. indexed views)?

I've found it's pretty easy to run an "ALTER VIEW" as I'm developing and overlook that I'm not only editing the view but also dropping an existing index. So I thought it would be nice to have a little utility query around that would list me off all the views with indexes.

like image 652
EBarr Avatar asked Feb 10 '11 21:02

EBarr


People also ask

How do I list all indexes in SQL Server?

You can use the sp_helpindex to view all the indexes of one table. And for all the indexes, you can traverse sys. objects to get all the indexes for each table. Only problem with this is that it only includes the index key columns, not the included columns.

How do you view indexes in SQL Server?

Introduction to SQL Server indexed view To create an indexed view, you use the following steps: First, create a view that uses the WITH SCHEMABINDING option which binds the view to the schema of the underlying tables. Second, create a unique clustered index on the view. This materializes the view.

How can we get the list of all the indexes on a table?

To see indexes for all tables within a specific schema you can use the STATISTICS table from INFORMATION_SCHEMA: SELECT DISTINCT TABLE_NAME, INDEX_NAME FROM INFORMATION_SCHEMA. STATISTICS WHERE TABLE_SCHEMA = 'your_schema'; Removing the where clause will show you all indexes in all schemas.


2 Answers

SELECT o.name as view_name, i.name as index_name     FROM sysobjects o          INNER JOIN sysindexes i              ON o.id = i.id      WHERE o.xtype = 'V' -- View 
like image 188
Joe Stefanelli Avatar answered Sep 20 '22 14:09

Joe Stefanelli


I like using the newer system tables:

select      OBJECT_SCHEMA_NAME(object_id) as [SchemaName],     OBJECT_NAME(object_id) as [ViewName],     Name as IndexName from sys.indexes where object_id in    (     select object_id     from sys.views   ) 

The inner join version

select      OBJECT_SCHEMA_NAME(si.object_id) as [SchemaName],     OBJECT_NAME(si.object_id) as [ViewName],     si.Name as IndexName from sys.indexes AS si inner join sys.views AS sv     ON si.object_id = sv.object_id 
like image 20
Michael J Swart Avatar answered Sep 19 '22 14:09

Michael J Swart