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
userstable 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
userstable get indexed for the full text search and this can be done using triggers on theuserstable to do it automatically. The triggers would look like:With all of this in place, you can now use the
users_ftstable to perform full text searches.So how do indexes on the
userstable affect theusers_ftstable? If you are only searching using theusers_ftstable, then indexes on theuserstable do not matter. I do not know how you plan on populating theusers_ftstable, but if you use triggers on theuserstable then the proposed indexes on theuserstable still do not matter. If you are manually keeping theusers_ftstable up to date, then the answer is that indexes on theuserstable 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 theuserstable at all - if you have any queries against theuserstable, then you may need supporting indexes.You also asked if there are any caveats to the approach of using the
users_ftstable for your queries - as long as you keep theusers_ftstable 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_ftstable). 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.