I have read that joins are better than subqueries.
But
EXPLAIN QUERY PLAN
SELECT Queue.Id, NULL
FROM Queue
INNER JOIN LastQueue
ON Queue.Id=LastQueue.Id
gives
Array
(
[0] => Array
(
[selectid] => 0
[order] => 0
[from] => 0
[detail] => SCAN TABLE Queue (~1000000 rows)
)
[1] => Array
(
[selectid] => 0
[order] => 1
[from] => 1
[detail] => SEARCH TABLE LastQueue USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
)
)
while
EXPLAIN QUERY PLAN
SELECT Queue.Id, NULL
FROM Queue
WHERE (SELECT 1 FROM LastQueue WHERE Queue.Id=LastQueue.Id) IS NOT NULL
gives
Array
(
[0] => Array
(
[selectid] => 0
[order] => 0
[from] => 0
[detail] => SCAN TABLE Queue (~500000 rows)
)
[1] => Array
(
[selectid] => 0
[order] => 0
[from] => 0
[detail] => EXECUTE CORRELATED SCALAR SUBQUERY 1
)
[2] => Array
(
[selectid] => 1
[order] => 0
[from] => 0
[detail] => SEARCH TABLE LastQueue USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
)
)
I think I have to multiply the number of rows to get an idea of cost. I am right?
Then,
- Using join: 1000000*1
- Using subquery: 500000*1*1
Then, is the subquery is faster than join?
Is there a good tutorial to learn how to understand EXPLAIN
/EXPLAIN QUERY PLAN
results?
And why does it say that SCAN TABLE Queue is ~1000000 and ~500000, when that table has 76 rows?