optimalize

A set of rules for improving SQL queries, database indexes, schemas, and advanced MySQL or MariaDB patterns. It covers query structure, pagination, eager loading, transactions, prepared statements, and execution-plan checks.

In plain words
What is it for?
Use it when changing queries, indexes, schemas, or database-heavy application code. It helps identify N+1 queries, inspect plans with EXPLAIN, choose indexes, and keep transactions efficient.
Why use it?
It helps prevent slow queries, excessive database work, unsafe SQL construction, and unnecessary locking. It also guides index choices so filtering, joining, sorting, and grouping can use the database efficiently.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/pekral/cursor-rules/optimalize
Clone the repo
git clone --depth 1 https://github.com/pekral/cursor-rules
Per session 7,339 This file is loaded in full into every session.
When invoked 7,339 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.07339 $0.07339
Opus 5 $0.03669 $0.03669
Sonnet 5 $0.01468 $0.01468
Haiku 4.5 $0.00734 $0.00734

Measured yesterday against content hash a80d99fef509, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

optimalize scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/sql/optimalize.mdc · 332 lines

How it starts

The opening of the file, as written. The whole thing — 332 lines — stays where its author put it; the contents beside it link to each section on GitHub.

General

  • No N+1: use with(), load(), or JOINs
  • SARGable WHERE clauses (no functions on indexed columns)
  • Seek pagination over OFFSET
  • Never SELECT *; fetch only needed columns
  • Use EXPLAIN for new or changed queries: avoid type ALL, high rows, Using filesort, Using temporary
  • Prepared statements with bound parameters; never concatenate user input into SQL
  • Push filtering, sorting, aggregation into SQL; avoid doing it in application code
  • Refactor or split very complex joins when EXPLAIN shows poor plans; when profiling use slow query log and prioritize frequent or longest queries
  • Indexes: index columns used in WHERE, JOIN, ORDER BY, GROUP BY; composite index order must match query filter/sort; avoid low-cardinality-only indexes; use covering indexes when a query can be satisfied from the index; drop unused or redundant indexes when changing schema; aim for 3–5 well-chosen indexes per table; prefer parallel index creation on large tables
  • Transactions: keep transactions short; avoid holding locks during app logic; batch writes in single transactions where appropriate; reduce lock contention via batching and suitable isolation levels; use SHOW ENGINE INNODB STATUS to diagnose lock waits when investigating issues
  • Index JOIN columns on both tables; composite index order matters (left-to-right)

Query Analysis

  • Run EXPLAIN on every new or modified query.
  • Flag: type ALL, high rows, Using filesort, Using temporary.
  • Use EXPLAIN ANALYZE for actual vs estimated row counts.
  • Check slow query log — prioritize frequent or longest-running queries.
  • Apply one optimization at a time; measure before and after.

Performance Non-Regression on Query Changes

Whenever a query is refactored or changed (Eloquent / query-builder rewrite, raw-SQL edit, added / removed JOIN / WHERE / ORDER BY / GROUP BY / subquery / eager load, pagination change, index-driven rewrite, or a refactor that moves the query into another layer), the changed query must be at least as fast as the original — ideally faster. Never ship a query change that is slower without a documented justification.

Read the full file on GitHub · 332 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 332 lines · 7,339 tokens per session scan A a80d99fef509

Subscribe to this mod's changes

optimalize is a cursor rule published in the GitHub repository pekral/cursor-rules (6 stars, last pushed 6d ago), licensed MIT. It adds 7,339 tokens to every session, about $0.0367 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.