12

即使在阅读了很多关于 SQLite 全文索引的内容之后,出现了一个我在任何地方都没有看到答案的问题:

我已经有一个要使用全文索引搜索的表。我只会创建一个额外的虚拟表USING FTS3,或者USING FTS4然后将INSERT我的数据放入其中。

那么这是否总共使用了双存储?我可以像使用普通表一样使用这样的虚拟表,从而防止两次存储数据吗?

(我在 Android 上使用 SQLite,但这个问题可能适用于任何 SQLite 兼容平台上的使用。)

4

2 回答 2

13

尽管您确实找到了一些细节,但我将尝试提供详细的答案:

1. 那这样一共使用双存储吗?

是的,它确实。此外,它可能会使用更多的事件空间。例如,对于广为人知的Enron E-Mail Dataset 和 FTS3 示例,只需感受一下不同之处:

在此处输入图像描述

  • FTS3 表在磁盘上消耗大约 2006 MB,而普通表仅消耗 1453 MB

  • The FTS3 table took just under 31 minutes to populate, versus 25 for the ordinary table

Which makes the situation a bit unpleasant, but still full-text search worth it.

2. Can I use such a virtual table just like a normal table?

The short answer no, you can't. Virtual table is just a some kind of a View with several limitations. You've noticed several already.

Generally saying you should not use any feature which is seems to be unnatural for a View. Just a bare minimum required to let your application fully utilize the power of full-text search. So there will be no surprises later, with newer version of the module.

There is no magic behind this solution, it is just a trade-off between performance, required disk space and functionality.

Final conclusion

I would highly recommend to use FTS4, because it is faster and the only drawback is additional storage space needed.

Anyway, you have to carefully design virtual table taking into account a supplementary and highly specialized nature of such solution. In the other words, do not try to replace your initial table with the virtual one. Use both with a great care.

Update I would recommend to look through the following article: iOS full-text search with Core Data and SQLite. Several interesting moments:

  • The virtual table is created in the same SQLite database in wich the Core Data content resides. To keep this table as light as possible only object properties relevant to the search query are inserted.
  • SQLite 实现提供了 Core Data 所没有的东西:全文搜索。除此之外,它的执行速度比类似的 Core Data 查询快了近 10%,效率至少提高了 660%(内存)。
于 2013-08-26T13:20:19.620 回答
4

我刚刚发现了虚拟表的主要区别,它似乎取决于您的使用情况,单个表是否足以满足您的需求。

  • 不能在虚拟表上创建触发器。

  • 不能在虚拟表上创建额外的索引。(虚拟表可以有索引,但必须内置到虚拟表实现中。不能使用 CREATE INDEX 语句单独添加索引。)

  • 不能对虚拟表运行 ALTER TABLE ... ADD COLUMN 命令。

因此,如果您需要在表上创建另一个索引,则需要使用两个表。

于 2012-09-20T07:11:03.137 回答