技巧

Notion 实现 PostgreSQL 查询流水线提升性能

精选理由

Notion 工程师分享了他们如何通过 PostgreSQL 查询流水线将性能提升 50%,具体代码示例很实用。

Notion 工程师 Jake 实现了 PostgreSQL 扩展查询协议流水线技术。该技术将查询网络往返次数从 N 次减少到 1 次(读)或 2 次(写)。Notion 的查询性能因此提升了约 50%。Notion 在事务中运行大量查询,配合 pgbouncer 事务池使用效果更佳。

原文 · Akshay Kothari

i enjoy posting about new features. i enjoy posting about performance improvement more. Jake 🎉 @jitl implemented postgres extended query protocol pipelining, makes queries at notion go about 50% faster why? notion runs many queries in transactions so we can use pgbouncer txn pooling + SET LOCAL; many reads look like this: await query(sql`BEGIN; SET LOCAL search_path = ...`) const result = await query(sql`SELECT ...`) // rare txns sometimes do more reads await query(sql`COMMIT`) pipelining which reduces network round trips from N → 1 for reads and N → 2 for writes (tho maybe its fine to pipeline COMMIT there also) const results = await query(new ExtendedQueryPipeline([ sql`BEGIN`, sql`SET LOCAL ...`, sql`SELECT ...`, sql`COMMIT`, ])) 🔗 View Quoted Tweet 💬 2 🔄 1 ❤️ 22 👀 1533 📊 4 ⚡