stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

Joining a Virtual Table (FTS) and a regular Table takes a lot of time to execute #1185

Open gholias opened 1 year ago

gholias commented 1 year ago

I'm trying to join query between my Cars regular Table and my Makers Virtual Table for FTS

This is the makers Virtual Table

let markersTable = VirtualTable("makers")
let idColumn = Expression<String>("id")
let name = Expression<String>("name")
let text = Expression<String>("text")
let code = Expression<String>("code")

let config = FTS5Config()
      .column(idColumn)
      .column(name, [.unindexed])
      .column(code)
      .column(text)

and this is the Cars regular table

let carsTable = Table("Cars")
let idColumn = Expression<String>("id")
let name = Expression<String>("name")
let description = Expression<String>("description")
let makersCode = Expression<String>("makersCode")

The tables can be joined by

let query = carsTable
      .limit(1)
      .join(markersTable, on: makersCode == markersTable[code])

This works but takes a long time and I'm wondering what is the best way to make this join be faster?

I did some tests before using the FTS Virtual Table and if both tables are regular tables, the join is a lot faster.

Any help is appreciated.

nathanfallet commented 1 year ago

Try to add an index on code and markersCode. It will help SQLite to find data faster. Also, please close the issue if it solves your problem.