Shaun Thomas发布了一篇深入解析PostgreSQL中TOAST(The Oversized-Attribute Storage Technique)机制工作原理及其对数据库性能影响的文章 1。作者通过实验对比了使用TOAST存储大字段与将其分离到独立表后的读写效率,指出虽然TOAST增加了读取复杂数据时的开销,但显著减小了主表堆大小,从而加速了不涉及大字段的常规操作 1。最终建议在高负载系统中将超大列拆分至辅助表以优化整体性能 1。
TOAST默认针对TEXT、BYTEA、JSONB等可变长类型,当压缩后数据超过约2KB(toast_tuple_target)时触发 1。在示例实验中,包含TOAST的表大小为47 MB,而不使用TOAST的表大小为318 MB 1。
在具体查询性能方面,全表扫描特定大字段时,TOAST表需要更多缓冲区读取(288127 vs 40754),导致查询变慢 1。相反,简单计数或分页查询在不访问TOAST列时,TOAST表因堆更小且页密度更高而表现更快,如count耗时26ms,而未使用TOAST时耗时91ms 1。此外,PostgreSQL 14支持选择lz4压缩算法,相比pglz,lz4在插入大量数据时速度提升显著,耗时从52秒降至1.7秒 1。
最佳实践建议是将经常一起访问但体积巨大的列拆分到独立表中,并通过LEFT JOIN视图来保持应用层兼容性 1。
Database expert Shaun Thomas has published an analysis of the Oversized-Attribute Storage Technique (TOAST) in PostgreSQL, detailing how the system manages large data fields and its impact on overall performance 1. The mechanism automatically targets variable-length types such as TEXT, BYTEA, and JSONB, activating when compressed data exceeds approximately 2KB, a threshold defined by the toast_tuple_target parameter 1. By separating oversized attributes from the main table rows, TOAST significantly reduces the size of the primary heap, although this architectural choice introduces specific trade-offs for different types of database operations 1.
Thomas conducted experiments to quantify these effects, demonstrating that a table utilizing TOAST had a total size of 47 MB compared to 318 MB for a similar table without it 1. While scanning entire tables containing large fields incurs higher costs due to increased buffer requirements—specifically requiring 288,127 buffers versus 40,754 in the non-TOAST scenario—operations that do not access these columns benefit from improved speed 1. For instance, simple counting queries ran faster on the TOAST-enabled table, completing in 26 milliseconds compared to 91 milliseconds for the larger heap, owing to better page density 1. Additionally, the article highlights that PostgreSQL 14 supports the LZ4 compression algorithm, which offers substantial insertion speed improvements over the default PGLZ, reducing processing time from 52 seconds to just 1.7 seconds for large datasets 1. Based on these findings, Thomas recommends splitting frequently accessed but voluminous columns into separate auxiliary tables in high-load systems, using LEFT JOIN views to maintain application compatibility 1.
评论
还没有评论,欢迎留下第一条。