The Prompt Library
CodingSQL

Optimize a SQL Query

Refactor a SQL query for performance, indexing, or simplicity.

Select Model:

Prompt:

I need help optimizing a SQL query that's currently experiencing performance issues. Please analyze the query and provide recommendations for improvement. ORIGINAL QUERY: ```sql [INSERT QUERY HERE] ``` CONTEXT: - Database system: [SPECIFY DATABASE SYSTEM] - Table information: * [TABLE_NAME_1]: ~[ROW_COUNT_1] rows, [KEY_COLUMNS_1] * [TABLE_NAME_2]: ~[ROW_COUNT_2] rows, [KEY_COLUMNS_2] * [Additional tables as needed] - Current indexes: * [LIST EXISTING INDEXES] - Performance issues: * Current execution time: [CURRENT_TIME] * Resource usage: [RESOURCE_USAGE] * Problem areas: [KNOWN_ISSUES] Please provide: 1. QUERY ANALYSIS - Identify performance bottlenecks - Highlight inefficient patterns - Explain query execution concerns 2. OPTIMIZATION RECOMMENDATIONS - Structure improvements (joins, subqueries, etc.) - Filter and predicate optimizations - Projection optimizations - Aggregation improvements 3. INDEXING SUGGESTIONS - New indexes with rationale - Modifications to existing indexes - Covering index opportunities 4. OPTIMIZED QUERY - Provide refactored SQL code - Include comments explaining key changes 5. EXPECTED BENEFITS - Anticipated performance improvements - Execution plan changes - Resource utilization impact 6. TRADE-OFF CONSIDERATIONS - Potential drawbacks - Maintenance implications - Alternative approaches Please ensure the optimized query follows best practices for the specified database system and clearly explain your reasoning for each recommendation.

Related Prompts

Other prompts you might find useful

CodingSQL

Write a SQL Query

Generate a SELECT query with filters, joins, and sorting.

6 models availableView details
CodingSQL

Explain a SQL Query

Break down what a SQL query does, step-by-step.

6 models availableView details
CodingSQL

Create a Table Schema

Define a SQL table schema with columns, types, and constraints.

6 models availableView details
CodingSQL

Optimize a SQL Query

Refactor a SQL query for performance, indexing, or simplicity.

6 models availableView details