patternMinor
does a subquery pull the entire table?
Viewed 0 times
thepullentiresubquerydoestable
Problem
i am attempting to run a subquery in a table:
My main question is, since that subquery called on it's own would pull the entire time, does it also pull the entire time while it's in a subquery, even though the WHERE condition on the parent query is only pulling just a few rows of the actual database?
I suppose I could put the where query inside the subquery, but its a pretty complex where query that involves other tables.
SELECT t1.*, t2.* FROM t1
LEFT JOIN (SELECT * FROM table2 GROUP BY col1) AS t2 on t1.col1 = t2.col1
WHERE ...My main question is, since that subquery called on it's own would pull the entire time, does it also pull the entire time while it's in a subquery, even though the WHERE condition on the parent query is only pulling just a few rows of the actual database?
I suppose I could put the where query inside the subquery, but its a pretty complex where query that involves other tables.
Solution
It shouldn't. It depends on what the system can simplify the query to.
You can demonstrate this very easily by considering the plan for queries such as:
The two should be identical.
You can demonstrate this very easily by considering the plan for queries such as:
SELECT *
FROM YourTable
WHERE ID < 5;
SELECT *
FROM (SELECT * FROM YourTable) as t
WHERE ID < 5;The two should be identical.
Code Snippets
SELECT *
FROM YourTable
WHERE ID < 5;
SELECT *
FROM (SELECT * FROM YourTable) as t
WHERE ID < 5;Context
StackExchange Database Administrators Q#33426, answer score: 3
Revisions (0)
No revisions yet.