lab/note/资源/数据库/SQL/索引.md
张育新 be711334fb docs(note): 添加笔记目录及 gitignore 规则
- 新增 note 目录包含 AI、SQL、Golang、网络等多领域学习笔记
- 更新 .gitignore 添加 note/assets/ 和 note/项目/ 忽略规则
2026-07-10 09:24:40 +08:00

45 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## B-Tree
**适用场景**
| 场景 | 示例 |
| :------- | :-------------------------- |
| 等值查询 | WHERE user_id = 100 |
| 范围查询 | WHERE age BETWEEN 20 AND 30 |
| 排序优化 | ORDER BY created_at DESC |
| 前缀匹配 | WHERE name LIKE '张%' |
**不适用场景**
- LIKE '%关键词%'(后缀/中间模糊匹配)
- 全文搜索
- 数组包含查询
## GIN
**结构特点**
- 倒排索引Inverted Index 机制:从"内容"映射到"行"
- 索引的是复合值内部的独立元素
- 索引构建较慢,但查询性能极佳
**适用场景**
> `@>`表示包含、`<@`表示被包含、`&&`表示重叠、`@@`表示全文搜索匹配、`?`表示JSONB 键存在
| 场景 | 示例 |
| :----------- | :----------------------------------------------------- |
| 全文搜索 | WHERE to_tsvector(content) @@ to_tsquery('postgresql') |
| 数组包含 | WHERE tags @> ARRAY['java', 'sql'] |
| JSONB 查询 | WHERE data @> '{"status": "active"}' |
| 多值元素搜索 | WHERE arr && ARRAY[1, 2] |