Skip to content

[ENHANCEMENT (P3-Nice-to-have)]: @Statistics filter combined with @Tables filter uses AND logic — no way to specify 'all statistics on these tables PLUS this specific stat on other tables #206

@nanoDBA

Description

@nanoDBA

Summary

When both @Statistics and @Tables are specified, the filters are combined with AND logic: only statistics matching @Statistics pattern ON tables matching @Tables pattern are processed. There is no OR mode for "give me all stats on these tables, or this specific stat regardless of table." This limits the expressiveness for targeted maintenance scenarios.

Current Behavior

Discovery query (lines ~4765–4791):

AND (@Tables IS NULL OR table_name LIKE @Tables COLLATE DATABASE_DEFAULT)
AND (@Statistics IS NULL OR stat_name LIKE @Statistics COLLATE DATABASE_DEFAULT)

The AND combination means: "table matches AND stat matches." A DBA who wants "all stats on dbo.Orders AND the specific stat dbo.Products._WA_Sys_00000001 on Products" cannot express this with a single invocation — they need two separate calls.

Expected Behavior / Proposed Fix

Document the AND logic explicitly in @Help:

When both @Tables and @Statistics are specified, the filters are combined as AND: only statistics that match BOTH patterns are processed. For OR-mode targeting (e.g., all stats on Table A plus one specific stat on Table B), run two separate invocations or use a single @Statistics pattern broad enough to cover both targets.

Optionally add @StatisticsFilterMode nvarchar(3) = 'AND' parameter:

'AND' (default): table AND stat must match
'OR': either table OR stat matches

This is a low-priority enhancement — the workaround (two invocations) is straightforward. The documentation fix is the immediate deliverable.

Technical Detail

The AND filter combination is correct for the primary use case: "run all stats on my specific tables." The gap emerges in "targeted recovery" scenarios: a DBA who wants to urgently refresh a specific statistic on an untargeted table without modifying the @Tables filter. The @Statistics pattern alone (without @Tables) covers the single-stat case. The enhancement would only be useful for multi-table multi-stat targeting in a single invocation.

Test Cases

  • Set @Tables='dbo.Orders', @Statistics='_WA_Sys%'. Verify only auto-stats on Orders are processed (AND logic: must be on Orders AND match _WA_Sys pattern).
  • Set @Statistics='IX_Orders_Date'. Verify this stat is processed regardless of @Tables (no @tables filter).
  • Verify @Help explicitly documents AND logic for combined @Tables + @Statistics.

References

  • sp_StatUpdate discovery WHERE clause (lines ~4765–4791)
  • sp_StatUpdate @Tables and @Statistics parameters
  • sp_StatUpdate @Help — parameter interaction documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or requestp3-nice-to-haveLow-impact enhancement or minor improvement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions