Question: How to Find Size of All the Indexes on the Database?
Answer: Just another day while I was doing SQL Server Performance Tuning for a leading organization, I was asked by if I have a query which brings out all the indexes on the table with its size. I love this kind of questions as this helps me understand my client’s mindset and understanding of the subject.
Here is a script which will list all the indexes in your database with its size.
SELECT OBJECT_SCHEMA_NAME(i.OBJECT_ID) AS SchemaName, OBJECT_NAME(i.OBJECT_ID) AS TableName, i.name AS IndexName, i.index_id AS IndexID, 8 * SUM(a.used_pages) AS 'Indexsize(KB)' FROM sys.indexes AS i JOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id JOIN sys.allocation_units AS a ON a.container_id = p.partition_id GROUP BY i.OBJECT_ID,i.index_id,i.name ORDER BY OBJECT_NAME(i.OBJECT_ID),i.index_id
What Next:
If you are interested in helping me with you to improve your SQL Server Performance, you can opt for my extremely popular Index Tuning and Strategy Guidance 99 minute session.
In 99 minutes of time we together fix issues with your indexes on your one instance and I teach how you can do it yourself for the rest of the instances. Together we carry out the following tasks:
- Analysis every single index on your one instance for its uselessness
- Identify indexes which are not useful anymore with your current schema and workload
- Propose new indexes which are very impactful for your system
- Check index related settings on your database and suggest optimal value
- Explain you and your team how to read execution plan
- Explain a very powerful real world demonstration to understand how to create a proper index for your system
At the end of the session I hand over every single script to you so you can do it yourself in future.
Reference: Pinal Dave (http://blog.SQLAuthority.com)
First appeared on How to Find Size of All the Indexes on the Database – Interview Question of the Week #097