Domanda

PRIMARY KEY(col1, col2)

creates a index called "sqlite_autoindex_mytable_1". I saw it in "SQlite Expert". And in the "fields to index" box it shows both col1 and col2.

In this post: https://dba.stackexchange.com/a/14260 it says that I have to create separate indexes for col2 if I want to use col2 in JOIN queries without col1.

So I would need to add:

CREATE INDEX "myidx" ON "mytable"("col2");

If I have this query:

SELECT t2.* FROM mytable as t1 
INNER JOIN mytable2 as t2 ON t1.col2 = t2.id

do I still need a index for col2? I'm not using col1 in it.

And what about this query:

SELECT t2.* FROM mytable as t1 
INNER JOIN mytable2 as t2 ON t1.col2 = t2.id WHERE t1.col1 = x

Here I'm using col1, but in the where clause. Does it still need the col2 index?

È stato utile?

Soluzione

  • In SQLite, joins are implemented as nested loop joins, i.e., SQLite goes through all (maybe filtered) records of one table, and for each one, looks up the matching record(s) in the other table. Which of the two joined tables is chosen as the outer or the inner one depends on which lookup is estimated to be faster.
  • In a query, SQLite uses at most one index per table.
  • A multi-column index can be used for lookups on a subset of its columns only if all the leftmost columns are used. For example, your col1,col2 index can be used for lookups that use both col1 and col2, or for lookups that use only col1.

In your first query, the two-column index cannot be used for the lookup on col2. If the column id of the other table has an index, SQLite will just use that table as the inner table of the loop. If id is not indexed either, SQLite is likely to create a temporary index for this query.

In your second query, SQLite is likely to use t1 as the outer table because the WHERE filter will reduce the number of records that must be looked up in the other table. The two-column index can be used to search for matching col1 records first; then each of those records is joined with t2.

To check what indexes (if any) are used by a query, execute EXPLAIN QUERY PLAN.

Altri suggerimenti

You better always create an explict index on needed columns.

Composite indexes may speed up single column search, but requirements must be met!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top