@patricio0312rev/skillset 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +176 -0
- package/bin/cli.js +37 -0
- package/package.json +55 -0
- package/src/commands/init.js +301 -0
- package/src/index.js +168 -0
- package/src/lib/config.js +200 -0
- package/src/lib/generator.js +166 -0
- package/src/utils/display.js +95 -0
- package/src/utils/readme.js +196 -0
- package/src/utils/tool-specific.js +233 -0
- package/templates/ai-engineering/agent-orchestration-planner/ SKILL.md +266 -0
- package/templates/ai-engineering/cost-latency-optimizer/ SKILL.md +270 -0
- package/templates/ai-engineering/doc-to-vector-dataset-generator/ SKILL.md +239 -0
- package/templates/ai-engineering/evaluation-harness/ SKILL.md +219 -0
- package/templates/ai-engineering/guardrails-safety-filter-builder/ SKILL.md +226 -0
- package/templates/ai-engineering/llm-debugger/ SKILL.md +283 -0
- package/templates/ai-engineering/prompt-regression-tester/ SKILL.md +216 -0
- package/templates/ai-engineering/prompt-template-builder/ SKILL.md +393 -0
- package/templates/ai-engineering/rag-pipeline-builder/ SKILL.md +244 -0
- package/templates/ai-engineering/tool-function-schema-designer/ SKILL.md +219 -0
- package/templates/architecture/adr-writer/ SKILL.md +250 -0
- package/templates/architecture/api-versioning-deprecation-planner/ SKILL.md +331 -0
- package/templates/architecture/domain-model-boundaries-mapper/ SKILL.md +300 -0
- package/templates/architecture/migration-planner/ SKILL.md +376 -0
- package/templates/architecture/performance-budget-setter/ SKILL.md +318 -0
- package/templates/architecture/reliability-strategy-builder/ SKILL.md +286 -0
- package/templates/architecture/rfc-generator/ SKILL.md +362 -0
- package/templates/architecture/scalability-playbook/ SKILL.md +279 -0
- package/templates/architecture/system-design-generator/ SKILL.md +339 -0
- package/templates/architecture/tech-debt-prioritizer/ SKILL.md +329 -0
- package/templates/backend/api-contract-normalizer/ SKILL.md +487 -0
- package/templates/backend/api-endpoint-generator/ SKILL.md +415 -0
- package/templates/backend/auth-module-builder/ SKILL.md +99 -0
- package/templates/backend/background-jobs-designer/ SKILL.md +166 -0
- package/templates/backend/caching-strategist/ SKILL.md +190 -0
- package/templates/backend/error-handling-standardizer/ SKILL.md +174 -0
- package/templates/backend/rate-limiting-abuse-protection/ SKILL.md +147 -0
- package/templates/backend/rbac-permissions-builder/ SKILL.md +158 -0
- package/templates/backend/service-layer-extractor/ SKILL.md +269 -0
- package/templates/backend/webhook-receiver-hardener/ SKILL.md +211 -0
- package/templates/ci-cd/artifact-sbom-publisher/ SKILL.md +236 -0
- package/templates/ci-cd/caching-strategy-optimizer/ SKILL.md +195 -0
- package/templates/ci-cd/deployment-checklist-generator/ SKILL.md +381 -0
- package/templates/ci-cd/github-actions-pipeline-creator/ SKILL.md +348 -0
- package/templates/ci-cd/monorepo-ci-optimizer/ SKILL.md +298 -0
- package/templates/ci-cd/preview-environments-builder/ SKILL.md +187 -0
- package/templates/ci-cd/quality-gates-enforcer/ SKILL.md +342 -0
- package/templates/ci-cd/release-automation-builder/ SKILL.md +281 -0
- package/templates/ci-cd/rollback-workflow-builder/ SKILL.md +372 -0
- package/templates/ci-cd/secrets-env-manager/ SKILL.md +242 -0
- package/templates/db-management/backup-restore-runbook-generator/ SKILL.md +505 -0
- package/templates/db-management/data-integrity-auditor/ SKILL.md +505 -0
- package/templates/db-management/data-retention-archiving-planner/ SKILL.md +430 -0
- package/templates/db-management/data-seeding-fixtures-builder/ SKILL.md +375 -0
- package/templates/db-management/db-performance-watchlist/ SKILL.md +425 -0
- package/templates/db-management/etl-sync-job-builder/ SKILL.md +457 -0
- package/templates/db-management/multi-tenant-safety-checker/ SKILL.md +398 -0
- package/templates/db-management/prisma-migration-assistant/ SKILL.md +379 -0
- package/templates/db-management/schema-consistency-checker/ SKILL.md +440 -0
- package/templates/db-management/sql-query-optimizer/ SKILL.md +324 -0
- package/templates/foundation/changelog-writer/ SKILL.md +431 -0
- package/templates/foundation/code-formatter-installer/ SKILL.md +320 -0
- package/templates/foundation/codebase-summarizer/ SKILL.md +360 -0
- package/templates/foundation/dependency-doctor/ SKILL.md +163 -0
- package/templates/foundation/dev-environment-bootstrapper/ SKILL.md +259 -0
- package/templates/foundation/dev-onboarding-builder/ SKILL.md +556 -0
- package/templates/foundation/docs-starter-kit/ SKILL.md +574 -0
- package/templates/foundation/explaining-code/SKILL.md +13 -0
- package/templates/foundation/git-hygiene-enforcer/ SKILL.md +455 -0
- package/templates/foundation/project-scaffolder/ SKILL.md +65 -0
- package/templates/foundation/project-scaffolder/references/templates.md +126 -0
- package/templates/foundation/repo-structure-linter/ SKILL.md +0 -0
- package/templates/foundation/repo-structure-linter/references/conventions.md +98 -0
- package/templates/frontend/animation-micro-interaction-pack/ SKILL.md +41 -0
- package/templates/frontend/component-scaffold-generator/ SKILL.md +562 -0
- package/templates/frontend/design-to-component-translator/ SKILL.md +547 -0
- package/templates/frontend/form-wizard-builder/ SKILL.md +553 -0
- package/templates/frontend/frontend-refactor-planner/ SKILL.md +37 -0
- package/templates/frontend/i18n-frontend-implementer/ SKILL.md +44 -0
- package/templates/frontend/modal-drawer-system/ SKILL.md +377 -0
- package/templates/frontend/page-layout-builder/ SKILL.md +630 -0
- package/templates/frontend/state-ux-flow-builder/ SKILL.md +23 -0
- package/templates/frontend/table-builder/ SKILL.md +350 -0
- package/templates/performance/alerting-dashboard-builder/ SKILL.md +162 -0
- package/templates/performance/backend-latency-profiler-helper/ SKILL.md +108 -0
- package/templates/performance/caching-cdn-strategy-planner/ SKILL.md +150 -0
- package/templates/performance/capacity-planning-helper/ SKILL.md +242 -0
- package/templates/performance/core-web-vitals-tuner/ SKILL.md +126 -0
- package/templates/performance/incident-runbook-generator/ SKILL.md +162 -0
- package/templates/performance/load-test-scenario-builder/ SKILL.md +256 -0
- package/templates/performance/observability-setup/ SKILL.md +232 -0
- package/templates/performance/postmortem-writer/ SKILL.md +203 -0
- package/templates/performance/structured-logging-standardizer/ SKILL.md +122 -0
- package/templates/security/auth-security-reviewer/ SKILL.md +428 -0
- package/templates/security/dependency-vulnerability-triage/ SKILL.md +495 -0
- package/templates/security/input-validation-sanitization-auditor/ SKILL.md +76 -0
- package/templates/security/pii-redaction-logging-policy-builder/ SKILL.md +65 -0
- package/templates/security/rbac-policy-tester/ SKILL.md +80 -0
- package/templates/security/secrets-scanner/ SKILL.md +462 -0
- package/templates/security/secure-headers-csp-builder/ SKILL.md +404 -0
- package/templates/security/security-incident-playbook-generator/ SKILL.md +76 -0
- package/templates/security/security-pr-checklist-skill/ SKILL.md +62 -0
- package/templates/security/threat-model-generator/ SKILL.md +394 -0
- package/templates/testing/contract-testing-builder/ SKILL.md +492 -0
- package/templates/testing/coverage-strategist/ SKILL.md +436 -0
- package/templates/testing/e2e-test-builder/ SKILL.md +382 -0
- package/templates/testing/flaky-test-detective/ SKILL.md +416 -0
- package/templates/testing/integration-test-builder/ SKILL.md +525 -0
- package/templates/testing/mocking-assistant/ SKILL.md +383 -0
- package/templates/testing/snapshot-test-refactorer/ SKILL.md +375 -0
- package/templates/testing/test-data-factory-builder/ SKILL.md +449 -0
- package/templates/testing/test-reporting-triage-skill/ SKILL.md +469 -0
- package/templates/testing/unit-test-generator/ SKILL.md +548 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: sql-query-optimizer
|
|
3
|
+
description: Analyzes and optimizes SQL queries using EXPLAIN plans, index recommendations, query rewrites, and performance benchmarking. Use for "query optimization", "slow queries", "database performance", or "EXPLAIN analysis".
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# SQL Query Optimizer
|
|
7
|
+
|
|
8
|
+
Optimize SQL queries for maximum performance.
|
|
9
|
+
|
|
10
|
+
## EXPLAIN Analysis
|
|
11
|
+
|
|
12
|
+
```sql
|
|
13
|
+
-- Original slow query
|
|
14
|
+
EXPLAIN ANALYZE
|
|
15
|
+
SELECT u.*, COUNT(o.id) as order_count
|
|
16
|
+
FROM users u
|
|
17
|
+
LEFT JOIN orders o ON o.user_id = u.id
|
|
18
|
+
WHERE u.created_at > '2024-01-01'
|
|
19
|
+
GROUP BY u.id
|
|
20
|
+
ORDER BY order_count DESC
|
|
21
|
+
LIMIT 10;
|
|
22
|
+
|
|
23
|
+
-- Output analysis:
|
|
24
|
+
/*
|
|
25
|
+
Sort (cost=15234.32..15234.34 rows=10 width=120) (actual time=245.123..245.125 rows=10 loops=1)
|
|
26
|
+
Sort Key: (count(o.id)) DESC
|
|
27
|
+
-> HashAggregate (cost=15000.00..15100.00 rows=1000 width=120) (actual time=244.891..245.023 rows=1000 loops=1)
|
|
28
|
+
Group Key: u.id
|
|
29
|
+
-> Hash Left Join (cost=1234.56..14500.00 rows=50000 width=112) (actual time=12.345..230.456 rows=50000 loops=1)
|
|
30
|
+
Hash Cond: (o.user_id = u.id)
|
|
31
|
+
-> Seq Scan on orders o (cost=0.00..10000.00 rows=100000 width=8) (actual time=0.012..180.234 rows=100000 loops=1)
|
|
32
|
+
-> Hash (cost=1000.00..1000.00 rows=5000 width=112) (actual time=10.234..10.234 rows=5000 loops=1)
|
|
33
|
+
Buckets: 8192 Batches: 1 Memory Usage: 456kB
|
|
34
|
+
-> Seq Scan on users u (cost=0.00..1000.00 rows=5000 width=112) (actual time=0.008..5.123 rows=5000 loops=1)
|
|
35
|
+
Filter: (created_at > '2024-01-01'::date)
|
|
36
|
+
Rows Removed by Filter: 1000
|
|
37
|
+
Planning Time: 0.234 ms
|
|
38
|
+
Execution Time: 245.234 ms
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
-- Issues identified:
|
|
42
|
+
-- 1. Seq Scan on orders (no index on user_id)
|
|
43
|
+
-- 2. Seq Scan on users (no index on created_at)
|
|
44
|
+
-- 3. Full table scans expensive
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Index Recommendations
|
|
48
|
+
|
|
49
|
+
```sql
|
|
50
|
+
-- Problem: Sequential scans
|
|
51
|
+
EXPLAIN ANALYZE
|
|
52
|
+
SELECT * FROM orders WHERE user_id = 123;
|
|
53
|
+
/*
|
|
54
|
+
Seq Scan on orders (cost=0.00..10000.00 rows=50 width=100) (actual time=0.012..89.456 rows=50 loops=1)
|
|
55
|
+
Filter: (user_id = 123)
|
|
56
|
+
Rows Removed by Filter: 99950
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
-- Solution: Add index
|
|
60
|
+
CREATE INDEX idx_orders_user_id ON orders(user_id);
|
|
61
|
+
|
|
62
|
+
-- After index:
|
|
63
|
+
/*
|
|
64
|
+
Index Scan using idx_orders_user_id on orders (cost=0.29..45.32 rows=50 width=100) (actual time=0.023..0.089 rows=50 loops=1)
|
|
65
|
+
Index Cond: (user_id = 123)
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
-- Performance: 89ms ā 0.09ms (990x faster!)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Query Rewrites
|
|
72
|
+
|
|
73
|
+
### 1. Avoid SELECT \*
|
|
74
|
+
|
|
75
|
+
```sql
|
|
76
|
+
-- ā Bad: Fetches all columns
|
|
77
|
+
SELECT * FROM users WHERE id = 123;
|
|
78
|
+
|
|
79
|
+
-- ā
Good: Fetch only needed columns
|
|
80
|
+
SELECT id, email, name FROM users WHERE id = 123;
|
|
81
|
+
|
|
82
|
+
-- Performance: 50% faster, less network transfer
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 2. Use EXISTS Instead of IN
|
|
86
|
+
|
|
87
|
+
```sql
|
|
88
|
+
-- ā Slow: Subquery executed fully
|
|
89
|
+
SELECT * FROM users
|
|
90
|
+
WHERE id IN (SELECT user_id FROM orders WHERE total > 100);
|
|
91
|
+
|
|
92
|
+
-- ā
Fast: Short-circuits on first match
|
|
93
|
+
SELECT * FROM users u
|
|
94
|
+
WHERE EXISTS (
|
|
95
|
+
SELECT 1 FROM orders o
|
|
96
|
+
WHERE o.user_id = u.id AND o.total > 100
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
-- Performance: 3x faster on large datasets
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 3. Avoid Functions on Indexed Columns
|
|
103
|
+
|
|
104
|
+
```sql
|
|
105
|
+
-- ā Bad: Index not used
|
|
106
|
+
SELECT * FROM users WHERE LOWER(email) = 'john@example.com';
|
|
107
|
+
|
|
108
|
+
-- ā
Good: Index scan possible
|
|
109
|
+
SELECT * FROM users WHERE email = 'john@example.com';
|
|
110
|
+
|
|
111
|
+
-- Or create functional index:
|
|
112
|
+
CREATE INDEX idx_users_email_lower ON users(LOWER(email));
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 4. Use Covering Indexes
|
|
116
|
+
|
|
117
|
+
```sql
|
|
118
|
+
-- Query needs: id, email, name
|
|
119
|
+
SELECT id, email, name FROM users WHERE email = 'john@example.com';
|
|
120
|
+
|
|
121
|
+
-- Create covering index (includes all needed columns)
|
|
122
|
+
CREATE INDEX idx_users_email_covering ON users(email) INCLUDE (id, name);
|
|
123
|
+
|
|
124
|
+
-- Result: Index-only scan (no table access needed)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### 5. Optimize JOIN Order
|
|
128
|
+
|
|
129
|
+
```sql
|
|
130
|
+
-- ā Bad: Large table first
|
|
131
|
+
SELECT * FROM orders o
|
|
132
|
+
JOIN users u ON u.id = o.user_id
|
|
133
|
+
WHERE u.email = 'john@example.com';
|
|
134
|
+
|
|
135
|
+
-- ā
Good: Filter first, join second
|
|
136
|
+
SELECT * FROM users u
|
|
137
|
+
JOIN orders o ON o.user_id = u.id
|
|
138
|
+
WHERE u.email = 'john@example.com';
|
|
139
|
+
|
|
140
|
+
-- Or use CTE for clarity:
|
|
141
|
+
WITH filtered_users AS (
|
|
142
|
+
SELECT id FROM users WHERE email = 'john@example.com'
|
|
143
|
+
)
|
|
144
|
+
SELECT o.* FROM orders o
|
|
145
|
+
JOIN filtered_users u ON u.id = o.user_id;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Composite Indexes
|
|
149
|
+
|
|
150
|
+
```sql
|
|
151
|
+
-- Query pattern: WHERE user_id = X AND status = 'active' ORDER BY created_at DESC
|
|
152
|
+
CREATE INDEX idx_orders_user_status_created
|
|
153
|
+
ON orders(user_id, status, created_at DESC);
|
|
154
|
+
|
|
155
|
+
-- Index column order matters!
|
|
156
|
+
-- Rule: Equality filters ā Range filters ā Sort columns
|
|
157
|
+
|
|
158
|
+
-- Example queries that use this index:
|
|
159
|
+
-- 1. SELECT * FROM orders WHERE user_id = 123; ā
|
|
160
|
+
-- 2. SELECT * FROM orders WHERE user_id = 123 AND status = 'active'; ā
|
|
161
|
+
-- 3. SELECT * FROM orders WHERE user_id = 123 ORDER BY created_at DESC; ā
|
|
162
|
+
-- 4. SELECT * FROM orders WHERE status = 'active'; ā (doesn't start with user_id)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Query Performance Benchmarking
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
// scripts/benchmark-queries.ts
|
|
169
|
+
import { PrismaClient } from "@prisma/client";
|
|
170
|
+
import { performance } from "perf_hooks";
|
|
171
|
+
|
|
172
|
+
const prisma = new PrismaClient();
|
|
173
|
+
|
|
174
|
+
async function benchmarkQuery(
|
|
175
|
+
name: string,
|
|
176
|
+
query: () => Promise<any>,
|
|
177
|
+
iterations: number = 10
|
|
178
|
+
) {
|
|
179
|
+
const times: number[] = [];
|
|
180
|
+
|
|
181
|
+
for (let i = 0; i < iterations; i++) {
|
|
182
|
+
const start = performance.now();
|
|
183
|
+
await query();
|
|
184
|
+
const end = performance.now();
|
|
185
|
+
times.push(end - start);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const avg = times.reduce((a, b) => a + b, 0) / times.length;
|
|
189
|
+
const min = Math.min(...times);
|
|
190
|
+
const max = Math.max(...times);
|
|
191
|
+
|
|
192
|
+
console.log(`\n${name}:`);
|
|
193
|
+
console.log(` Avg: ${avg.toFixed(2)}ms`);
|
|
194
|
+
console.log(` Min: ${min.toFixed(2)}ms`);
|
|
195
|
+
console.log(` Max: ${max.toFixed(2)}ms`);
|
|
196
|
+
|
|
197
|
+
return { avg, min, max };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Compare queries
|
|
201
|
+
async function compareQueries() {
|
|
202
|
+
console.log("š Benchmarking queries...\n");
|
|
203
|
+
|
|
204
|
+
// Query 1: Original
|
|
205
|
+
const result1 = await benchmarkQuery("Original Query", async () => {
|
|
206
|
+
return prisma.$queryRaw`
|
|
207
|
+
SELECT u.*, COUNT(o.id) as order_count
|
|
208
|
+
FROM users u
|
|
209
|
+
LEFT JOIN orders o ON o.user_id = u.id
|
|
210
|
+
GROUP BY u.id
|
|
211
|
+
LIMIT 10
|
|
212
|
+
`;
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Query 2: Optimized
|
|
216
|
+
const result2 = await benchmarkQuery("Optimized Query", async () => {
|
|
217
|
+
return prisma.$queryRaw`
|
|
218
|
+
SELECT u.id, u.email, u.name,
|
|
219
|
+
(SELECT COUNT(*) FROM orders WHERE user_id = u.id) as order_count
|
|
220
|
+
FROM users u
|
|
221
|
+
LIMIT 10
|
|
222
|
+
`;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// Comparison
|
|
226
|
+
const improvement = (
|
|
227
|
+
((result1.avg - result2.avg) / result1.avg) *
|
|
228
|
+
100
|
|
229
|
+
).toFixed(1);
|
|
230
|
+
console.log(`\nš Improvement: ${improvement}% faster`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
compareQueries();
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Query Optimization Checklist
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
interface QueryOptimization {
|
|
240
|
+
query: string;
|
|
241
|
+
issues: string[];
|
|
242
|
+
recommendations: string[];
|
|
243
|
+
estimatedImprovement: string;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const optimizations: QueryOptimization[] = [
|
|
247
|
+
{
|
|
248
|
+
query: "SELECT * FROM orders WHERE user_id = $1",
|
|
249
|
+
issues: [
|
|
250
|
+
"Missing index on user_id",
|
|
251
|
+
"SELECT * fetches unnecessary columns",
|
|
252
|
+
],
|
|
253
|
+
recommendations: [
|
|
254
|
+
"CREATE INDEX idx_orders_user_id ON orders(user_id)",
|
|
255
|
+
"SELECT id, total, status instead of *",
|
|
256
|
+
],
|
|
257
|
+
estimatedImprovement: "90% faster",
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
query: "SELECT COUNT(*) FROM orders",
|
|
261
|
+
issues: ["Full table scan", "No WHERE clause filtering"],
|
|
262
|
+
recommendations: [
|
|
263
|
+
"Add WHERE clause to filter rows",
|
|
264
|
+
"Consider approximate count for large tables",
|
|
265
|
+
],
|
|
266
|
+
estimatedImprovement: "70% faster",
|
|
267
|
+
},
|
|
268
|
+
];
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Automated Slow Query Detection
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
// scripts/detect-slow-queries.ts
|
|
275
|
+
async function detectSlowQueries() {
|
|
276
|
+
// Enable slow query logging in PostgreSQL
|
|
277
|
+
await prisma.$executeRaw`
|
|
278
|
+
ALTER DATABASE mydb SET log_min_duration_statement = 100;
|
|
279
|
+
`;
|
|
280
|
+
|
|
281
|
+
// Query pg_stat_statements for slow queries
|
|
282
|
+
const slowQueries = await prisma.$queryRaw<any[]>`
|
|
283
|
+
SELECT
|
|
284
|
+
query,
|
|
285
|
+
calls,
|
|
286
|
+
total_exec_time / 1000 as total_time_seconds,
|
|
287
|
+
mean_exec_time / 1000 as mean_time_ms,
|
|
288
|
+
max_exec_time / 1000 as max_time_ms
|
|
289
|
+
FROM pg_stat_statements
|
|
290
|
+
WHERE mean_exec_time > 100 -- > 100ms
|
|
291
|
+
ORDER BY mean_exec_time DESC
|
|
292
|
+
LIMIT 20
|
|
293
|
+
`;
|
|
294
|
+
|
|
295
|
+
console.log("š Slow Queries Detected:\n");
|
|
296
|
+
slowQueries.forEach((q, i) => {
|
|
297
|
+
console.log(`${i + 1}. ${q.query.substring(0, 80)}...`);
|
|
298
|
+
console.log(` Calls: ${q.calls}`);
|
|
299
|
+
console.log(` Avg: ${q.mean_time_ms.toFixed(2)}ms`);
|
|
300
|
+
console.log(` Max: ${q.max_time_ms.toFixed(2)}ms\n`);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## Best Practices
|
|
306
|
+
|
|
307
|
+
1. **Always use EXPLAIN**: Understand query plans
|
|
308
|
+
2. **Index foreign keys**: Essential for joins
|
|
309
|
+
3. **Avoid SELECT \***: Fetch only needed columns
|
|
310
|
+
4. **Use composite indexes**: Multi-column queries
|
|
311
|
+
5. **Consider covering indexes**: Eliminate table access
|
|
312
|
+
6. **Batch operations**: Reduce round trips
|
|
313
|
+
7. **Monitor regularly**: Track slow queries
|
|
314
|
+
|
|
315
|
+
## Output Checklist
|
|
316
|
+
|
|
317
|
+
- [ ] EXPLAIN plan analyzed
|
|
318
|
+
- [ ] Missing indexes identified
|
|
319
|
+
- [ ] Query rewrite suggestions
|
|
320
|
+
- [ ] Performance benchmarks
|
|
321
|
+
- [ ] Before/after metrics
|
|
322
|
+
- [ ] Index creation scripts
|
|
323
|
+
- [ ] Slow query monitoring
|
|
324
|
+
- [ ] Optimization priority list
|