阅读full-text index文档

This commit is contained in:
asahi
2025-02-21 12:47:18 +08:00
parent c56c06555e
commit 6faa9ac410

View File

@@ -53,9 +53,14 @@
- [innodb全文检索示例](#innodb全文检索示例)
- [innodb full-text design](#innodb-full-text-design)
- [innodb full-text index tables](#innodb-full-text-index-tables)
- [辅助索引表](#辅助索引表)
- [辅助索引表auxiliary index table](#辅助索引表auxiliary-index-table)
- [table\_id hex](#table_id-hex)
- [index\_id hex](#index_id-hex)
- [公共索引表common index table](#公共索引表common-index-table)
- [innodb full-text index cache](#innodb-full-text-index-cache)
- [innodb\_ft\_cache\_size](#innodb_ft_cache_size-1)
- [innodb\_ft\_total\_cache\_size](#innodb_ft_total_cache_size)
- [full-text查询](#full-text查询)
# innodb索引与算法
@@ -573,7 +578,7 @@ select * from information_schema.innodb_tables where name like 'innodb_demo%'
| 1832 | innodb\_demo/fts\_000000000000071e\_00000000000005ad\_index\_5 | 33 | 8 | 106 | Dynamic | 0 | Single | 0 | 0 |
| 1833 | innodb\_demo/fts\_000000000000071e\_00000000000005ad\_index\_6 | 33 | 8 | 107 | Dynamic | 0 | Single | 0 | 0 |
#### 辅助索引表
#### 辅助索引表auxiliary index table
其中,以`innodb_demo/fts_000000000000071e_00000000000005ad_index`开头的6张表用于存储倒排索引并被称为`辅助索引表`
当新增的文档被分割为`token`时,每个独立的`word`(也可被称为`token`被插入到辅助索引表中随着word被插入的还有`postion``DOC_ID`
@@ -592,4 +597,51 @@ word按照`第一个字符的字符集排序权重`被排序,并且在六张
> 如果是`file per table tablespace`那么idnex table将会保存在其自己的tablespace中。
#### 公共索引表common index table
除了辅助索引表之外剩下的表被称为公共索引表用于处理删除和存储full-text index的状态。
公共索引表和辅助索引表区别如下:
- 辅助索引表辅助索引表用于存储倒排索引的内容其6张表都是针对索引的若一张表内拥有两个`fulltext`索引那么每个fulltext索引都会有其自己的6张辅助索引表
- 公共索引表:公共索引表是针对表的,不管一张表中存在多少`fulltext`索引,都只有一张公共索引表
即使在删除fulltext索引后common index table仍然会保留当删除fulltext索引后为该索引创建的`FTS_DOC_ID`字段也会被保留因为删除FTS_DOC_ID列需要对先前被索引的表进行重构。
公共索引表用于管理`FTS_DOC_ID`列:
- `fts_*_deleted``fts_*_deleted_cache`
- 该表用于保存`文档已被删除但是数据仍然没有从full-text index中移除`的文档idDOC_ID
- `fts_*_deleted_cache``fts_*_deleted`的内存保本
- `fts_*_being_deleted``fts_*_being_deleted_cache`
- 该表用于保存`文档已被删除并且文档数据正在从full-text index中被移除`的文档id(DOC_ID)
- `fts_*_deleted_cache``fts_*deleted`的内存版本
- `fts_*_config`
- 存储full-text index的内部状态
- 其会存储`FTS_SYNCED_DOC_ID`,用于标识`已经被转化并且刷新到磁盘中`的文档。当mysql应用发生崩溃并且重启恢复时`FS_SYNCED_DOC_ID`会标识还没有被刷新到磁盘中的文档。故而所有未被刷新到磁盘的文档都会被重新reparsed并且添加到full-text index cache中
#### innodb full-text index cache
当文档被插入时,其会被执行`tokenize`操作,之后得到的`word``word关联的数据`将会被插入到full-text index中。
- 在上述过程中,即使对于大小很小的文档,也会产生对`辅助索引表`的大量小插入,会并发访问辅助索引表,产生竞争
为了避免上述问题innodb使用full-text index cache来对index table insertions进行缓存。该内存缓存结构将会缓存插入操作直到cache被填满并将其批量刷新到磁盘即刷新到辅助索引表
> 可以在`information_schema.innodb_ft_index_cahce`来查看最近新插行的数据。
`通过cache来缓存插入并在cache满后批量刷新到磁盘`,该策略能够避免频繁访问辅助索引表,避免在插入和更新时并发访问带来的问题。
除此之外批量插入还能避免针对相同word的多次插入进而将重复条目最小化。
- 在使用innodb full-text index cache时对于相同word的插入将会被合并为一条entry并插入其不仅能提高插入性能同时也能令库中的辅助索引表尽可能的小。
##### innodb_ft_cache_size
`innodb_ft_cache_size`用于指定full-text index cache的大小针对每一张表大小的指定将会影响full-text index cache被刷新的频率。
##### innodb_ft_total_cache_size
`innodb_ft_total_cache_size`用于限制所有表`full-text index cache`大小。
##### full-text查询
full-text index cache中存储的信息和辅助索引表中相同。但是full-text cache中只存储近期插入的行。在执行查询操作时已经被刷新到磁盘的数据并并不会被重新带到缓存中。
对full-text中数据的查询如下
- 直接查询辅助索引表中的数据
- 查询full-text index cache中的数据
- 将辅助索引表中查询的数据和full-text index cache中查询的数据进行合并