SQLite Select Query Performance Tuning
UNION
removes duplicate rows that appear in both subqueries, and therefore must create a temporary table for the result.
If you can guarantee that the subqueries are distinct, or if you don't care about those duplicates, use UNION ALL
instead.
(In your case, the sorting by the non-indexed value int3
requires a temporary table anyway.)
To optimize the subqueries, run them with EXPLAIN QUERY PLAN
to check whether they use indexes.
You can use LIKE
if you ensure that your indexes are declared correctly; see LIKE
optimization.
See Query Planning for an explanation of how to construct indexes. Your two indexes seem to be optimal.