Index Tuning Report – VMEnt\SQL25 / ForumDemo – 2026-11-13

Tuning goal: Index Tuning

Server: VMEnt\SQL25 · SQL Server 2025 (17.x), Enterprise Developer Edition, RTM 17.0.1000.7 · Database: ForumDemo

Executive summary

Overall posture: The workload has several useful existing indexes and no duplicate or dependency-blocked indexes. The highest-confidence opportunity is to add ordered composite indexes for three repeatedly executed queries. The evidence window is only 2.4 days, so low-use index removals are not recommended outright.
  1. Priority 1 — Add a composite Posts index for the 16,000-execution query filtering by OwnerUserId and ordering by CreationDate DESC, Id DESC. Plan-cache average CPU is 472 and Query Store reports 31,971 executions with average duration 0.45 ms and average reads 319.
  2. Priority 2 — Add a composite Votes index beginning with PostId and followed by VoteTypeId. The query averages 80,836 CPU units in plan cache, 81.78 ms in Query Store, and 1,535 reads per execution. The overlap signal estimates a PostId candidate at 14.4 MB for 945,284 rows.
  3. Priority 3 — Add ordered composite indexes for Comments and Badges to avoid sorting after equality seeks. Both queries are frequent; the Comments query ran 32,000 times in plan cache and the Badges query ran 12,000 times.
  4. Priority 4 — Restore trust to all 19 reported foreign keys. Untrusted foreign keys can prevent join elimination and predicate pruning. The supplied script validates each relationship before re-trusting it and prints a skip message when violating rows exist.
  5. Priority 5 — Enable statistics maintenance. Auto Create Stats and Auto Update Stats are both false. This can impair cardinality estimates, including the otherwise available CE feedback features.

Confidence: High for the Posts, Votes, Comments, Badges, foreign-key, and statistics recommendations because the workload statements, execution counts, reads, and existing access paths are supplied. Storage estimates for new indexes are medium confidence because exact column data types, nullability, row compression details, and page overhead are not fully provided.

Scope and evidence

  • Index usage, missing-index, operational, and plan-cache statistics cover only 2026-11-11 03:22:28 through 2026-11-13, approximately 2.4 days since the latest database startup.
  • Query Store workload figures are reported for the last 7 days, although the supplied server/database usage statistics have the shorter 2.4-day collection window.
  • No missing-index recommendations were reported. One overlap signal exists for dbo.Votes, with candidate key PostId, 945,284 rows, estimated storage 14.4 MB, and write penalty 6,400.
  • No large heap tables, forwarded records, duplicate indexes, left-prefix overlaps, forced-plan failures, non-sargable predicates in the top 50 plan-cache statements, or indexes with page density below approximately 75% were reported.
  • No table exceeds the supplied “large table” classification. The largest reported table is dbo.Votes at 945,284 rows, below the requested 1,000,000-row columnstore consideration threshold.

Detailed prioritized recommendations

1. Add an ordered covering index for the Posts owner-history query High confidence

Target statement: SELECT TOP (20) p.Id, p.Title, p.CreationDate, p.PostTypeId FROM dbo.Posts AS p WHERE p.OwnerUserId = @UserId ORDER BY p.CreationDate DESC, p.Id DESC.

The current IX_Posts_OwnerUserId has 16,120 seeks and no reported updates during the 2.4-day usage window, but it does not encode the requested ordering or cover the projected columns. The recommended key order is OwnerUserId, CreationDate DESC, Id DESC. Include Title and PostTypeId; Id is already available as the clustered key. This should reduce sorting and key lookups.

Estimated size: approximately 15–25 MB for 246,672 rows. Estimate is based on the supplied average widths of OwnerUserId 3.89 bytes, CreationDate 8 bytes, Title 24.06 bytes, PostTypeId 2 bytes, the 4-byte clustered key, plus row and page overhead. The exact estimate is uncertain because physical data types and nullability are not supplied.

The existing single-column index should initially remain. Its low-use-drop decision cannot be made safely because usage covers only 2.4 days; monthly or quarterly work may not have run.

2. Add a composite Votes aggregation index High confidence

Target statement: SELECT v.VoteTypeId, COUNT_BIG(*) AS Votes FROM dbo.Votes AS v WHERE v.PostId = @PostId GROUP BY v.VoteTypeId.

IX_Votes_VoteTypeId is led by the wrong predicate column for this query. The query executed 80 times in plan cache with average CPU 80,836 and average logical reads 1,539; Query Store reports 159 executions, average duration 81.78 ms, and average reads 1,535. Create (PostId, VoteTypeId) so the equality predicate and grouping are both supported.

Estimated size: approximately 17–22 MB for 945,284 rows. The supplied overlap candidate for PostId alone is 14.4 MB; the range adds the smallint-like VoteTypeId key, clustered-key locator, and page overhead. The table has 6,400 reported updates during the short usage window, so the additional write maintenance is acceptable only if the aggregation query remains part of the workload.

Do not add an unfiltered index led by the low-cardinality VoteTypeId; the reported distribution has only 13 estimated distinct values and would store rows that the query does not use as its leading filter.

3. Add an ordered Comments index and leave Text to lookups High confidence

Target statement: SELECT TOP (20) c.Id, c.Score, c.Text, c.CreationDate, c.UserId FROM dbo.Comments AS c WHERE c.PostId = @PostId ORDER BY c.CreationDate DESC, c.Id DESC.

The current IX_Comments_PostId has 32,000 seeks and 5.1 MB size, but it does not provide the requested order. Create (PostId, CreationDate DESC, Id DESC) and include Score, UserId. Do not include Text: its supplied average length is 180.97 bytes, and the query returns only up to 20 rows. Key lookups for that wide value are preferable to storing it in every index row.

Estimated size: approximately 10–18 MB for the reported 365,839-row index population, based on PostId 4 bytes, CreationDate 8 bytes, Id 4 bytes, Score 4 bytes, UserId approximately 3.71 bytes, the clustered locator, and overhead. The existing index has 4,800 reported inserts and should not be dropped solely on this short observation period.

4. Add an ordered Badges index Medium-high confidence

Target statement: SELECT b.Name, b.Class, b.Date FROM dbo.Badges AS b WHERE b.UserId = @UserId ORDER BY b.Date DESC, b.Id DESC.

The current IX_Badges_UserId has 12,000 seeks but does not provide the requested ordering. Create (UserId, Date DESC, Id DESC) and include Name, Class. The query executed 12,000 times in plan cache and Query Store reports 23,971 executions, average duration 0.29 ms, and average reads 171.

Estimated size: approximately 12–20 MB for 439,352 rows, based on UserId 4 bytes, Date 8 bytes, Id 4 bytes, Name 10.82 bytes, Class 2 bytes, clustered locator, and overhead. No Badges writes were reported in the usage or operational windows, reducing write-maintenance risk.

5. Re-trust the reported foreign keys after conditional validation High confidence

Nineteen foreign keys are enabled but not trusted, including relationships on Badges, Comments, PostHistory, PostLinks, Posts, Tags, Votes, and the AIIndexTuner tables. Untrusted constraints can block optimizer join elimination and predicate pruning. The supplied script checks for violating rows individually; each constraint is re-trusted only when no violation exists, otherwise a clear message is printed.

Validation scans can be expensive on large tables. No index storage change is associated with this recommendation.

6. Enable automatic statistics creation and updating High confidence

AUTO_CREATE_STATISTICS and AUTO_UPDATE_STATISTICS are both false. Enable them to allow SQL Server to maintain statistics needed by the recommended indexes and changing distributions. Because automatic update statistics is currently disabled, enabling asynchronous updates is also recommended to reduce synchronous statistics-refresh waits after auto update is enabled.

The supplied database is on SQL Server 2025 Enterprise Developer Edition, so online index operations and advanced compression are available. No statistics update or index rebuild is required immediately based on the reported page density.

7. Do not add columnstore or filtered low-cardinality indexes in this run High confidence

No large tables were reported. The largest supplied table, Votes at 945,284 rows, is below the 1,000,000-row threshold and has insert/update activity. The workload evidence is primarily selective OLTP lookups and ordered result sets, not a demonstrated analytic scan workload. A columnstore index would add write overhead: updates to compressed rows create delete markers and new delta-store versions, while trickle inserts accumulate in the delta store.

Skewed values exist for Posts.PostTypeId and PostLinks.LinkTypeId, but no supplied frequent query filters one of the rare literals in a way that justifies a filtered index. Do not create an unfiltered index led by those low-cardinality columns.

Workload findings

AreaFindingAction
Existing index reads and writes Posts clustered PK: 76,280 seeks, 40,000 lookups, 19,200 updates, 400.3 MB. Comments.PostId: 32,000 seeks, 4,800 updates, 5.1 MB. Votes single-column indexes: zero reads and 6,400 updates each during the short window. Add only the targeted composite indexes above. Do not drop low-use indexes outright until the 2.4-day observation period is longer.
Unused PostHistory indexes PostHistory.PostId, UserId, and PostHistoryTypeId indexes report zero reads and zero writes, with sizes of 11.5 MB, 11.5 MB, and 9.8 MB. Candidate for later review, not an immediate drop. The source period is only 2.4 days and may exclude periodic work.
Page density All supplied indexes have density at or above 93.57%. The lowest is the Posts clustered PK at 93.57% with 11.69% fragmentation. No fill-factor change or rebuild is justified by density. A rebuild solely for the reported 11.69% fragmentation is not prioritized.
Non-sargability The top 50 plan-cache statements contain no reported wrapped columns, but a Query Store statement with 2,955.85 ms average duration and 505,869 reads uses YEAR(CreationDate) and a string conversion of Comments.CreationDate. Rewrite date predicates as half-open ranges. This is a code recommendation; the supplied report does not identify a stable query hash for the complete statement, so no executable rewrite script is included.
OPPO OPPO is not active because compatibility level is 160; SQL Server 2025 OPPO requires level 170 and OPTIONAL_PARAMETER_OPTIMIZATION ON. Do not change compatibility level solely for OPPO. If a broader level-170 upgrade is planned, test the workload and use Query Store to find and fix regressions.
CE feedback CE feedback and CE feedback for expressions can run: CE_FEEDBACK is ON, compatibility level is 160, and Query Store is READ_WRITE. Four rows are NO_RECOMMENDATION. NO_RECOMMENDATION is working as designed, not a problem. Avoid forcing plans or adding query hints to queries where CE feedback is desired, because such changes can make the query ineligible for CE feedback. Automatic plan correction is already ON.
DOP feedback DOP feedback is ON and can run. No feedback rows are reported. No DOP hint is recommended. DOP feedback lowers inefficient parallelism only after validation and does not replace correct MAXDOP or cost-threshold settings.
Automatic tuning FORCE_LAST_GOOD_PLAN desired and actual states are both ON. Keep enabled. No forced-plan failures were detected.

Maintenance and settings

  • Constraint trust: The conditional script below performs validation and re-trust in one batch. Existing violating data is not modified.
  • Index maintenance: No supplied index has page density below approximately 75%. The reported density does not justify a blanket rebuild or lower fill factor.
  • Statistics: Auto Create Stats and Auto Update Stats are disabled. This is a material configuration finding; each setting is separated into its own runnable script.
  • Automatic tuning: FORCE_LAST_GOOD_PLAN is already correctly enabled on an Enterprise Developer environment. No change is required.
  • Observation period caution: Any drop based only on low reads must wait because statistics cover only 2.4 days. This caution does not override dependency rules, but no duplicate or dependency-free redundant drop is identified here.

Storage impact

RecommendationEstimated new sizeBasis
Posts OwnerUserId, CreationDate, Id with Title and PostTypeId15–25 MB246,672 rows and supplied average column widths; exact physical metadata is incomplete.
Votes PostId, VoteTypeId17–22 MB945,284 rows and the supplied 14.4 MB PostId-only candidate estimate plus VoteTypeId and overhead.
Comments PostId, CreationDate, Id with Score and UserId10–18 MB365,839 reported index rows; Text is deliberately excluded because its average width is 180.97 bytes.
Badges UserId, Date, Id with Name and Class12–20 MB439,352 rows and supplied average widths.

Estimated net storage change: approximately 54–85 MB added if all four indexes are created and no existing indexes are removed. No dropped-index storage reclaim is recommended in this run. Estimates are ranges because the input does not provide complete data types, nullability, row-overhead, compression, and exact page-layout information. New indexes are specified with PAGE compression where supported; actual compressed size may differ from the uncompressed width estimate.

Scripts

Create the ordered Posts owner-history index — implements recommendation 1

USE [ForumDemo];
GO
IF NOT EXISTS
(
    SELECT 1
    FROM sys.indexes
    WHERE object_id = OBJECT_ID(N'dbo.Posts')
      AND name = N'IX_Posts_OwnerUserId_CreationDate_Id'
)
BEGIN
    CREATE NONCLUSTERED INDEX [IX_Posts_OwnerUserId_CreationDate_Id]
    ON [dbo].[Posts]
    (
        [OwnerUserId] ASC,
        [CreationDate] DESC,
        [Id] DESC
    )
    INCLUDE
    (
        [Title],
        [PostTypeId]
    )
    WITH
    (
        ONLINE = ON,
        DATA_COMPRESSION = PAGE,
        FILLFACTOR = 100
    );
END;
GO

Create the Votes aggregation index — implements recommendation 2

USE [ForumDemo];
GO
IF NOT EXISTS
(
    SELECT 1
    FROM sys.indexes
    WHERE object_id = OBJECT_ID(N'dbo.Votes')
      AND name = N'IX_Votes_PostId_VoteTypeId'
)
BEGIN
    CREATE NONCLUSTERED INDEX [IX_Votes_PostId_VoteTypeId]
    ON [dbo].[Votes]
    (
        [PostId] ASC,
        [VoteTypeId] ASC
    )
    WITH
    (
        ONLINE = ON,
        DATA_COMPRESSION = PAGE,
        FILLFACTOR = 100
    );
END;
GO

Create the ordered Comments index without the wide Text column — implements recommendation 3

USE [ForumDemo];
GO
IF NOT EXISTS
(
    SELECT 1
    FROM sys.indexes
    WHERE object_id = OBJECT_ID(N'dbo.Comments')
      AND name = N'IX_Comments_PostId_CreationDate_Id'
)
BEGIN
    CREATE NONCLUSTERED INDEX [IX_Comments_PostId_CreationDate_Id]
    ON [dbo].[Comments]
    (
        [PostId] ASC,
        [CreationDate] DESC,
        [Id] DESC
    )
    INCLUDE
    (
        [Score],
        [UserId]
    )
    WITH
    (
        ONLINE = ON,
        DATA_COMPRESSION = PAGE,
        FILLFACTOR = 100
    );
END;
GO

Create the ordered Badges index — implements recommendation 4

USE [ForumDemo];
GO
IF NOT EXISTS
(
    SELECT 1
    FROM sys.indexes
    WHERE object_id = OBJECT_ID(N'dbo.Badges')
      AND name = N'IX_Badges_UserId_Date_Id'
)
BEGIN
    CREATE NONCLUSTERED INDEX [IX_Badges_UserId_Date_Id]
    ON [dbo].[Badges]
    (
        [UserId] ASC,
        [Date] DESC,
        [Id] DESC
    )
    INCLUDE
    (
        [Name],
        [Class]
    )
    WITH
    (
        ONLINE = ON,
        DATA_COMPRESSION = PAGE,
        FILLFACTOR = 100
    );
END;
GO

Conditionally re-trust all reported foreign keys — implements recommendation 5

USE [ForumDemo];
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Badges] AS c
    LEFT JOIN [dbo].[Users] AS p ON p.[Id] = c.[UserId]
    WHERE c.[UserId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Badges] WITH CHECK CHECK CONSTRAINT [FK__Badges__UserId__6477ECF3];
ELSE
    PRINT N'Skipped FK__Badges__UserId__6477ECF3 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Comments] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[PostId]
    WHERE c.[PostId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Comments] WITH CHECK CHECK CONSTRAINT [FK__Comments__PostId__4CA06362];
ELSE
    PRINT N'Skipped FK__Comments__PostId__4CA06362 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Comments] AS c
    LEFT JOIN [dbo].[Users] AS p ON p.[Id] = c.[UserId]
    WHERE c.[UserId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Comments] WITH CHECK CHECK CONSTRAINT [FK__Comments__UserId__4D94879B];
ELSE
    PRINT N'Skipped FK__Comments__UserId__4D94879B because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[PostHistory] AS c
    LEFT JOIN [dbo].[PostHistoryTypes] AS p ON p.[Id] = c.[PostHistoryTypeId]
    WHERE c.[PostHistoryTypeId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[PostHistory] WITH CHECK CHECK CONSTRAINT [FK__PostHisto__PostH__5070F446];
ELSE
    PRINT N'Skipped FK__PostHisto__PostH__5070F446 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[PostHistory] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[PostId]
    WHERE c.[PostId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[PostHistory] WITH CHECK CHECK CONSTRAINT [FK__PostHisto__PostI__5165187F];
ELSE
    PRINT N'Skipped FK__PostHisto__PostI__5165187F because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[PostHistory] AS c
    LEFT JOIN [dbo].[Users] AS p ON p.[Id] = c.[UserId]
    WHERE c.[UserId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[PostHistory] WITH CHECK CHECK CONSTRAINT [FK__PostHisto__UserI__52593CB8];
ELSE
    PRINT N'Skipped FK__PostHisto__UserI__52593CB8 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[PostLinks] AS c
    LEFT JOIN [dbo].[LinkTypes] AS p ON p.[Id] = c.[LinkTypeId]
    WHERE c.[LinkTypeId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[PostLinks] WITH CHECK CHECK CONSTRAINT [FK__PostLinks__LinkT__571DF1D5];
ELSE
    PRINT N'Skipped FK__PostLinks__LinkT__571DF1D5 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[PostLinks] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[PostId]
    WHERE c.[PostId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[PostLinks] WITH CHECK CHECK CONSTRAINT [FK__PostLinks__PostI__5535A963];
ELSE
    PRINT N'Skipped FK__PostLinks__PostI__5535A963 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[PostLinks] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[RelatedPostId]
    WHERE c.[RelatedPostId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[PostLinks] WITH CHECK CHECK CONSTRAINT [FK__PostLinks__Relat__5629CD9C];
ELSE
    PRINT N'Skipped FK__PostLinks__Relat__5629CD9C because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Posts] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[AcceptedAnswerId]
    WHERE c.[AcceptedAnswerId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Posts] WITH CHECK CHECK CONSTRAINT [FK__Posts__AcceptedA__48CFD27E];
ELSE
    PRINT N'Skipped FK__Posts__AcceptedA__48CFD27E because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Posts] AS c
    LEFT JOIN [dbo].[Users] AS p ON p.[Id] = c.[LastEditorUserId]
    WHERE c.[LastEditorUserId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Posts] WITH CHECK CHECK CONSTRAINT [FK__Posts__LastEdito__47DBAE45];
ELSE
    PRINT N'Skipped FK__Posts__LastEdito__47DBAE45 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Posts] AS c
    LEFT JOIN [dbo].[Users] AS p ON p.[Id] = c.[OwnerUserId]
    WHERE c.[OwnerUserId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Posts] WITH CHECK CHECK CONSTRAINT [FK__Posts__OwnerUser__46E78A0C];
ELSE
    PRINT N'Skipped FK__Posts__OwnerUser__46E78A0C because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Posts] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[ParentId]
    WHERE c.[ParentId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Posts] WITH CHECK CHECK CONSTRAINT [FK__Posts__ParentId__49C3F6B7];
ELSE
    PRINT N'Skipped FK__Posts__ParentId__49C3F6B7 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Posts] AS c
    LEFT JOIN [dbo].[PostTypes] AS p ON p.[Id] = c.[PostTypeId]
    WHERE c.[PostTypeId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Posts] WITH CHECK CHECK CONSTRAINT [FK__Posts__PostTypeI__45F365D3];
ELSE
    PRINT N'Skipped FK__Posts__PostTypeI__45F365D3 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Tags] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[ExcerptPostId]
    WHERE c.[ExcerptPostId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Tags] WITH CHECK CHECK CONSTRAINT [FK__Tags__ExcerptPos__59FA5E80];
ELSE
    PRINT N'Skipped FK__Tags__ExcerptPos__59FA5E80 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Tags] AS c
    LEFT JOIN [dbo].[Posts] AS p ON p.[Id] = c.[WikiPostId]
    WHERE c.[WikiPostId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Tags] WITH CHECK CHECK CONSTRAINT [FK__Tags__WikiPostId__5AEE82B9];
ELSE
    PRINT N'Skipped FK__Tags__WikiPostId__5AEE82B9 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Votes] AS c
    LEFT JOIN [dbo].[Users] AS p ON p.[Id] = c.[UserId]
    WHERE c.[UserId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Votes] WITH CHECK CHECK CONSTRAINT [FK__Votes__UserId__5EBF139D];
ELSE
    PRINT N'Skipped FK__Votes__UserId__5EBF139D because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [dbo].[Votes] AS c
    LEFT JOIN [dbo].[VoteTypes] AS p ON p.[Id] = c.[VoteTypeId]
    WHERE c.[VoteTypeId] IS NOT NULL AND p.[Id] IS NULL
)
    ALTER TABLE [dbo].[Votes] WITH CHECK CHECK CONSTRAINT [FK__Votes__VoteTypeI__5DCAEF64];
ELSE
    PRINT N'Skipped FK__Votes__VoteTypeI__5DCAEF64 because violating rows exist.';
GO

IF NOT EXISTS
(
    SELECT 1
    FROM [AIIndexTuner].[Recommendation] AS c
    LEFT JOIN [AIIndexTuner].[Run] AS p ON p.[RunId] = c.[RunId]
    WHERE c.[RunId] IS NOT NULL AND p.[RunId] IS NULL
)
    ALTER TABLE [AIIndexTuner].[Recommendation] WITH CHECK CHECK CONSTRAINT [FK_AIIndexTuner_Recommendation_Run];
ELSE
    PRINT N'Skipped FK_AIIndexTuner_Recommendation_Run because violating rows exist.';
GO

Enable automatic statistics creation — implements recommendation 6

USE [ForumDemo];
GO
ALTER DATABASE [ForumDemo] SET AUTO_CREATE_STATISTICS ON;
GO

Enable automatic statistics updates — implements recommendation 6

USE [ForumDemo];
GO
ALTER DATABASE [ForumDemo] SET AUTO_UPDATE_STATISTICS ON;
GO

Enable asynchronous automatic statistics updates — implements recommendation 6

USE [ForumDemo];
GO
ALTER DATABASE [ForumDemo] SET AUTO_UPDATE_STATISTICS_ASYNC ON;
GO

Allow low-priority waiting for asynchronous statistics refresh — implements recommendation 6

USE [ForumDemo];
GO
ALTER DATABASE SCOPED CONFIGURATION SET ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY = ON;
GO