supabase-postgres-best-practices - query-index-types
title: Choose the Right Index Type for Your Dataimpact: HIGHimpactDescription: 10-100x improvement with correct index typetags: indexes, btree, gin, gist, brin, hash, index-types为你的数据选择正确的索引类型不同的索引类型擅长不同的查询模式。默认的 B-tree 并不总是最优选择。错误做法对 JSONB 包含查询使用 B-tree-- B-tree 无法优化包含运算符createindexproducts_attrs_idxonproducts(attributes);select*fromproductswhereattributes {color: red};-- 全表扫描——B-tree 不支持 运算符正确做法JSONB 使用 GIN-- GIN 支持 , ?, ?, ?| 运算符createindexproducts_attrs_idxonproductsusinggin(attributes);select*fromproductswhereattributes {color: red};索引类型指南-- B-tree默认, , , BETWEEN, IN, IS NULLcreateindexusers_created_idxonusers(created_at);-- GIN数组、JSONB、全文搜索createindexposts_tags_idxonpostsusinggin(tags);-- GiST几何数据、范围类型、最近邻KNN查询createindexlocations_idxonplacesusinggist(location);-- BRIN大型时序表体积小 10-100 倍createindexevents_time_idxoneventsusingbrin(created_at);-- Hash仅等值比较对 比 B-tree 略快createindexsessions_token_idxonsessionsusinghash(token);参考索引类型