Quantcast
Channel: SQL PraRup – SQL Server Mentalist
Viewing all articles
Browse latest Browse all 121

BI SQL # 150 : SQL Server DBA Scripts : Detailed Table and Index Breakdown information

$
0
0

Hi Geeks,

In this article we are going to cover Detailed Table and Index Breakdown information.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Output Column
  • SQL Script Code
  • SQL Script Output Screenshot
  • User Level to execute

Problem Statement of SQL Script:

Get Detailed Table and Index Breakdown information.

Description of SQL Script:

This script will give you a comprehensive breakdown of your tables and indexes, which is shown with schema(s),table(s),index type(s),index size(s),row count(s) as well as the filegroup(s) and partition(s) information. Also in this script I have included two columns called TippingPoint* which relate to the rough boundaries where a nonclustered index is no longer selective enough. I have applied the formula to the clustered index for ease.

For more detail on an indexes tipping point please see Kimberly Tripp’s article.

http://www.sqlskills.com/BLOGS/KIMBERLY/category/The-Tipping-Point.aspx

SQL Script Output Column

image

SQL Script Code

SELECT DB_NAME() [DatabaseName]
    ,ao.[object_id] [ObjectID]
    ,SCHEMA_NAME(ao.[schema_id]) [SchemaName]
    ,ao.[name] [ObjectName]
    ,ao.[is_ms_shipped] [IsSystemObject]
    ,i.[index_id] [IndexID]
    ,i.[name] [IndexName]
    ,i.[type_desc] [IndexType]
    ,au.[type_desc] [AllocationUnitType]
    ,p.[partition_number] [PartitionNumber]
    ,ds.[type] [IsPartition]
    --,p.[data_compression_desc] [Compression] 
    ,ds.[name] [PartitionName]
    ,fg.[name] [FileGroupName]
    ,p.[rows] [NumberOfRows]
    ,CASE 
        WHEN pf.[boundary_value_on_right] = 1
            AND ds.[type] = 'PS'
            THEN 'RIGHT'
        WHEN pf.[boundary_value_on_right] IS NULL
            AND ds.[type] = 'PS'
            THEN 'LEFT'
        ELSE NULL
        END [Range]
    ,prv.[value] [LowerBoundaryValue]
    ,prv2.[value] [UpperBoundaryValue]
    ,CONVERT(DECIMAL(15, 3), (
            CASE 
                WHEN au.[type_desc] = 'IN_ROW_DATA'
                    AND p.[rows] > 0
                    THEN p.[rows] / au.[data_pages]
                ELSE 0
                END
            )) [RowsPerPage]
    ,(
        CASE 
            WHEN au.[type_desc] = 'IN_ROW_DATA'
                AND i.[type_desc] = 'CLUSTERED'
                THEN au.[used_pages] * 0.20
            ELSE NULL
            END
        ) [TippingPointLower_Rows]
    ,(
        CASE 
            WHEN au.[type_desc] = 'IN_ROW_DATA'
                AND i.[type_desc] = 'CLUSTERED'
                THEN au.[used_pages] * 0.30
            ELSE NULL
            END
        ) [TippingPointUpper_Rows]
    ,au.[used_pages] [UsedPages]
    ,CONVERT(DECIMAL(15, 3), (
            CASE 
                WHEN au.[type] <> 1
                    THEN au.[used_pages]
                WHEN p.[index_id] < 2
                    THEN au.[data_pages]
                ELSE 0
                END
            ) * CONVERT(FLOAT, 8) / 1024) [DataUsedSpace_MiB]
    ,CONVERT(DECIMAL(15, 3), (
            au.[used_pages] - (
                CASE 
                    WHEN au.[type] <> 1
                        THEN au.[used_pages]
                    WHEN p.[index_id] < 2
                        THEN au.[data_pages]
                    ELSE 0
                    END
                )
            ) * CONVERT(FLOAT, 8) / 1024) [IndexUsedSpace_MiB]
    ,au.[data_pages] [DataPages]
FROM sys.partition_functions pf
INNER JOIN sys.partition_schemes ps ON pf.[function_id] = ps.
    [function_id]
RIGHT OUTER JOIN sys.partitions p
INNER JOIN sys.indexes i ON p.[object_id] = i.[object_id]
    AND p.[index_id] = i.[index_id]
INNER JOIN sys.allocation_units au ON au.[container_id] = p.
    [partition_id]
INNER JOIN sys.filegroups fg ON au.[data_space_id] = fg.
    [data_space_id]
INNER JOIN sys.data_spaces ds ON i.[data_space_id] = ds.
    [data_space_id]
INNER JOIN sys.all_objects ao ON i.[object_id] = ao.[object_id] ON 
    ps.[data_space_id] = ds.[data_space_id] LEFT OUTER JOIN sys.
    partition_range_values prv ON ps.[function_id] = prv.
    [function_id]
    AND p.[partition_number] - 1 = prv.[boundary_id] 
    LEFT OUTER JOIN sys.partition_range_values prv2 ON ps.
    [function_id] = prv2.[function_id]
    AND prv2.[boundary_id] = p.[partition_number] WHERE ao.
    [is_ms_shipped] = 0
    AND au.[type_desc] = 'IN_ROW_DATA'
--AND SCHEMA_NAME(ao.[schema_id]) ='dbo' 
--AND ao.[name] LIKE '%%' 
ORDER BY SCHEMA_NAME(ao.[schema_id])
    ,ao.[name]

SQL Script Output Screenshot

image

User Level to execute

200

    Hope you will like Detailed Table and Index Breakdown information.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |


Filed under: Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar Tagged: DBA, Fast SQL, Microsoft SQL Server, SQL Help, SQL Mentalist, SQL Script, SQL Server, SQL Server 2008 R2, SQL Server 2012, SQL tips, SQL Tricks, TSQL, Vishal Pawar

Viewing all articles
Browse latest Browse all 121

Trending Articles