Skip to main content
Glama
blitzstermayank

Teradata MCP Server

sql_Retrieve_Cluster_Queries

Extract SQL queries and performance metrics from Teradata clusters to analyze query patterns, identify optimization opportunities, and correlate performance issues with specific SQL structures.

Instructions

RETRIEVE ACTUAL SQL QUERIES FROM SPECIFIC CLUSTERS FOR PATTERN ANALYSIS

This tool extracts the actual SQL query text and performance metrics from selected clusters, enabling detailed pattern analysis and specific optimization recommendations. Essential for moving from cluster-level analysis to actual query optimization.

DETAILED ANALYSIS CAPABILITIES:

  • SQL Pattern Recognition: Analyze actual query structures, joins, predicates, and functions

  • Performance Correlation: Connect query patterns to specific performance characteristics

  • Optimization Identification: Identify common anti-patterns, missing indexes, inefficient joins

  • Code Quality Assessment: Evaluate query construction, complexity, and best practices

  • Workload Understanding: See actual business logic and data access patterns

QUERY SELECTION STRATEGIES:

  • By CPU Impact: Sort by 'ampcputime' to focus on highest CPU consumers

  • By I/O Volume: Sort by 'logicalio' to find scan-intensive queries

  • By Skew Problems: Sort by 'cpuskw' or 'ioskw' for distribution issues

  • By Complexity: Sort by 'numsteps' for complex execution plans

  • By Response Time: Sort by 'response_secs' for user experience impact

AVAILABLE METRICS FOR SORTING:

  • ampcputime: Total CPU seconds (primary optimization target)

  • logicalio: Total logical I/O operations (scan indicator)

  • cpuskw: CPU skew ratio (distribution problems)

  • ioskw: I/O skew ratio (hot spot indicators)

  • pji: Physical-to-Logical I/O ratio (compute intensity)

  • uii: Unit I/O Intensity (I/O efficiency)

  • numsteps: Query execution plan steps (complexity)

  • response_secs: Wall-clock execution time (user impact)

  • delaytime: Time spent in queue (concurrency issues)

AUTOMATIC PERFORMANCE CATEGORIZATION: Each query is categorized using configurable thresholds (from sql_opt_config.yml):

  • CPU Categories: VERY_HIGH_CPU (>config.very_high), HIGH_CPU (>config.high), MEDIUM_CPU (>10s), LOW_CPU

  • CPU Skew: SEVERE_CPU_SKEW (>config.severe), HIGH_CPU_SKEW (>config.high), MODERATE_CPU_SKEW (>config.moderate), NORMAL

  • I/O Skew: SEVERE_IO_SKEW (>config.severe), HIGH_IO_SKEW (>config.high), MODERATE_IO_SKEW (>config.moderate), NORMAL

Use thresholds set in config file for, CPU - high, very_high, Skew moderate, high, severe

TYPICAL OPTIMIZATION WORKFLOW:

  1. Start with clusters identified from sql_Analyze_Cluster_Stats

  2. Retrieve top queries by impact metric (usually 'ampcputime')

  3. Analyze SQL patterns for common issues:

    • Missing WHERE clauses or inefficient predicates

    • Cartesian products or missing JOIN conditions

    • Inefficient GROUP BY or ORDER BY operations

    • Suboptimal table access patterns

    • Missing or outdated statistics

  4. Develop specific optimization recommendations

QUERY LIMIT STRATEGY:

  • Use the query limit set in config file for pattern recognition and analysis, unless user specifies a different limit

OUTPUT INCLUDES:

  • Complete SQL query text for each query

  • All performance metrics, user, application, and workload context, cluster membership and rankings

  • Performance categories for quick filtering

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cluster_idsYes
metricNoampcputime
limit_per_clusterNo

Implementation Reference

  • The main handler function for the 'sql_Retrieve_Cluster_Queries' tool. It takes database connection, cluster IDs, sorting metric, and limit; executes SQL to retrieve top queries from clusters with performance metrics and categorizations; formats results as JSON with metadata.
    def handle_sql_Retrieve_Cluster_Queries(
        conn,
        cluster_ids: List[int],
        metric: str = "ampcputime",
        limit_per_cluster: int = 250,
        *args,
        **kwargs
    ):
        """
            **RETRIEVE ACTUAL SQL QUERIES FROM SPECIFIC CLUSTERS FOR PATTERN ANALYSIS**
    
            This tool extracts the actual SQL query text and performance metrics from selected clusters, enabling detailed pattern analysis and specific optimization recommendations. Essential for moving from cluster-level analysis to actual query optimization.
    
            **DETAILED ANALYSIS CAPABILITIES:**
            - **SQL Pattern Recognition**: Analyze actual query structures, joins, predicates, and functions
            - **Performance Correlation**: Connect query patterns to specific performance characteristics
            - **Optimization Identification**: Identify common anti-patterns, missing indexes, inefficient joins
            - **Code Quality Assessment**: Evaluate query construction, complexity, and best practices
            - **Workload Understanding**: See actual business logic and data access patterns
    
            **QUERY SELECTION STRATEGIES:**
            - **By CPU Impact**: Sort by 'ampcputime' to focus on highest CPU consumers
            - **By I/O Volume**: Sort by 'logicalio' to find scan-intensive queries
            - **By Skew Problems**: Sort by 'cpuskw' or 'ioskw' for distribution issues
            - **By Complexity**: Sort by 'numsteps' for complex execution plans
            - **By Response Time**: Sort by 'response_secs' for user experience impact
    
            **AVAILABLE METRICS FOR SORTING:**
            - **ampcputime**: Total CPU seconds (primary optimization target)
            - **logicalio**: Total logical I/O operations (scan indicator)
            - **cpuskw**: CPU skew ratio (distribution problems)
            - **ioskw**: I/O skew ratio (hot spot indicators)
            - **pji**: Physical-to-Logical I/O ratio (compute intensity)
            - **uii**: Unit I/O Intensity (I/O efficiency)
            - **numsteps**: Query execution plan steps (complexity)
            - **response_secs**: Wall-clock execution time (user impact)
            - **delaytime**: Time spent in queue (concurrency issues)
    
            **AUTOMATIC PERFORMANCE CATEGORIZATION:**
            Each query is categorized using configurable thresholds (from sql_opt_config.yml):
            - **CPU Categories**: VERY_HIGH_CPU (>config.very_high), HIGH_CPU (>config.high), MEDIUM_CPU (>10s), LOW_CPU
            - **CPU Skew**: SEVERE_CPU_SKEW (>config.severe), HIGH_CPU_SKEW (>config.high), MODERATE_CPU_SKEW (>config.moderate), NORMAL
            - **I/O Skew**: SEVERE_IO_SKEW (>config.severe), HIGH_IO_SKEW (>config.high), MODERATE_IO_SKEW (>config.moderate), NORMAL
            
            Use thresholds set in config file for, CPU - high, very_high, Skew moderate, high, severe
    
            **TYPICAL OPTIMIZATION WORKFLOW:**
            1. Start with clusters identified from sql_Analyze_Cluster_Stats
            2. Retrieve top queries by impact metric (usually 'ampcputime')
            3. Analyze SQL patterns for common issues:
               - Missing WHERE clauses or inefficient predicates
               - Cartesian products or missing JOIN conditions
               - Inefficient GROUP BY or ORDER BY operations
               - Suboptimal table access patterns
               - Missing or outdated statistics
            4. Develop specific optimization recommendations
    
            **QUERY LIMIT STRATEGY:**
            - Use the query limit set in config file for  pattern recognition and analysis, unless user specifies a different limit
    
            **OUTPUT INCLUDES:**
            - Complete SQL query text for each query
            - All performance metrics, user, application, and workload context, cluster membership and rankings
            - Performance categories for quick filtering        
            """
        
        config = SQL_CLUSTERING_CONFIG
        
        logger.debug(f"handle_sql_Retrieve_Cluster_Queries: clusters={cluster_ids}, metric={metric}, limit={limit_per_cluster}")
        
        feature_db = config['databases']['feature_db']
        clusters_table = config['tables']['sql_query_clusters']
        
        # Validate metric
        valid_metrics = [
            'ampcputime', 'logicalio', 'cpuskw', 'ioskw', 'pji', 'uii',
            'numsteps', 'response_secs', 'delaytime'
        ]
        
        if metric not in valid_metrics:
            metric = 'ampcputime'  # Default fallback
    
        # Convert cluster_ids list to comma-separated string for SQL IN clause
        cluster_ids_str = ','.join(map(str, cluster_ids))
    
        with conn.cursor() as cur:
            
            # Get thresholds from config
            thresholds = config.get('performance_thresholds', {})
            cpu_high = thresholds.get('cpu', {}).get('high', 100)
            cpu_very_high = thresholds.get('cpu', {}).get('very_high', 1000)
            skew_moderate = thresholds.get('skew', {}).get('moderate', 2.0)
            skew_high = thresholds.get('skew', {}).get('high', 3.0)
            skew_severe = thresholds.get('skew', {}).get('severe', 5.0)
            
            retrieve_queries_sql = f"""
            SELECT 
                td_clusterid_kmeans,
                id,
                txt,
                username,
                appid,
                numsteps,
                ampcputime,
                logicalio,
                wdname,
                cpuskw,
                ioskw,
                pji,
                uii,
                response_secs,
                response_mins,
                delaytime,
                silhouette_score,
                -- Ranking within cluster by selected metric
                ROW_NUMBER() OVER (PARTITION BY td_clusterid_kmeans ORDER BY {metric} DESC) AS rank_in_cluster,
                -- Overall ranking across all selected clusters
                ROW_NUMBER() OVER (ORDER BY {metric} DESC) AS overall_rank,
                -- Performance categorization with configurable thresholds
                CASE 
                    WHEN ampcputime > {cpu_very_high} THEN 'VERY_HIGH_CPU'
                    WHEN ampcputime > {cpu_high} THEN 'HIGH_CPU'
                    WHEN ampcputime > 10 THEN 'MEDIUM_CPU'
                    ELSE 'LOW_CPU'
                END AS cpu_category,
                CASE 
                    WHEN cpuskw > {skew_severe} THEN 'SEVERE_CPU_SKEW'
                    WHEN cpuskw > {skew_high} THEN 'HIGH_CPU_SKEW'
                    WHEN cpuskw > {skew_moderate} THEN 'MODERATE_CPU_SKEW'
                    ELSE 'NORMAL_CPU_SKEW'
                END AS cpu_skew_category,
                CASE 
                    WHEN ioskw > {skew_severe} THEN 'SEVERE_IO_SKEW'
                    WHEN ioskw > {skew_high} THEN 'HIGH_IO_SKEW'
                    WHEN ioskw > {skew_moderate} THEN 'MODERATE_IO_SKEW'
                    ELSE 'NORMAL_IO_SKEW'
                END AS io_skew_category
            FROM {feature_db}.{clusters_table}
            WHERE td_clusterid_kmeans IN ({cluster_ids_str})
            QUALIFY ROW_NUMBER() OVER (PARTITION BY td_clusterid_kmeans ORDER BY {metric} DESC) <= {limit_per_cluster}
            ORDER BY td_clusterid_kmeans, {metric} DESC
            """
            
            cur.execute(retrieve_queries_sql)
            data = rows_to_json(cur.description, cur.fetchall())
            
            # Get summary by cluster
            cur.execute(f"""
            SELECT 
                td_clusterid_kmeans,
                COUNT(*) AS queries_retrieved,
                AVG({metric}) AS avg_metric_value,
                MAX({metric}) AS max_metric_value,
                MIN({metric}) AS min_metric_value
            FROM {feature_db}.{clusters_table}
            WHERE td_clusterid_kmeans IN ({cluster_ids_str})
            GROUP BY td_clusterid_kmeans
            ORDER BY td_clusterid_kmeans
            """)
            
            cluster_summary = rows_to_json(cur.description, cur.fetchall())
            
            logger.debug(f"Retrieved {len(data)} queries from {len(cluster_ids)} clusters")
    
        # Return results with metadata
        metadata = {
            "tool_name": "sql_Retrieve_Cluster_Queries",
            "retrieval_parameters": {
                "cluster_ids": cluster_ids,
                "sort_metric": metric,
                "limit_per_cluster": limit_per_cluster,
                "valid_metrics": valid_metrics
            },
            "cluster_summary": cluster_summary,
            "queries_retrieved": len(data),
            "table_source": f"{feature_db}.{clusters_table}",
            "analysis_ready": True,
            "description": f"Retrieved top {limit_per_cluster} queries per cluster sorted by {metric} - ready for pattern analysis and optimization recommendations"
        }
    
        return create_response(data, metadata)
  • Detailed docstring serving as input/output schema documentation for the tool, describing parameters (cluster_ids: List[int], metric: str, limit_per_cluster: int), available metrics, expected behavior, categorization logic, and output structure.
    """
        **RETRIEVE ACTUAL SQL QUERIES FROM SPECIFIC CLUSTERS FOR PATTERN ANALYSIS**
    
        This tool extracts the actual SQL query text and performance metrics from selected clusters, enabling detailed pattern analysis and specific optimization recommendations. Essential for moving from cluster-level analysis to actual query optimization.
    
        **DETAILED ANALYSIS CAPABILITIES:**
        - **SQL Pattern Recognition**: Analyze actual query structures, joins, predicates, and functions
        - **Performance Correlation**: Connect query patterns to specific performance characteristics
        - **Optimization Identification**: Identify common anti-patterns, missing indexes, inefficient joins
        - **Code Quality Assessment**: Evaluate query construction, complexity, and best practices
        - **Workload Understanding**: See actual business logic and data access patterns
    
        **QUERY SELECTION STRATEGIES:**
        - **By CPU Impact**: Sort by 'ampcputime' to focus on highest CPU consumers
        - **By I/O Volume**: Sort by 'logicalio' to find scan-intensive queries
        - **By Skew Problems**: Sort by 'cpuskw' or 'ioskw' for distribution issues
        - **By Complexity**: Sort by 'numsteps' for complex execution plans
        - **By Response Time**: Sort by 'response_secs' for user experience impact
    
        **AVAILABLE METRICS FOR SORTING:**
        - **ampcputime**: Total CPU seconds (primary optimization target)
        - **logicalio**: Total logical I/O operations (scan indicator)
        - **cpuskw**: CPU skew ratio (distribution problems)
        - **ioskw**: I/O skew ratio (hot spot indicators)
        - **pji**: Physical-to-Logical I/O ratio (compute intensity)
        - **uii**: Unit I/O Intensity (I/O efficiency)
        - **numsteps**: Query execution plan steps (complexity)
        - **response_secs**: Wall-clock execution time (user impact)
        - **delaytime**: Time spent in queue (concurrency issues)
    
        **AUTOMATIC PERFORMANCE CATEGORIZATION:**
        Each query is categorized using configurable thresholds (from sql_opt_config.yml):
        - **CPU Categories**: VERY_HIGH_CPU (>config.very_high), HIGH_CPU (>config.high), MEDIUM_CPU (>10s), LOW_CPU
        - **CPU Skew**: SEVERE_CPU_SKEW (>config.severe), HIGH_CPU_SKEW (>config.high), MODERATE_CPU_SKEW (>config.moderate), NORMAL
        - **I/O Skew**: SEVERE_IO_SKEW (>config.severe), HIGH_IO_SKEW (>config.high), MODERATE_IO_SKEW (>config.moderate), NORMAL
        
        Use thresholds set in config file for, CPU - high, very_high, Skew moderate, high, severe
    
        **TYPICAL OPTIMIZATION WORKFLOW:**
        1. Start with clusters identified from sql_Analyze_Cluster_Stats
        2. Retrieve top queries by impact metric (usually 'ampcputime')
        3. Analyze SQL patterns for common issues:
           - Missing WHERE clauses or inefficient predicates
           - Cartesian products or missing JOIN conditions
           - Inefficient GROUP BY or ORDER BY operations
           - Suboptimal table access patterns
           - Missing or outdated statistics
        4. Develop specific optimization recommendations
    
        **QUERY LIMIT STRATEGY:**
        - Use the query limit set in config file for  pattern recognition and analysis, unless user specifies a different limit
    
        **OUTPUT INCLUDES:**
        - Complete SQL query text for each query
        - All performance metrics, user, application, and workload context, cluster membership and rankings
        - Performance categories for quick filtering        
        """
  • Package __init__.py that imports sql_opt_tools, making the handler available for auto-discovery by the module_loader which scans functions in loaded modules for tool registration.
    from .sql_opt_resources import *
    from .sql_opt_tools import *
  • Utility function used by the handler to format the tool response as standardized JSON with optional metadata.
    def create_response(data: Any, metadata: dict[str, Any] | None = None) -> str:
        """Create a standardized JSON response structure"""
        if metadata:
            response = {
                "status": "success",
                "metadata": metadata,
                "results": data
            }
        else:
            response = {
                "status": "success",
                "results": data
            }
    
        return json.dumps(response, default=serialize_teradata_types)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It does well by explaining the tool's capabilities (e.g., pattern recognition, performance correlation), output format ('Complete SQL query text for each query, all performance metrics...'), and workflow context. However, it doesn't explicitly mention potential side effects (e.g., read-only nature, performance impact on the database) or error conditions, which would be helpful for a tool interacting with production systems.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (e.g., DETAILED ANALYSIS CAPABILITIES, QUERY SELECTION STRATEGIES), but it's overly verbose at 500+ words. Some sections like 'AUTOMATIC PERFORMANCE CATEGORIZATION' and 'TYPICAL OPTIMIZATION WORKFLOW' contain implementation details that could be condensed. While informative, it's not front-loaded enough—the core purpose gets buried after bold headers.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (3 parameters, no output schema, no annotations), the description provides substantial context about capabilities, metrics, categorization logic, and workflow integration. It explains what the tool returns and how to interpret results. However, it lacks explicit information about error handling, authentication requirements, or rate limits, which would be valuable for a database querying tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 0%, so the description must compensate fully. It provides extensive context for the parameters: 'cluster_ids' is implied through references to 'selected clusters' and workflow steps; 'metric' is thoroughly explained with a dedicated 'AVAILABLE METRICS FOR SORTING' section listing all options and their meanings; 'limit_per_cluster' is addressed in 'QUERY LIMIT STRATEGY' with default behavior and user override capability. The description adds significant value beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'extracts the actual SQL query text and performance metrics from selected clusters, enabling detailed pattern analysis and specific optimization recommendations.' It specifies the verb ('extracts'), resource ('SQL query text and performance metrics'), and scope ('from selected clusters'), and distinguishes it from sibling tools like sql_Analyze_Cluster_Stats by focusing on query-level details rather than cluster-level stats.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool: 'Start with clusters identified from sql_Analyze_Cluster_Stats' and 'Essential for moving from cluster-level analysis to actual query optimization.' It clearly positions this tool in a workflow with a specific sibling tool and explains its role in transitioning from high-level analysis to detailed optimization.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/blitzstermayank/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server