Backend Queries

Secure and optimize backend queries against SQL injection and N+1 problems.

Updated Jan 15, 2026
One-click install
npx skills add https://github.com/dpietersz/pietersz.me --skill backend-queries-dpietersz
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Backend Queries
Source: https://github.com/dpietersz/pietersz.me/tree/main/.claude/skills/backend-queries
Command: npx skills add https://github.com/dpietersz/pietersz.me --skill backend-queries-dpietersz

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Backend queries often suffer from SQL injection risks, N+1 query problems, and performance bottlenecks due to unparameterized inputs, missing joins, or improper data loading strategies. This skill provides guidelines and best practices to write secure, efficient queries across ORM and raw SQL contexts.

Core Features & Use Cases

  • Parameterized queries: Use parameterized statements to prevent SQL injection across ORM and raw SQL.
  • Eager loading & joins: Employ joins and eager loading to minimize N+1 queries and fetch related data efficiently.
  • Indexing & optimization: Recommend indexing strategies, query plan analysis, caching, and transaction-aware patterns to improve performance.
  • Use Case: Example: fetching a user and their related records in a single, well-parameterized query to avoid multiple round-trips.

Quick Start

Refactor a representative query to use parameterized statements and eager loading to prevent injection flaws and N+1 problems.

Frequently Asked Questions about Backend Queries

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 query problems in my ORM data access layer?

Fix N+1 query problems by employing joins and eager loading to fetch related data efficiently in a single query. This minimizes multiple database round-trips and significantly optimizes backend query performance across your services.

How do I prevent SQL injection when writing raw SQL queries?

Prevent SQL injection by using parameterized statements for safe query construction across both raw SQL and ORM contexts. Parameterization ensures user inputs are treated strictly as data values rather than executable code.

What is the best way to optimize slow backend queries and database bottlenecks?

Optimize slow backend queries by applying indexing strategies, analyzing query plans, and implementing caching. Transaction-aware patterns and proper data loading strategies also reduce performance bottlenecks in your repository patterns.

Does this backend query optimization apply to both ORM and raw SQL contexts?

Yes, this approach applies to ORM-based data access, raw SQL, and repository patterns across services and controllers. It covers parameterization, safe query construction, proper data loading, and performance monitoring uniformly.

How do I use eager loading and joins to optimize database queries?

Use eager loading and joins to fetch a record and its related data in a single, well-parameterized query. This approach avoids multiple round-trips and directly resolves inefficient data loading patterns in your backend.