So let's say I have two tables: users
and users_fts
where users_fts
being the search table. So users
has two fields name
and surname
.
Normally I would create index for both of em'. But since I have users_fts
should I need to create index for name
and surname
in users
? And is there any caveat of using users_fts
to perform all queries instead of using the main table users
?
SQLite provides full text search, and I am assuming that is what you are using from your table name. I will show the sample code using FTS5 but you can adapt it backward if you need to. If you have a
users
table something like:then you made your full text search table using something like this:
At this point you we have to make sure that the records in the
users
table get indexed for the full text search and this can be done using triggers on theusers
table to do it automatically. The triggers would look like:With all of this in place, you can now use the
users_fts
table to perform full text searches.So how do indexes on the
users
table affect theusers_fts
table? If you are only searching using theusers_fts
table, then indexes on theusers
table do not matter. I do not know how you plan on populating theusers_fts
table, but if you use triggers on theusers
table then the proposed indexes on theusers
table still do not matter. If you are manually keeping theusers_fts
table up to date, then the answer is that indexes on theusers
table might affect performance. Most people I know use the trigger approach, and that is what I do which let's you forget about manually maintaining the full text search and you get the added benefit that you can ignore indexes on the source table in regards to populating the full text search. Remember though this is the scenario where you are not querying theusers
table at all - if you have any queries against theusers
table, then you may need supporting indexes.You also asked if there are any caveats to the approach of using the
users_fts
table for your queries - as long as you keep theusers_fts
table up to date, then there is no drawback to this approach. If you need full text search features and ranking, this is a very convenient approach baked into SQLite. It will require more storage space, but you can minimize that impact by using an external content table (I showed this when I created theusers_fts
table). You can read some details about it in section 4.4.2 in the FTS5 extension documentation at https://www.sqlite.org/fts5.htmlThis approach works well for full text search capabilities and as long as you maintain the indexes, it should work well and provide you with more capabilities for searching as well as ranking. In my experience, most full text searches are faster than what you could do using standard SQL functions and operators (such as
LIKE
, etc.) and are far more powerful.