I was wondering if a SELECT
of a SELECT
incurs any additional performance, or if the database is smart enough to optimize this.
For example, I have some selects I do like the following:
SELECT a.year, b.month, c.day, totalVolume from
(SELECT DATEPART(year, transactionTime) as year,
DATEPART(month, transactionTime) as month,
DATEPART(day, transactionTime) as day,
SUM(volume) as totalVolume FROM DOW30
GROUP BY DATEPART(year, transactionTime),
DATEPART(month, transactionTime),
DATEPART(day, transactionTime)) a
order by a.year, b.month, c.day
All this particular query does is group all the trades in the Dow30 by date, and sums up the total volume. I use this because the table stores every trade on a per second basis, and I want to roll it up on a per-day basis.
This isn't the full query, but just an example. I know I could just simply doing this from a single SELECT
, but I was wondering if doing a SELECT
of a SELECT
would incur some sort of performance penalty, or if this all gets optimized magically.
The reason why I formulate the query this way is because that's how I visualize the query, and I actually join this top-level select
with another table, and selecting out the columns from the underlying SELECT
makes it easier for me to visualize the query. But having no formal DBA background, I'm not sure if this is wrong, and if I need to figure out a better way of writing my queries.