Backend Queries

Optimize and secure database queries against SQL injection and performance issues.

Updated Mar 13, 2023
One-click install
npx skills add https://github.com/pdovhomilja/dovhomilja-cz --skill backend-queries-pdovhomilja
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Backend Queries
Source: https://github.com/pdovhomilja/dovhomilja-cz/tree/main/.claude/skills/backend-queries
Command: npx skills add https://github.com/pdovhomilja/dovhomilja-cz --skill backend-queries-pdovhomilja

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill ensures database queries are performant, secure, and efficient, preventing common issues like N+1 query problems, SQL injection attacks, and slow application responses, leading to a faster and more reliable backend.

Core Features & Use Cases

  • Performance Optimization: Use eager loading, proper indexing strategies, and selective column fetching to minimize database load.
  • Security: Implement parameterized queries to prevent SQL injection attacks and protect your data.
  • Efficient Data Fetching: Optimize WHERE clauses, JOINs, and ORDER BY statements for quick data retrieval.
  • Use Case: When fetching a list of users and their associated orders, this skill guides the AI to use eager loading to prevent N+1 queries, select only the necessary columns, and ensure the query is indexed for optimal performance, returning results quickly.

Quick Start

Write a database query to fetch all active products, including their categories, ensuring eager loading and selecting only the 'name', 'price', and 'category_name' fields for efficiency.

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?

Prevent SQL injection by using parameterized queries, which separate SQL logic from data values. Apply this across raw SQL and ORMs like Prisma, Sequelize, and ActiveRecord to ensure user input is never directly concatenated into query strings.

What causes N+1 query problems and how do I fix them?

N+1 query problems occur when fetching a parent record triggers separate queries for each related child record. Fix this by using eager loading to fetch related data in a single query, significantly reducing database load and improving response time.

How do I optimize database query performance for large datasets?

Optimize query performance by selecting only necessary columns, adding proper database indexes on frequently queried fields, using JOINs efficiently, and implementing result caching to avoid repeated identical queries against your database.

Can I use safe pagination and sorting with parameterized queries?

Yes, safe pagination and sorting work with parameterized queries by using parameters for LIMIT, OFFSET, and ORDER BY values, preventing injection attacks while maintaining query efficiency across ORMs and raw SQL.

Why should I set timeouts and transactions on database queries?

Timeouts prevent long-running queries from blocking resources and degrading application performance. Transactions ensure multiple related queries execute atomically, maintaining data consistency and preventing partial updates if errors occur.

Does this approach work with different database systems?

Yes, these optimization and security practices apply across database systems. Parameterized queries, indexing strategies, eager loading, and transaction patterns are supported in SQL databases and ORMs like Prisma, Sequelize, and ActiveRecord.