首页 / 资讯中心 / 文章详情

supabase-postgres-best-practices - advanced-jsonb-indexing

supabase-postgres-best-practices - advanced-jsonb-indexing ★ FEATURED ARTICLE
title: 为高效查询给 JSONB 列建索引impact: 中impactDescription: 正确的索引让 JSONB 查询快 10-100 倍tags: jsonb, gin, indexes, json为高效查询给 JSONB 列建索引没有索引的 JSONB 查询会扫描整张表。对包含查询使用 GIN 索引。错误做法JSONB 无索引createtableproducts(idbigintprimarykey,attributes jsonb);-- 每次查询都是全表扫描select*fromproductswhereattributes {color: red};select*fromproductswhereattributes-brandNike;正确做法JSONB 的 GIN 索引-- 用于包含运算符, ?, ?, ?|的 GIN 索引createindexproducts_attrs_ginonproductsusinggin(attributes);-- 现在包含查询使用索引select*fromproductswhereattributes {color: red};-- 对于特定键的查找使用表达式索引createindexproducts_brand_idxonproducts((attributes-brand));select*fromproductswhereattributes-brandNike;选择正确的运算符类-- jsonb_ops默认支持所有运算符索引更大createindexidx1onproductsusinggin(attributes);-- jsonb_path_ops仅支持 运算符但索引小 2-3 倍createindexidx2onproductsusinggin(attributes jsonb_path_ops);参考JSONB 索引
阅读完成 · 觉得有帮助?
咨询建站