You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added functionality to correctly update statistics on external table. Per MS, they cannot be ALTERED. They must be dropped and recreated. Added variable @ExternalTables. Default is to REBUILD external table stats. Any additional values will be ignored.
IF (EXISTS(SELECT*FROM @ActionsPreferred) OR @UpdateStatistics ISNOTNULL) AND (SYSDATETIME() <DATEADD(SECOND,@TimeLimit,@StartTime) OR @TimeLimit ISNULL)
1551
1559
BEGIN
1552
1560
SET @CurrentCommand ='SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'
IFEXISTS(SELECT*FROM @ActionsPreferred) OR @UpdateStatistics IN('ALL','INDEX')
@@ -1592,14 +1600,21 @@ BEGIN
1592
1600
+', '+CASEWHEN @PartitionLevel ='Y'THEN'partitions.partition_id AS PartitionID'WHEN @PartitionLevel ='N'THEN'NULL AS PartitionID'END
1593
1601
+', '+CASEWHEN @PartitionLevel ='Y'THEN'partitions.partition_number AS PartitionNumber'WHEN @PartitionLevel ='N'THEN'NULL AS PartitionNumber'END
1594
1602
+', '+CASEWHEN @PartitionLevel ='Y'THEN'IndexPartitions.partition_count AS PartitionCount'WHEN @PartitionLevel ='N'THEN'NULL AS PartitionCount'END
1595
-
+', 0 AS [Order]'
1603
+
+', '+CASEWHEN @Version >=13THEN'CASE WHEN external_tables.name IS NULL THEN 0 ELSE 1 END 'ELSE'0 'END+'as isExternalTable '
1604
+
+', 0 AS [Order]'
1596
1605
+', 0 AS Selected'
1597
1606
+', 0 AS Completed'
1598
1607
+' FROM sys.indexes indexes'
1599
1608
+' INNER JOIN sys.objects objects ON indexes.[object_id] = objects.[object_id]'
1600
1609
+' INNER JOIN sys.schemas schemas ON objects.[schema_id] = schemas.[schema_id]'
1601
1610
+' LEFT OUTER JOIN sys.tables tables ON objects.[object_id] = tables.[object_id]'
1602
1611
+' LEFT OUTER JOIN sys.stats stats ON indexes.[object_id] = stats.[object_id] AND indexes.[index_id] = stats.[stats_id]'
1612
+
1613
+
IF @Version >=13
1614
+
BEGIN
1615
+
SET @CurrentCommand +=' LEFT OUTER JOIN sys.external_tables external_tables ON objects.[object_id] = external_tables.[object_id] '
1616
+
END
1617
+
1603
1618
IF @PartitionLevel ='Y'
1604
1619
BEGIN
1605
1620
SET @CurrentCommand = @CurrentCommand +' LEFT OUTER JOIN sys.partitions partitions ON indexes.[object_id] = partitions.[object_id] AND indexes.index_id = partitions.index_id'
@@ -1643,6 +1658,7 @@ BEGIN
1643
1658
+', NULL AS PartitionID'
1644
1659
+', '+CASEWHEN @PartitionLevelStatistics =1THEN'dm_db_incremental_stats_properties.partition_number'ELSE'NULL'END+' AS PartitionNumber'
1645
1660
+', NULL AS PartitionCount'
1661
+
+', '+CASEWHEN @Version >=13THEN'CASE WHEN external_tables.name IS NULL THEN 0 ELSE 1 END 'ELSE'0 'END+'as isExternalTable '
1646
1662
+', 0 AS [Order]'
1647
1663
+', 0 AS Selected'
1648
1664
+', 0 AS Completed'
@@ -1651,6 +1667,11 @@ BEGIN
1651
1667
+' INNER JOIN sys.schemas schemas ON objects.[schema_id] = schemas.[schema_id]'
1652
1668
+' LEFT OUTER JOIN sys.tables tables ON objects.[object_id] = tables.[object_id]'
1653
1669
1670
+
IF @Version >=13
1671
+
BEGIN
1672
+
SET @CurrentCommand +=' LEFT OUTER JOIN sys.external_tables external_tables ON objects.[object_id] = external_tables.[object_id] '
1673
+
END
1674
+
1654
1675
IF @PartitionLevelStatistics =1
1655
1676
BEGIN
1656
1677
SET @CurrentCommand = @CurrentCommand +' OUTER APPLY sys.dm_db_incremental_stats_properties(stats.object_id, stats.stats_id) dm_db_incremental_stats_properties'
@@ -1661,9 +1682,16 @@ BEGIN
1661
1682
+' AND NOT EXISTS(SELECT * FROM sys.indexes indexes WHERE indexes.[object_id] = stats.[object_id] AND indexes.index_id = stats.stats_id)'
1662
1683
END
1663
1684
1664
-
SET @CurrentCommand = @CurrentCommand +') IndexesStatistics'
1685
+
SET @CurrentCommand = @CurrentCommand +') IndexesStatistics '
1686
+
SET @CurrentCommand = @CurrentCommand +'WHERE 1=1 '
SET @CurrentCommand +='DROP STATISTICS '+QUOTENAME(@CurrentSchemaName) +'.'+QUOTENAME(@CurrentObjectName) +'.'+QUOTENAME(@CurrentStatisticsName) +';'
2273
+
SET @CurrentCommand +='CREATE STATISTICS '+QUOTENAME(@CurrentStatisticsName) +' ON '+QUOTENAME(@CurrentSchemaName) +'.'+QUOTENAME(@CurrentObjectName) +'('
SET @CurrentCommand =LEFT(@CurrentCommand, LEN(@CurrentCommand)-1) +') '
2278
+
2279
+
END
2280
+
2281
+
IF @CurrentMaxDOP ISNOTNULLAND ((@Version >=12.06024AND @Version <13) OR (@Version >=13.05026AND @Version <14) OR @Version >=14.030154) AND @CurrentIsExtenalTable =0
0 commit comments