functions/Get-DbaHelpIndex.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
function Get-DbaHelpIndex
{
<#
.SYNOPSIS
Returns size, row and configuration information for indexes in databases.
 
.DESCRIPTION
This function will return detailed information on indexes (and optionally statistics) for all indexes in a database, or a given index should one be passed along.
As this uses SQL Server DMVs to access the data it will only work in 2005 and up (sorry folks still running SQL Server 2000).
For performance reasons certain statistics information will not be returned from SQL Server 2005 if an ObjectName is not provided.
 
The data includes:
 - ObjectName: the table containing the index
 - IndexType: clustered/non-clustered/columnstore and whether the index is unique/primary key
 - KeyColumns: the key columns of the index
 - IncludeColumns: any include columns in the index
 - FilterDefinition: any filter that may have been used in the index
 - DataCompression: row/page/none depending upon whether or not compression has been used
 - IndexReads: the number of reads of the index since last restart or index rebuild
 - IndexUpdates: the number of writes to the index since last restart or index rebuild
 - SizeKB: the size the index in KB
 - IndexRows: the number of the rows in the index (note filtered indexes will have fewer rows than exist in the table)
 - IndexLookups: the number of lookups that have been performed (only applicable for the heap or clustered index)
 - MostRecentlyUsed: when the index was most recently queried (default to 1900 for when never read)
 - StatsSampleRows: the number of rows queried when the statistics were built/rebuilt (not included in SQL Server 2005 unless ObjectName is specified)
 - StatsRowMods: the number of changes to the statistics since the last rebuild
 - HistogramSteps: the number of steps in the statistics histogram (not included in SQL Server 2005 unless ObjectName is specified)
 - StatsLastUpdated: when the statistics were last rebuilt (not included in SQL Server 2005 unless ObjectName is specified)
 
.PARAMETER SqlServer
SQLServer name or SMO object representing the SQL Server to connect to.
 
.PARAMETER SqlCredential
PSCredential object to connect as. If not specified, current Windows login will be used.
 
.PARAMETER Databases
The database(s) to be connected to for gathering the index information.
  
.PARAMETER ObjectName
The name of a table for which you want to obtain the index information. If the two part naming convention for an object is not used it will use the default schema for the executing user.
 If not passed it will return data on all indexes in a given database.
  
.PARAMETER IncludeStats
This includes statistics as well as indexes in the output (statistics information such as the StatsRowMods will always be returned for indexes).
  
.PARAMETER IncludeDataTypes
This will include the data type of each column that makes up a part of the index definition (key and include columns)
  
.PARAMETER FormatResults
Returns the numerical data in a more user readable format (numerical separator will depend on localization settings).
  
 
.NOTES
Original Author: Nic Cain, https://sirsql.net/
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Get-DbaHelpIndex
 
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Databases MyDB
 Returns information on all indexes on the MyDB database on the localhost.
 
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Databases MyDB,MyDB2
 Returns information on all indexes on the MyDB, MyDB2 databases
  
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Databases MyDB -ObjectName dbo.Table1
 Returns index information on the object dbo.Table1 in the database MyDB
  
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Databases MyDB -ObjectName dbo.Table1 -IncludeStats
 Returns information on the indexes and statistics for the table dbo.Table1 in the MyDB database
  
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Database MyDB -ObjectName dbo.Table1 -IncludeDataTypes
 Returns the index information for the table dbo.Table1 in the MyDB database, and includes the data types for the key and include columns
  
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Database MyDB -ObjectName dbo.Table1 -FormatResults
 Returns the index information for the table dbo.Table1 in the MyDB database, and returns the numerical data with separators to make it more readable (ie 1234 becomes 1,234)
  
.EXAMPLE
Get-DbaHelpIndex -SqlServer localhost -Database MyDB -IncludeStats -FormatResults
 Returns the index information for all index in the MyDB database, as well as statistics, and formats the numerical data to be more redable
  
#>


[CmdletBinding(SupportsShouldProcess = $false)]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlInstance")]
[object[]]$SqlServer,
[object]$SqlCredential,
[string]$ObjectName,
[switch]$IncludeStats,
[switch]$IncludeDataTypes,
[switch]$FormatResults
)
DynamicParam { if ($SqlServer) { return Get-ParamSqlDatabases -SqlServer $SqlServer -SqlCredential $SqlCredential } }

BEGIN
{

#Add the table predicate to the query
IF (!$ObjectName)
{
$TablePredicate = "DECLARE @TableName NVARCHAR(256);";
}
ELSE
{
$TablePredicate = "DECLARE @TableName NVARCHAR(256); SET @TableName = '$ObjectName';";
}


#Figure out if we are including stats in the results
IF ($IncludeStats)
{
$IncludeStatsPredicate = "";
}
ELSE
{
$IncludeStatsPredicate = "WHERE IndexType != 'STATISTICS'";
}

#Data types being returns with the results?
IF ($IncludeDataTypes)
{
$IncludeDataTypesPredicate = 'DECLARE @IncludeDataTypes BIT; SET @IncludeDataTypes = 1';
}
ELSE
{
$IncludeDataTypesPredicate = 'DECLARE @IncludeDataTypes BIT; SET @IncludeDataTypes = 0';
}

#region SizesQuery
$SizesQuery = @"
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  
  $TablePredicate
  $IncludeDataTypesPredicate
  ;
 
DECLARE @IndexUsageStats TABLE
    (
      object_id INT ,
      index_id INT ,
      user_scans BIGINT ,
      user_seeks BIGINT ,
      user_updates BIGINT ,
      user_lookups BIGINT ,
      last_user_lookup DATETIME2(0) ,
      last_user_scan DATETIME2(0) ,
      last_user_seek DATETIME2(0)
    );
 
DECLARE @StatsInfo TABLE
    (
      object_id INT ,
      stats_id INT ,
      stats_column_name NVARCHAR(128) ,
      stats_column_id INT ,
      stats_name NVARCHAR(128) ,
      stats_last_updated DATETIME2(0) ,
      stats_sampled_rows BIGINT ,
      rowmods BIGINT ,
      histogramsteps INT ,
      StatsRows BIGINT ,
   FullObjectName NVARCHAR(256)
    );
 
INSERT INTO @IndexUsageStats
        ( object_id ,
          index_id ,
          user_scans ,
          user_seeks ,
          user_updates ,
          user_lookups ,
          last_user_lookup ,
          last_user_scan ,
          last_user_seek
        )
        SELECT object_id ,
                index_id ,
                user_scans ,
                user_seeks ,
                user_updates ,
                user_lookups ,
                last_user_lookup ,
                last_user_scan ,
                last_user_seek
        FROM sys.dm_db_index_usage_stats
        WHERE database_id = DB_ID();
 
INSERT INTO @StatsInfo
        ( object_id ,
          stats_id ,
          stats_column_name ,
          stats_column_id ,
          stats_name ,
          stats_last_updated ,
          stats_sampled_rows ,
          rowmods ,
          histogramsteps ,
          StatsRows ,
    FullObjectName
        )
        SELECT s.object_id ,
                s.stats_id ,
                c.name ,
                sc.stats_column_id ,
                s.name ,
                sp.last_updated ,
                sp.rows_sampled ,
                sp.modification_counter ,
                sp.steps ,
                sp.rows ,
    QUOTENAME(sch.name) + '.' + QUOTENAME(t.name) AS FullObjectName
        FROM [sys].[stats] AS [s]
                INNER JOIN sys.stats_columns sc ON s.stats_id = sc.stats_id
                                                   AND s.object_id = sc.object_id
                INNER JOIN sys.columns c ON c.object_id = sc.object_id
                                            AND c.column_id = sc.column_id
                INNER JOIN sys.tables t ON c.object_id = t.object_id
    INNER JOIN sys.schemas sch ON sch.schema_id = t.schema_id
                OUTER APPLY sys.dm_db_stats_properties([s].[object_id],
                                                       [s].[stats_id]) AS [sp]
        WHERE s.object_id = CASE WHEN @TableName IS NULL THEN s.object_id
                                   ELSE OBJECT_ID(@TableName)
                              END;
 
 
;
WITH cteStatsInfo
          AS ( SELECT object_id ,
                        si.stats_id ,
                        si.stats_name ,
                        STUFF((SELECT N', ' + stats_column_name
                               FROM @StatsInfo si2
                               WHERE si2.object_id = si.object_id
                                        AND si2.stats_id = si.stats_id
                               ORDER BY si2.stats_column_id
                        FOR XML PATH(N'') ,
                                  TYPE).value(N'.[1]', N'nvarchar(1000)'), 1,
                              2, N'') AS StatsColumns ,
                        MAX(si.stats_sampled_rows) AS SampleRows ,
                        MAX(si.rowmods) AS RowMods ,
                        MAX(si.histogramsteps) AS HistogramSteps ,
                        MAX(si.stats_last_updated) AS StatsLastUpdated ,
                        MAX(si.StatsRows) AS StatsRows,
      FullObjectName
               FROM @StatsInfo si
               GROUP BY si.object_id ,
                        si.stats_id ,
                        si.stats_name ,
      si.FullObjectName
             ),
        cteIndexSizes
          AS ( SELECT object_id ,
                        index_id ,
                        CASE WHEN index_id < 2
                             THEN ( ( SUM(in_row_data_page_count
                                          + lob_used_page_count
                                          + row_overflow_used_page_count)
                                      * 8192 ) / 1024 )
                             ELSE ( ( SUM(used_page_count) * 8192 ) / 1024 )
                        END AS SizeKB
               FROM sys.dm_db_partition_stats
               GROUP BY object_id ,
                        index_id
             ),
        cteRows
          AS ( SELECT object_id ,
                        index_id ,
                        SUM(rows) AS IndexRows
               FROM sys.partitions
               GROUP BY object_id ,
                        index_id
             ),
        cteIndex
          AS ( SELECT OBJECT_NAME(c.object_id) AS ObjectName ,
                        c.object_id ,
                        c.index_id ,
                        i.name COLLATE SQL_Latin1_General_CP1_CI_AS AS name ,
                        c.index_column_id ,
                        c.column_id ,
                        c.is_included_column ,
                        CASE WHEN @IncludeDataTypes = 0
                                  AND c.is_descending_key = 1
                             THEN sc.name + ' DESC'
                             WHEN @IncludeDataTypes = 0
                                  AND c.is_descending_key = 0 THEN sc.name
                             WHEN @IncludeDataTypes = 1
                                  AND c.is_descending_key = 1
                                  AND c.is_included_column = 0
                             THEN sc.name + ' DESC (' + t.name + ') '
                             WHEN @IncludeDataTypes = 1
                                  AND c.is_descending_key = 0
                                  AND c.is_included_column = 0
                             THEN sc.name + ' (' + t.name + ')'
                             ELSE sc.name
                        END AS ColumnName ,
                        i.filter_definition ,
                        ISNULL(dd.user_scans, 0) AS user_scans ,
                        ISNULL(dd.user_seeks, 0) AS user_seeks ,
                        ISNULL(dd.user_updates, 0) AS user_updates ,
                        ISNULL(dd.user_lookups, 0) AS user_lookups ,
                        CONVERT(DATETIME2(0), ISNULL(dd.last_user_lookup,
                                                     '1901-01-01')) AS LastLookup ,
                        CONVERT(DATETIME2(0), ISNULL(dd.last_user_scan,
                                                     '1901-01-01')) AS LastScan ,
                        CONVERT(DATETIME2(0), ISNULL(dd.last_user_seek,
                                                     '1901-01-01')) AS LastSeek ,
                        i.fill_factor ,
                        c.is_descending_key ,
                        p.data_compression_desc ,
                        i.type_desc ,
                        i.is_unique ,
                        i.is_unique_constraint ,
                        i.is_primary_key ,
                        ci.SizeKB ,
                        cr.IndexRows ,
      QUOTENAME(sch.name) + '.' + QUOTENAME(tbl.name) AS FullObjectName
               FROM sys.indexes i
                        JOIN sys.index_columns c ON i.object_id = c.object_id
                                                    AND i.index_id = c.index_id
                        JOIN sys.columns sc ON c.object_id = sc.object_id
                                               AND c.column_id = sc.column_id
      INNER JOIN sys.tables tbl ON c.object_id = tbl.object_id
      INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id
                        LEFT JOIN sys.types t ON sc.user_type_id = t.user_type_id
                        LEFT JOIN @IndexUsageStats dd ON i.object_id = dd.object_id
                                                         AND i.index_id = dd.index_id --and dd.database_id = db_id()
                        JOIN sys.partitions p ON i.object_id = p.object_id
                                                 AND i.index_id = p.index_id
                        JOIN cteIndexSizes ci ON i.object_id = ci.object_id
                                                 AND i.index_id = ci.index_id
                        JOIN cteRows cr ON i.object_id = cr.object_id
                                           AND i.index_id = cr.index_id
               WHERE i.object_id = CASE WHEN @TableName IS NULL
                                           THEN i.object_id
                                           ELSE OBJECT_ID(@TableName)
                                      END
             ),
        cteResults
          AS ( SELECT ci.FullObjectName ,
                        ci.object_id ,
                        MAX(index_id) AS Index_Id ,
                        ci.type_desc
                        + CASE WHEN ci.is_primary_key = 1
                               THEN ' (PRIMARY KEY)'
                               WHEN ci.is_unique_constraint = 1
                               THEN ' (UNIQUE CONSTRAINT)'
                               WHEN ci.is_unique = 1 THEN ' (UNIQUE)'
                               ELSE ''
                          END AS IndexType ,
                        name AS IndexName ,
                        STUFF((SELECT N', ' + ColumnName
                               FROM cteIndex ci2
                               WHERE ci2.name = ci.name
                                        AND ci2.is_included_column = 0
                               GROUP BY ci2.index_column_id ,
                                        ci2.ColumnName
                               ORDER BY ci2.index_column_id
                        FOR XML PATH(N'') ,
                                  TYPE).value(N'.[1]', N'nvarchar(1000)'), 1,
                              2, N'') AS KeyColumns ,
                        ISNULL(STUFF((SELECT N', ' + ColumnName
                                      FROM cteIndex ci3
                                      WHERE ci3.name = ci.name
                                                AND ci3.is_included_column = 1
                                      GROUP BY ci3.index_column_id ,
                                                ci3.ColumnName
                                      ORDER BY ci3.index_column_id
                               FOR XML PATH(N'') ,
                                         TYPE).value(N'.[1]',
                                                     N'nvarchar(1000)'), 1, 2,
                                     N''), '') AS IncludeColumns ,
                        ISNULL(filter_definition, '') AS FilterDefinition ,
                        ci.fill_factor ,
                        CASE WHEN ci.data_compression_desc = 'NONE' THEN ''
                             ELSE ci.data_compression_desc
                        END AS DataCompression ,
                        MAX(ci.user_seeks) + MAX(ci.user_scans)
                        + MAX(ci.user_lookups) AS IndexReads ,
                        MAX(ci.user_lookups) AS IndexLookups ,
                        ci.user_updates AS IndexUpdates ,
                        ci.SizeKB AS SizeKB ,
                        ci.IndexRows AS IndexRows ,
                        CASE WHEN LastScan > LastSeek
                                  AND LastScan > LastLookup THEN LastScan
                             WHEN LastSeek > LastScan
                                  AND LastSeek > LastLookup THEN LastSeek
                             WHEN LastLookup > LastScan
                                  AND LastLookup > LastSeek THEN LastLookup
                             ELSE ''
                        END AS MostRecentlyUsed
               FROM cteIndex ci
               GROUP BY ci.ObjectName ,
                        ci.name ,
                        ci.filter_definition ,
                        ci.object_id ,
                        ci.LastLookup ,
                        ci.LastSeek ,
                        ci.LastScan ,
                        ci.user_updates ,
                        ci.fill_factor ,
                        ci.data_compression_desc ,
                        ci.type_desc ,
                        ci.is_primary_key ,
                        ci.is_unique ,
                        ci.is_unique_constraint ,
                        ci.SizeKB ,
                        ci.IndexRows ,
      ci.FullObjectName
             ),
        AllResults
          AS ( SELECT c.FullObjectName ,
                        ISNULL(IndexType, 'STATISTICS') AS IndexType ,
                        ISNULL(IndexName, si.stats_name) AS IndexName ,
                        ISNULL(KeyColumns, si.StatsColumns) AS KeyColumns ,
                        ISNULL(IncludeColumns, '') AS IncludeColumns ,
                        FilterDefinition ,
                        fill_factor AS [FillFactor] ,
                        DataCompression ,
                        IndexReads ,
                        IndexUpdates ,
                        SizeKB ,
                        IndexRows ,
                        IndexLookups ,
                        MostRecentlyUsed ,
                        SampleRows AS StatsSampleRows ,
                        RowMods AS StatsRowMods ,
                        si.HistogramSteps ,
                        si.StatsLastUpdated ,
                        1 AS Ordering
               FROM cteResults c
                        INNER JOIN cteStatsInfo si ON si.object_id = c.object_id
                                                      AND si.stats_id = c.Index_Id
               UNION
               SELECT QUOTENAME(sch.name) + '.' + QUOTENAME(tbl.name) AS FullObjectName ,
                        'STATISTICS' ,
                        stats_name ,
                        StatsColumns ,
                        '' ,
                        '' AS FilterDefinition ,
                        '' AS Fill_Factor ,
                        '' AS DataCompression ,
                        '' AS IndexReads ,
                        '' AS IndexUpdates ,
                        '' AS SizeKB ,
                        StatsRows AS IndexRows ,
                        '' AS IndexLookups ,
                        '' AS MostRecentlyUsed ,
                        SampleRows AS StatsSampleRows ,
                        RowMods AS StatsRowMods ,
                        csi.HistogramSteps ,
                        csi.StatsLastUpdated ,
                        2
               FROM cteStatsInfo csi
      INNER JOIN sys.tables tbl ON csi.object_id = tbl.object_id
      INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id
               WHERE stats_id NOT IN (
                        SELECT stats_id
                        FROM cteResults c
                                INNER JOIN cteStatsInfo si ON si.object_id = c.object_id
                                                              AND si.stats_id = c.Index_Id )
             )
    SELECT FullObjectName ,
            ISNULL(IndexType, 'STATISTICS') AS IndexType ,
            IndexName ,
            KeyColumns ,
            ISNULL(IncludeColumns, '') AS IncludeColumns ,
            FilterDefinition ,
            [FillFactor] AS [FillFactor] ,
            DataCompression ,
            IndexReads ,
            IndexUpdates ,
            SizeKB ,
            IndexRows ,
            IndexLookups ,
            MostRecentlyUsed ,
            StatsSampleRows ,
            StatsRowMods ,
            HistogramSteps ,
            StatsLastUpdated
    FROM AllResults
   $IncludeStatsPredicate
OPTION ( RECOMPILE );
"@

#endRegion SizesQuery


#region sizesQuery2005
$SizesQuery2005 = @"
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  
  $TablePredicate
  $IncludeDataTypesPredicate
  ;
 
DECLARE @AllResults TABLE
 (
  RowNum INT ,
  FullObjectName NVARCHAR(300) ,
  IndexType NVARCHAR(256) ,
  IndexName NVARCHAR(256) ,
  KeyColumns NVARCHAR(2000) ,
  IncludeColumns NVARCHAR(2000) ,
  FilterDefinition NVARCHAR(100) ,
  [FillFactor] TINYINT ,
  DataCompression CHAR(4) ,
  IndexReads BIGINT ,
  IndexUpdates BIGINT ,
  SizeKB BIGINT ,
  IndexRows BIGINT ,
  IndexLookups BIGINT ,
  MostRecentlyUsed DATETIME ,
  StatsSampleRows BIGINT ,
  StatsRowMods BIGINT ,
  HistogramSteps INT ,
  StatsLastUpdated DATETIME ,
  object_id BIGINT ,
  index_id BIGINT
 );
 
DECLARE @IndexUsageStats TABLE
    (
      object_id INT ,
      index_id INT ,
      user_scans BIGINT ,
      user_seeks BIGINT ,
      user_updates BIGINT ,
      user_lookups BIGINT ,
      last_user_lookup DATETIME ,
      last_user_scan DATETIME ,
      last_user_seek DATETIME
    );
 
DECLARE @StatsInfo TABLE
    (
      object_id INT ,
      stats_id INT ,
      stats_column_name NVARCHAR(128) ,
      stats_column_id INT ,
      stats_name NVARCHAR(128) ,
      stats_last_updated DATETIME ,
      stats_sampled_rows BIGINT ,
      rowmods BIGINT ,
      histogramsteps INT ,
      StatsRows BIGINT ,
   FullObjectName NVARCHAR(256)
    );
 
INSERT INTO @IndexUsageStats
        ( object_id ,
          index_id ,
          user_scans ,
          user_seeks ,
          user_updates ,
          user_lookups ,
          last_user_lookup ,
          last_user_scan ,
          last_user_seek
        )
        SELECT object_id ,
                index_id ,
                user_scans ,
                user_seeks ,
                user_updates ,
                user_lookups ,
                last_user_lookup ,
                last_user_scan ,
                last_user_seek
        FROM sys.dm_db_index_usage_stats
        WHERE database_id = DB_ID();
 
 
INSERT INTO @StatsInfo
        ( object_id ,
          stats_id ,
          stats_column_name ,
          stats_column_id ,
          stats_name ,
          stats_last_updated ,
          stats_sampled_rows ,
          rowmods ,
          histogramsteps ,
          StatsRows ,
    FullObjectName
        )
        SELECT s.object_id ,
                s.stats_id ,
                c.name ,
                sc.stats_column_id ,
                s.name ,
                NULL AS last_updated ,
                NULL AS rows_sampled ,
                NULL AS modification_counter ,
                NULL AS steps ,
                NULL AS rows ,
    QUOTENAME(sch.name) + '.' + QUOTENAME(t.name) AS FullObjectName
        FROM [sys].[stats] AS [s]
                INNER JOIN sys.stats_columns sc ON s.stats_id = sc.stats_id
                                                   AND s.object_id = sc.object_id
                INNER JOIN sys.columns c ON c.object_id = sc.object_id
                                            AND c.column_id = sc.column_id
                INNER JOIN sys.tables t ON c.object_id = t.object_id
    INNER JOIN sys.schemas sch ON sch.schema_id = t.schema_id
             -- OUTER APPLY sys.dm_db_stats_properties([s].[object_id],
               -- [s].[stats_id]) AS [sp]
        WHERE s.object_id = CASE WHEN @TableName IS NULL THEN s.object_id
                                   ELSE OBJECT_ID(@TableName)
                              END;
 
 
;
WITH cteStatsInfo
          AS ( SELECT object_id ,
                        si.stats_id ,
                        si.stats_name ,
                        STUFF((SELECT N', ' + stats_column_name
                               FROM @StatsInfo si2
                               WHERE si2.object_id = si.object_id
                                        AND si2.stats_id = si.stats_id
                               ORDER BY si2.stats_column_id
                        FOR XML PATH(N'') ,
                                  TYPE).value(N'.[1]', N'nvarchar(1000)'), 1,
                              2, N'') AS StatsColumns ,
                        MAX(si.stats_sampled_rows) AS SampleRows ,
                        MAX(si.rowmods) AS RowMods ,
                        MAX(si.histogramsteps) AS HistogramSteps ,
                        MAX(si.stats_last_updated) AS StatsLastUpdated ,
                        MAX(si.StatsRows) AS StatsRows,
      FullObjectName
               FROM @StatsInfo si
               GROUP BY si.object_id ,
                        si.stats_id ,
                        si.stats_name ,
      si.FullObjectName
             ),
        cteIndexSizes
          AS ( SELECT object_id ,
                        index_id ,
                        CASE WHEN index_id < 2
                             THEN ( ( SUM(in_row_data_page_count
                                          + lob_used_page_count
                                          + row_overflow_used_page_count)
                                      * 8192 ) / 1024 )
                             ELSE ( ( SUM(used_page_count) * 8192 ) / 1024 )
                        END AS SizeKB
               FROM sys.dm_db_partition_stats
               GROUP BY object_id ,
                        index_id
             ),
        cteRows
          AS ( SELECT object_id ,
                        index_id ,
                        SUM(rows) AS IndexRows
               FROM sys.partitions
               GROUP BY object_id ,
                        index_id
             ),
        cteIndex
          AS ( SELECT OBJECT_NAME(c.object_id) AS ObjectName ,
                        c.object_id ,
                        c.index_id ,
                        i.name COLLATE SQL_Latin1_General_CP1_CI_AS AS name ,
                        c.index_column_id ,
                        c.column_id ,
                        c.is_included_column ,
                        CASE WHEN @IncludeDataTypes = 0
                                  AND c.is_descending_key = 1
                             THEN sc.name + ' DESC'
                             WHEN @IncludeDataTypes = 0
                                  AND c.is_descending_key = 0 THEN sc.name
                             WHEN @IncludeDataTypes = 1
                                  AND c.is_descending_key = 1
                                  AND c.is_included_column = 0
                             THEN sc.name + ' DESC (' + t.name + ') '
                             WHEN @IncludeDataTypes = 1
                                  AND c.is_descending_key = 0
                                  AND c.is_included_column = 0
                             THEN sc.name + ' (' + t.name + ')'
                             ELSE sc.name
                        END AS ColumnName ,
                        '' AS filter_definition ,
                        ISNULL(dd.user_scans, 0) AS user_scans ,
                        ISNULL(dd.user_seeks, 0) AS user_seeks ,
                        ISNULL(dd.user_updates, 0) AS user_updates ,
                        ISNULL(dd.user_lookups, 0) AS user_lookups ,
                        CONVERT(DATETIME, ISNULL(dd.last_user_lookup,
                                                     '1901-01-01')) AS LastLookup ,
                        CONVERT(DATETIME, ISNULL(dd.last_user_scan,
                                                     '1901-01-01')) AS LastScan ,
                        CONVERT(DATETIME, ISNULL(dd.last_user_seek,
                                                     '1901-01-01')) AS LastSeek ,
                        i.fill_factor ,
                        c.is_descending_key ,
                        'NONE' as data_compression_desc ,
                        i.type_desc ,
                        i.is_unique ,
                        i.is_unique_constraint ,
                        i.is_primary_key ,
                        ci.SizeKB ,
                        cr.IndexRows ,
      QUOTENAME(sch.name) + '.' + QUOTENAME(tbl.name) AS FullObjectName
               FROM sys.indexes i
                        JOIN sys.index_columns c ON i.object_id = c.object_id
                                                    AND i.index_id = c.index_id
                        JOIN sys.columns sc ON c.object_id = sc.object_id
                                               AND c.column_id = sc.column_id
      INNER JOIN sys.tables tbl ON c.object_id = tbl.object_id
      INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id
                        LEFT JOIN sys.types t ON sc.user_type_id = t.user_type_id
                        LEFT JOIN @IndexUsageStats dd ON i.object_id = dd.object_id
                                                         AND i.index_id = dd.index_id --and dd.database_id = db_id()
                        JOIN sys.partitions p ON i.object_id = p.object_id
                                                 AND i.index_id = p.index_id
                        JOIN cteIndexSizes ci ON i.object_id = ci.object_id
                                                 AND i.index_id = ci.index_id
                        JOIN cteRows cr ON i.object_id = cr.object_id
                                           AND i.index_id = cr.index_id
               WHERE i.object_id = CASE WHEN @TableName IS NULL
                                           THEN i.object_id
                                           ELSE OBJECT_ID(@TableName)
                                      END
             ),
        cteResults
          AS ( SELECT ci.FullObjectName ,
                        ci.object_id ,
                        MAX(index_id) AS Index_Id ,
                        ci.type_desc
                        + CASE WHEN ci.is_primary_key = 1
                               THEN ' (PRIMARY KEY)'
                               WHEN ci.is_unique_constraint = 1
                               THEN ' (UNIQUE CONSTRAINT)'
                               WHEN ci.is_unique = 1 THEN ' (UNIQUE)'
                               ELSE ''
                          END AS IndexType ,
                        name AS IndexName ,
                        STUFF((SELECT N', ' + ColumnName
                               FROM cteIndex ci2
                               WHERE ci2.name = ci.name
                                        AND ci2.is_included_column = 0
                               GROUP BY ci2.index_column_id ,
                                        ci2.ColumnName
                               ORDER BY ci2.index_column_id
                        FOR XML PATH(N'') ,
                                  TYPE).value(N'.[1]', N'nvarchar(1000)'), 1,
                              2, N'') AS KeyColumns ,
                        ISNULL(STUFF((SELECT N', ' + ColumnName
                                      FROM cteIndex ci3
                                      WHERE ci3.name = ci.name
                                                AND ci3.is_included_column = 1
                                      GROUP BY ci3.index_column_id ,
                                                ci3.ColumnName
                                      ORDER BY ci3.index_column_id
                               FOR XML PATH(N'') ,
                                         TYPE).value(N'.[1]',
                                                     N'nvarchar(1000)'), 1, 2,
                                     N''), '') AS IncludeColumns ,
                        ISNULL(filter_definition, '') AS FilterDefinition ,
                        ci.fill_factor ,
                        CASE WHEN ci.data_compression_desc = 'NONE' THEN ''
                             ELSE ci.data_compression_desc
                        END AS DataCompression ,
                        MAX(ci.user_seeks) + MAX(ci.user_scans)
                        + MAX(ci.user_lookups) AS IndexReads ,
                        MAX(ci.user_lookups) AS IndexLookups ,
                        ci.user_updates AS IndexUpdates ,
                        ci.SizeKB AS SizeKB ,
                        ci.IndexRows AS IndexRows ,
                        CASE WHEN LastScan > LastSeek
                                  AND LastScan > LastLookup THEN LastScan
                             WHEN LastSeek > LastScan
                                  AND LastSeek > LastLookup THEN LastSeek
                             WHEN LastLookup > LastScan
                                  AND LastLookup > LastSeek THEN LastLookup
                             ELSE ''
                        END AS MostRecentlyUsed
               FROM cteIndex ci
               GROUP BY ci.ObjectName ,
                        ci.name ,
                        ci.filter_definition ,
                        ci.object_id ,
                        ci.LastLookup ,
                        ci.LastSeek ,
                        ci.LastScan ,
                        ci.user_updates ,
                        ci.fill_factor ,
                        ci.data_compression_desc ,
                        ci.type_desc ,
                        ci.is_primary_key ,
                        ci.is_unique ,
                        ci.is_unique_constraint ,
                        ci.SizeKB ,
                        ci.IndexRows ,
      ci.FullObjectName
             ), AllResults AS
    ( SELECT c.FullObjectName ,
                        ISNULL(IndexType, 'STATISTICS') AS IndexType ,
                        ISNULL(IndexName, '') AS IndexName ,
                        ISNULL(KeyColumns, '') AS KeyColumns ,
                        ISNULL(IncludeColumns, '') AS IncludeColumns ,
                        FilterDefinition ,
                        fill_factor AS [FillFactor] ,
                        DataCompression ,
                        IndexReads ,
                        IndexUpdates ,
                        SizeKB ,
                        IndexRows ,
                        IndexLookups ,
                        MostRecentlyUsed ,
                        NULL AS StatsSampleRows ,
                        NULL AS StatsRowMods ,
                        NULL AS HistogramSteps ,
                        NULL AS StatsLastUpdated ,
                        1 AS Ordering ,
      c.object_id ,
      c.Index_Id
               FROM cteResults c
                        INNER JOIN cteStatsInfo si ON si.object_id = c.object_id
                                                      AND si.stats_id = c.Index_Id
    UNION
               SELECT QUOTENAME(sch.name) + '.' + QUOTENAME(tbl.name) AS FullObjectName ,
                        'STATISTICS' ,
                        stats_name ,
                        StatsColumns ,
                        '' ,
                        '' AS FilterDefinition ,
                        '' AS Fill_Factor ,
                        '' AS DataCompression ,
                        '' AS IndexReads ,
                        '' AS IndexUpdates ,
                        '' AS SizeKB ,
                        StatsRows AS IndexRows ,
                        '' AS IndexLookups ,
                        '' AS MostRecentlyUsed ,
                        SampleRows AS StatsSampleRows ,
                        RowMods AS StatsRowMods ,
                        csi.HistogramSteps ,
                        csi.StatsLastUpdated ,
                        2 ,
      csi.object_id ,
      csi.stats_id
               FROM cteStatsInfo csi
      INNER JOIN sys.tables tbl ON csi.object_id = tbl.object_id
      INNER JOIN sys.schemas sch ON sch.schema_id = tbl.schema_id
      LEFT JOIN (SELECT si.object_id, si.stats_id
         FROM cteResults c
         INNER JOIN cteStatsInfo si ON si.object_id = c.object_id
                                                              AND si.stats_id = c.Index_Id ) AS x on csi.object_id = x.object_id and csi.stats_id = x.stats_id
    WHERE x.object_id is null
             )
    INSERT INTO @AllResults
 SELECT row_number() OVER (ORDER BY FullObjectName) AS RowNum ,
   FullObjectName ,
            ISNULL(IndexType, 'STATISTICS') AS IndexType ,
            IndexName ,
            KeyColumns ,
            ISNULL(IncludeColumns, '') AS IncludeColumns ,
            FilterDefinition ,
            [FillFactor] AS [FillFactor] ,
            DataCompression ,
            IndexReads ,
            IndexUpdates ,
            SizeKB ,
            IndexRows ,
            IndexLookups ,
            MostRecentlyUsed ,
            StatsSampleRows ,
            StatsRowMods ,
            HistogramSteps ,
            StatsLastUpdated ,
   object_id ,
   index_id
    FROM AllResults
   $IncludeStatsPredicate
OPTION ( RECOMPILE );
 
/* Only update the stats data on 2005 for a single table, otherwise the run time for this is a potential problem for large table/index volumes */
IF @TableName IS NOT NULL
BEGIN
 
 DECLARE @StatsInfo2005 TABLE (Name nvarchar(128), Updated DATETIME, Rows BIGINT, RowsSampled BIGINT, Steps INT, Density INT, AverageKeyLength INT, StringIndex NVARCHAR(20))
 
 DECLARE @SqlCall NVARCHAR(2000), @RowNum INT;
 SELECT @RowNum = min(RowNum) FROM @AllResults;
 WHILE @RowNum IS NOT NULL
 BEGIN
  SELECT @SqlCall = 'dbcc show_statistics("'+FullObjectName+'", "'+IndexName+'") with stat_header' FROM @AllResults WHERE RowNum = @RowNum;
  INSERT INTO @StatsInfo2005 exec (@SqlCall);
  UPDATE @AllResults
   SET StatsSampleRows = RowsSampled,
   HistogramSteps = Steps,
   StatsLastUpdated = Updated
   FROM @StatsInfo2005
   WHERE RowNum = @RowNum;
  DELETE FROM @StatsInfo2005
  SELECT @RowNum = min(RowNum) FROM @AllResults WHERE RowNum > @RowNum;
 END;
 
END;
 
UPDATE a
SET a.StatsRowMods = i.rowmodctr
FROM @AllResults a
 JOIN sys.sysindexes i ON a.object_id = i.id AND a.index_id = i.indid;
 
SELECT FullObjectName ,
  IndexType ,
  IndexName ,
  KeyColumns ,
  IncludeColumns ,
  FilterDefinition ,
  [FillFactor] ,
  DataCompression ,
  IndexReads ,
  IndexUpdates ,
  SizeKB ,
  IndexRows ,
  IndexLookups ,
  MostRecentlyUsed ,
  StatsSampleRows ,
  StatsRowMods ,
  HistogramSteps ,
  StatsLastUpdated
FROM @AllResults;
 
"@


#endregion sizesQuery2005
$server = Connect-DbaSqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
}

PROCESS
{

        #Need to check the version of SQL
        if ($server.versionMajor -ge 10)
        {
            $indexesQuery = $SizesQuery
        }

        elseif ($server.Information.Version.Major -eq 9)
        {
            $indexesQuery = $SizesQuery2005
        }

        else 
        {
Write-Warning "This function does not support versions lower than SQL Server 2005 (v9)"
continue
        }

# Convert from RuntimeDefinedParameter object to regular array
$databases = $psboundparameters.Databases

if ($pipedatabase.Length -gt 0)
{
$Source = $pipedatabase[0].parent.name
$databases = $pipedatabase.name
}

if ($databases.Count -eq 0)
{
$databases = ($server.Databases | Where-Object { $_.Status -ne "Offline" }).Name
}

if ($databases.Count -gt 0)
{
foreach ($db in $databases)
{
try
{
                    $IndexDetails = ($server.Databases[$db].ExecuteWithResults($indexesQuery)).Tables[0];

if ($FormatResults)
{
foreach ($detail in $IndexDetails)
{
$recentlyused = [datetime]$detail.MostRecentlyUsed

if ($recentlyused.year -eq 1900)
{
$recentlyused = $null
}

[pscustomobject]@{
DatabaseName = $db
ObjectName = $detail.FullObjectName
IndexType = $detail.IndexType
KeyColumns = $detail.KeyColumns
IncludeColumns = $detail.IncludeColumns
FilterDefinition = $detail.FilterDefinition
DataCompression = $detail.DataCompression
IndexReads = "{0:N0}" -f $detail.IndexReads
IndexUpdates = "{0:N0}" -f $detail.IndexUpdates
SizeKB = "{0:N0}" -f $detail.SizeKB
IndexRows = "{0:N0}" -f $detail.IndexRows
IndexLookups = "{0:N0}" -f $detail.IndexLookups
MostRecentlyUsed = $recentlyused
StatsSampleRows = "{0:N0}" -f $detail.StatsSampleRows
StatsRowMods = "{0:N0}" -f $detail.StatsRowMods
HistogramSteps = $detail.HistogramSteps
StatsLastUpdated = $detail.StatsLastUpdated
}
}
}

else
{
foreach ($detail in $IndexDetails)
{
$recentlyused = [datetime]$detail.MostRecentlyUsed

if ($recentlyused.year -eq 1900)
{
$recentlyused = $null
}

[pscustomobject]@{
DatabaseName = $db
ObjectName = $detail.FullObjectName
IndexType = $detail.IndexType
KeyColumns = $detail.KeyColumns
IncludeColumns = $detail.IncludeColumns
FilterDefinition = $detail.FilterDefinition
DataCompression = $detail.DataCompression
IndexReads = $detail.IndexReads
IndexUpdates = $detail.IndexUpdates
SizeKB = $detail.SizeKB
IndexRows = $detail.IndexRows
IndexLookups = $detail.IndexLookups
MostRecentlyUsed = $recentlyused
StatsSampleRows = $detail.StatsSampleRows
StatsRowMods = $detail.StatsRowMods
HistogramSteps = $detail.HistogramSteps
StatsLastUpdated = $detail.StatsLastUpdated
}
}
}


}
catch
{
Write-Warning "Cannot process $db on $server"
}
}

}
}

}