Optimize MCP query tools for large databases#830
Merged
erikdarlingdata merged 1 commit intodevfrom Apr 11, 2026
Merged
Conversation
The three heaviest MCP tools (get_top_queries_by_cpu, get_top_procedures_by_cpu, get_query_store_top) timed out on large databases because they scanned every row, decompressed text on every row, and returned all results for client-side filtering. Dashboard: add new MCP-specific service methods using multi-phase temp tables — aggregate numerics first, rank TOP N, then hydrate text via OUTER APPLY for only the winners. DECOMPRESS now runs on ~N rows instead of the entire table. Lite: rewrite queries with CTE-based phasing — inner CTE aggregates numeric-only columns with LIMIT, outer query hydrates text via LEFT JOIN LATERAL for winners. Both: push database_name filter and TOP N into SQL instead of client-side filtering. Shared UI methods are untouched. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_top_queries_by_cpu,get_top_procedures_by_cpu, andget_query_store_toptimed out on large databases (600GB+, 100M+ rows) because they scanned every row, decompressed text on every row, and did client-side TOP N filteringDECOMPRESSnow runs on ~N rows instead of the entire tableLEFT JOIN LATERALfor winners onlydatabase_namefilter and TOP N pushed into SQL instead of client-side.Where().Take()Performance
Server-side execution times on SQL2022 (all under 100ms):
Test plan
database_namefilter tested — pushes into SQL correctlyFixes #826
🤖 Generated with Claude Code