Backend Queries

Resolve insecure and inefficient SQL, ORM, and query-builder database queries.

6|1|Updated Nov 12, 2022
One-click install
npx skills add https://github.com/coreyja/coreyja.com --skill backend-queries-coreyja
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Backend Queries
Source: https://github.com/coreyja/coreyja.com/tree/main/.claude/skills/backend-queries
Command: npx skills add https://github.com/coreyja/coreyja.com --skill backend-queries-coreyja

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill optimizes database interactions by guiding the creation of secure, performant, and efficient queries, preventing N+1 problems and SQL injection vulnerabilities.

Core Features & Use Cases

  • Performance Optimization: Guides the use of proper indexing, eager loading, and query optimization techniques to speed up data retrieval.
  • Security & Integrity: Ensures parameterized statements are used to prevent SQL injection and transactions for data consistency.
  • Use Case: When fetching a list of products with their associated categories, use this skill to write an optimized query that avoids N+1 problems by eager loading categories and uses parameterized statements for any search filters.

Quick Start

Using the Backend Queries skill, optimize the 'getRecentOrders' function to eager load customer details and ensure the query uses parameterized statements for the date range filter.

Frequently Asked Questions about Backend Queries

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

FAQPage Schema
How do I prevent SQL injection in database queries?

SQL injection prevention requires parameterized queries, which separate SQL code from user input. Use parameterized statements instead of string concatenation to safely pass filters and search terms to your database, protecting against malicious input across SQL, ORM, and query-builder code.

What is an N+1 query problem and how do I fix it?

An N+1 problem occurs when fetching a list triggers separate queries for each item's related data. Fix it by eager loading related objects in a single query—for example, fetch products with their categories in one call instead of looping queries, dramatically improving performance.

How do I optimize slow database queries?

Optimize queries by avoiding SELECT *, using explicit column selection, adding proper indexing, eager loading related data, and setting query timeouts. These techniques reduce data transfer, disk I/O, and unnecessary processing while preventing runaway queries.

When should I use transactions in database operations?

Use transactions to ensure data consistency when performing multiple related updates across tables. Transactions guarantee all changes succeed together or all roll back, preventing partial updates that leave data in an invalid state.

What's the best way to handle large result sets from database queries?

Handle large result sets using pagination with limit and offset parameters, combined with caching strategies to avoid repeated full-table scans. This reduces memory usage and improves response times for data-intensive applications.

How do joins and query structure affect database performance?

Joins combine related tables efficiently, but improper joins can cause performance issues. Use safe joins with explicit join conditions, avoid unnecessary joins, and combine with proper indexing and column selection to maintain fast retrieval.