I have two tables in my sqlite database t1 and t2. t1 has two columns a and b. t2 has one column a same as in t1. I've build indexes for every column in both tables. I want to select all rows from table t1 where t1.a exists in table t2. So I've written a query:
select t1.a, t1.b from t1 where t1.a in(select t2.a from t2) limit 10000000;
I'm using limit to select all rows, not first 100.
Table t1 contains 6 000 000 rows, table t2 100 000 rows. This query executes 1400ms.
But when I try to select only t1.a with this query:
select t1.a from t1 where t1.a in(select t2.a from t2) limit 10000000;
it tooks only 86ms.
Explain query plan:
SEARCH TABLE csv USING COVERING INDEX iin_idx (iin=?)
USING INDEX sqlite_autoindex_input_1 FOR IN-OPERATOR
Questions:
- Why does sqlite behaves so?
- What is the difference in selecting one column or several?
- Is it possible to speed up this query?
- Why does sqlite uses autoindex instead of created one?
I want to populate t1 table with 155 000 000 rows, so the query time will grow significantly.