Back to Skills

query-optimization

verified

T-SQL query optimization techniques for SQL Server and Azure SQL Database. Use this skill when: (1) User needs to optimize slow queries, (2) User asks about SARGability or index seeks, (3) User needs help with query hints, (4) User has parameter sniffing issues, (5) User needs to understand execution plans, (6) User asks about statistics and cardinality estimation.

View on GitHub

Marketplace

claude-plugin-marketplace

JosiahSiegel/claude-plugin-marketplace

Plugin

tsql-master

Repository

JosiahSiegel/claude-plugin-marketplace
7stars

plugins/tsql-master/skills/query-optimization/SKILL.md

Last Verified

January 20, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/JosiahSiegel/claude-plugin-marketplace/blob/main/plugins/tsql-master/skills/query-optimization/SKILL.md -a claude-code --skill query-optimization

Installation paths:

Claude
.claude/skills/query-optimization/
Powered by add-skill CLI

Instructions

# Query Optimization

Comprehensive guide to T-SQL query optimization techniques.

## Quick Reference

### SARGable vs Non-SARGable Patterns

| Non-SARGable (Bad) | SARGable (Good) |
|-------------------|-----------------|
| `WHERE YEAR(Date) = 2024` | `WHERE Date >= '2024-01-01' AND Date < '2025-01-01'` |
| `WHERE LEFT(Name, 3) = 'ABC'` | `WHERE Name LIKE 'ABC%'` |
| `WHERE Amount * 1.1 > 1000` | `WHERE Amount > 1000 / 1.1` |
| `WHERE ISNULL(Col, 0) = 5` | `WHERE Col = 5 OR Col IS NULL` |
| `WHERE VarcharCol = 123` | `WHERE VarcharCol = '123'` |

### Join Types Performance

| Join Type | Best For | Characteristics |
|-----------|----------|-----------------|
| Nested Loop | Small outer, indexed inner | Low memory, good for small sets |
| Merge Join | Sorted inputs, similar sizes | Efficient for sorted data |
| Hash Join | Large unsorted inputs | High memory, good for large sets |

### Query Hints Quick Reference

| Hint | Purpose |
|------|---------|
| `OPTION (RECOMPILE)` | Fresh plan each execution |
| `OPTION (OPTIMIZE FOR (@p = value))` | Optimize for specific value |
| `OPTION (OPTIMIZE FOR UNKNOWN)` | Use average statistics |
| `OPTION (MAXDOP n)` | Limit parallelism |
| `OPTION (FORCE ORDER)` | Use exact join order |
| `WITH (NOLOCK)` | Read uncommitted (dirty reads) |
| `WITH (FORCESEEK)` | Force index seek |

## Core Optimization Principles

### 1. SARGability

SARG = Search ARGument. SARGable queries can use index seeks:

```sql
-- Non-SARGable: Function on column
WHERE DATEPART(year, OrderDate) = 2024
WHERE UPPER(CustomerName) = 'JOHN'
WHERE OrderAmount + 100 > 500

-- SARGable: Preserve column
WHERE OrderDate >= '2024-01-01' AND OrderDate < '2025-01-01'
WHERE CustomerName = 'john' COLLATE SQL_Latin1_General_CP1_CI_AS
WHERE OrderAmount > 400
```

### 2. Implicit Conversions

Avoid data type mismatches:

```sql
-- Bad: Implicit conversion (varchar column compared to int)
WHERE VarcharColumn = 12345

-- Good: Match types exactly
WHERE VarcharColumn = '1234

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
5998 chars