Consider the following view
CREATE VIEW `my_view` AS
SELECT
a.id,
(SELECT
COUNT( b.id )
FROM
another_table b
WHERE
b.a_id = a.id AND b.other_column = 'ABC'
) AS counter
FROM
some_table a
some_table
has millions of rows and another_table
has an index on a_id
+ other_column
.
Now, consider the following SQL:
SELECT vw.*
FROM some_table a
LEFT JOIN my_view vw on vw.id = a.id
WHERE a.id = 12345
Can anyone tell me why this sql query is doing a full table scan on some_table
? The goal is to have a view that returns the number of rows from another_table
where the other_column
equals 'ABC'
.
If I replace LEFT JOIN my_view
in the query with LEFT JOIN ( the AS CLAUSE from the view )
, it does NOT do a full table scan and uses the a_id + other_column index.
I'm stumped. Any help is appreciated.