技能 query-builder
🗃️

query-builder

安全 🌐 网络访问⚙️ 外部命令

建立優化的資料庫查詢

撰寫高效的資料庫查詢需要對 SQL 和 NoSQL 模式有深入的了解。此技能可生成具有適當參數化的優化查詢,並解釋執行策略以獲得更好的效能。

支持: Claude Codex Code(CC)
⚠️ 68
1

下载技能 ZIP

2

在 Claude 中上传

前往 设置 → 功能 → 技能 → 上传技能

3

开启并开始使用

测试它

正在使用“query-builder”。 Get all products with their category name where the product is in stock

预期结果:

  • SELECT p.id, p.name, p.price, c.name as category_name
  • FROM products p
  • INNER JOIN categories c ON p.category_id = c.id
  • WHERE p.stock_count > 0 AND p.active = true
  • ORDER BY p.name ASC

正在使用“query-builder”。 Create a MongoDB aggregation to calculate daily revenue for the last 7 days

预期结果:

  • db.orders.aggregate([
  • { $match: { status: 'completed', createdAt: { $gte: 7 days ago } } },
  • { $group: { _id: { $dateToString: { format: '%Y-%m-%d', date: '$createdAt' } }, dailyRevenue: { $sum: '$totalAmount' }, orderCount: { $sum: 1 } } },
  • { $sort: { _id: -1 } }
  • ])

正在使用“query-builder”。 Write a TypeORM query to find users with their order statistics

预期结果:

  • const stats = await dataSource.getRepository(User)
  • .createQueryBuilder('user')
  • .leftJoin('user.orders', 'order')
  • .select('user.id', 'userId')
  • .addSelect('user.username', 'username')
  • .addSelect('COUNT(order.id)', 'orderCount')
  • .addSelect('COALESCE(SUM(order.totalAmount), 0)', 'totalSpent')
  • .groupBy('user.id')
  • .getRawMany();

质量评分

38
架构
100
可维护性
85
内容
20
社区
100
安全
78
规范符合性

你能构建什么

撰寫複雜的 JOIN 查詢

為報表功能生成具有適當過濾和聚合的多表聯結

建立聚合管線

為分析和資料轉換建立 MongoDB 聚合管線和視窗函數

生成 ORM 查詢

使用 Prisma、TypeORM 或 Mongoose 建立具有適當關聯的型別安全資料庫查詢

试试这些提示

基本查詢請求
Get all active users with their email addresses created in the last 30 days
聯結查詢
Get users with their orders including order items and product details for completed orders
ORM 查詢
Write a Prisma query to find top 10 customers by total spending with their order counts
優化審查
Optimize this query: SELECT * FROM orders WHERE YEAR(created_at) = 2024 AND status = 'active'

最佳实践

  • 始終使用帶有佔位符(如 $1 或 ?)的參數化查詢,而非字串插值
  • 僅指定您需要的欄位,而非使用 SELECT *
  • 在 WHERE、JOIN 和 ORDER BY 子句中使用的欄位上新增適當的索引

避免

  • 使用字串串接來建立查詢 - 這會產生 SQL 注入漏洞
  • 在生產環境中執行沒有 LIMIT 的查詢 - 始終對大型結果集進行分頁
  • 在 WHERE 子句中對已索引的欄位使用函數 - 這會阻止索引的使用

常见问题

支援哪些資料庫?
支援 PostgreSQL、MySQL、SQLite、SQL Server、MongoDB、DynamoDB、Redis 和 Cassandra 及其各自的查詢語法。
哪些 ORM 可與此技能搭配使用?
Prisma、TypeORM、Sequelize、SQLAlchemy、Django ORM 和 Mongoose 都支援,並提供範例模式。
此技能會執行查詢嗎?
否。此技能生成查詢範例。您必須在自己的資料庫環境中複製並執行查詢。
使用此技能時我的資料安全嗎?
是的。此技能僅生成文字 - 它無法存取您的資料庫、憑證或任何系統資源。
如何獲得更好的查詢建議?
提供您的表格名稱、欄位和關聯。包含任何特定的過濾或排序要求以獲得最佳結果。
這與原始 SQL 生成有何不同?
此技能提供原始 SQL 和 ORM 替代方案。ORM 版本使用您現有的模型直接整合到您的程式碼中。

开发者详情

文件结构

📄 SKILL.md