database-algorithm-rules

database-algorithm-rules is a cursor rule for coding agents from holtwood/awesome-cursorrules-zh. It costs 3 tokens per session (738 once invoked), scanned A, original, MIT.

Database algorithm rules guide Python applications that store and retrieve data, including containerized services. They cover schema design, indexes, query efficiency, transactions, connection pools, caching, retries, and migrations.

In plain words
What is it for?
They help design models, optimize queries, process large datasets, manage connections and transactions, cache results, retry safe operations, and version database changes.
Why use it?
They reduce slow queries, excessive database load, inconsistent updates, and deployment differences between environments.

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/holtwood/awesome-cursorrules-zh/database-algorithm-rules
Clone the repo
git clone --depth 1 https://github.com/holtwood/awesome-cursorrules-zh

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for database-algorithm-rules

README.md
[![agentmods](https://agentmods.dev/badge/rules/holtwood/awesome-cursorrules-zh/database-algorithm-rules.svg)](https://agentmods.dev/rules/holtwood/awesome-cursorrules-zh/database-algorithm-rules)
Your own site
<a href="https://agentmods.dev/rules/holtwood/awesome-cursorrules-zh/database-algorithm-rules"><img src="https://agentmods.dev/badge/rules/holtwood/awesome-cursorrules-zh/database-algorithm-rules.svg" alt="Measured on agentmods" height="20"></a>
Per session 3 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 738 The whole file, excluding the scripts and references it only reads on demand.
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.00003 $0.00738
Opus 5 $0.00002 $0.00369
Sonnet 5 $0.00001 $0.00148
Haiku 4.5 $0.00000 $0.00074

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

Security

Grade A, and why

database-algorithm-rules 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.

docs/rules/devops/python-containerization/database-algorithm-rules.mdc · 51 lines

What it actually says

数据库算法规则

本规则集定义了在 Python 容器化应用中与数据库交互时应遵循的算法和设计原则,旨在确保数据操作的效率、可靠性和可扩展性。

1. 数据模型设计

  • 范式化与反范式化: 根据业务需求和查询模式,权衡数据库范式化(减少数据冗余)和反范式化(优化读取性能)的程度。
  • 索引策略: 为频繁查询的列创建合适的索引,包括单列索引、复合索引和全文索引。避免过度索引。
  • 数据类型选择: 选择最合适的数据类型,以优化存储空间和查询性能。

2. 查询优化

  • 避免 N+1 查询: 在 ORM 中使用 select_relatedprefetch_related 来减少数据库查询次数。
  • 批量操作: 对于大量数据的插入、更新或删除,使用批量操作而非逐条操作,减少数据库往返次数。
  • 分页查询: 对于大数据集,始终使用分页查询,避免一次性加载所有数据导致内存溢出和性能下降。
  • 避免全表扫描: 优化查询语句,确保能够利用索引,避免不必要的全表扫描。

3. 事务管理

  • 原子性: 确保一组相关的数据库操作作为一个原子单元执行,要么全部成功,要么全部失败。
  • 隔离级别: 根据业务需求选择合适的事务隔离级别,以平衡数据一致性和并发性能。
  • 短事务: 尽量保持事务的简短,减少锁的持有时间,提高并发性。

4. 连接管理

  • 连接池: 使用数据库连接池来管理数据库连接,避免频繁地建立和关闭连接,提高效率。
  • 连接复用: 尽可能复用现有连接,减少资源消耗。

5. 缓存策略

  • 读写分离: 对于读多写少的应用,可以考虑读写分离,将读请求分发到只读副本。
  • 应用层缓存: 缓存频繁访问的、不经常变动的数据到应用内存或 Redis 等缓存系统中,减少数据库负载。
  • 缓存失效策略: 设计合理的缓存失效策略(如 LRU、TTL、写穿透、写回),确保数据一致性。

6. 错误处理与重试

  • 数据库连接错误: 妥善处理数据库连接中断、超时等错误。
  • 幂等性: 设计数据库操作为幂等的,以便在网络波动或服务重启时可以安全地重试。
  • 指数退避: 对于可重试的错误,采用指数退避策略进行重试。

7. 数据库迁移

  • 版本控制: 使用数据库迁移工具(如 Alembic, Django Migrations)来管理数据库 schema 的版本控制和变更。
  • 自动化: 自动化数据库迁移过程,确保开发、测试和生产环境的数据库结构一致。
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 · 51 lines · 3 tokens per session scan A a324b5662210

Subscribe to this mod's changes

database-algorithm-rules is a cursor rule published in the GitHub repository holtwood/awesome-cursorrules-zh (232 stars, last pushed 1mo ago), licensed MIT. It adds 3 tokens to every session and 738 once invoked, about $0.0000 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-09-03.