0

Consider a simple query like this:

SELECT * FROM DATA
JOIN
(
SELECT * FROM DATA
)TEMPORARY_DATA
ON TEMPORARY_DATA.DATA_IDN = DATA.DATA_IDN

What is the performance of this? Is it O(n2) because TEMPORARY_DATA doesn't have indexes as opposed to normal tables. Normal joins I think happens in O(nlogn) time because it is indexed right?

Does this change if the inner query is slightly complex and so direct? I mean is it possible the optimized it not able to guess that it is not the indexed DATA table!

I am newbie to these things, please let me know if this belongs to some other place on the SO Network.

4
  • 2
    All the relational databases that I know of have some way to explain the query plan. What does the plan say?
    – user40980
    Commented May 19, 2015 at 13:13
  • 1
    I will look this up and tell you. I am not an expert in understanding the execution plan, but thats the right direction.
    – Nishant
    Commented May 19, 2015 at 13:24
  • 1
    If you access the same table in different parts of your query, you may want to look into Common Table Expressions. This is a way to access the Data table/view just once. msdn.microsoft.com/en-us/library/ms175972(v=sql.105).aspx
    – JeffO
    Commented May 19, 2015 at 18:24
  • Yes. Actual temporary tables (with a #) can have indexes and statistics and behave almost entirely like a normal table. Commented Jun 9, 2015 at 3:10

1 Answer 1

4

(The following answer uses SQL Server RDBMS to explain)

First off, there are no temporary tables in your question. Temporary tables (in SQL Server) are actual tables that live in tempdb, and are defined with one or two pound signs (#) preceding it. What you have is a simple alias (temporary_data). Two extremely different things.

That is an extremely long and verbose way of writing a join on a table to itself. Take this table structure:

create table dbo.Data
(
    Id int not null
);
go

I will insert a few rows:

insert into dbo.Data
values (1), (2), (3);
go

The result set of:

select *
from dbo.Data
join
(
    select *
    from dbo.Data
) temporary_data
on temporary_data.Id = Data.Id;

Is...

Id  Id
1   1
2   2
3   3

Looking at the post execution plan of this operation, SQL Server just does a join on these two tables:

enter image description here

Logically this is the exact same query (results and execution plan) as the following:

select *
from dbo.Data d1
inner join dbo.Data d2
on d1.Id = d2.Id;

It is just an extremely long way to write it. Nothing is gained by the former, and the only thing lost is readability.

2
  • If you mean its optimization then I will have to dig further since the original query which I had an issue is more complex than this. So is it possible if the inner query is slighly complex, and not just the direct Data table things change - joins are more costly? Just curious. I will look up the execution plan anyway.
    – Nishant
    Commented May 19, 2015 at 13:35
  • 1
    @Nishant since SQL is a declarative you tell the server what you want and it figures out the best way to get it. There are things we can do to help (indexes and such) but the optimizer and parser will break down your statement into the most efficient execution plan. Knowing how the optimizer works we can manipulate the query to better help the optimizer get a good plan and thus execute faster. Commented May 19, 2015 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.