0

I am running two sql queries say,

select obname from table1 where obid = 12

select modname from table2 where modid = 12

Both are taking very less time, say 300 ms each.

But when I am running:

select obname, modname 
from (select obname from table1 where obid = 12) as alias1, 
(select modname from table2 where modid = 12) as alias2

It is taking 3500ms. Why is it so?

4
  • How many rows are the two queries returning? Commented Oct 17, 2015 at 11:52
  • First of all you are comparing 2 queries to 1,in execution queries are not like apples just add the times together and its done Commented Oct 17, 2015 at 11:53
  • 1
    Why don't you use UNION ALL Commented Oct 17, 2015 at 11:53
  • The second query is returning only one row Commented Oct 19, 2015 at 6:21

1 Answer 1

1

In general, putting two scalar queries in the from clause is not going to affect performance. In fact, from an application perspective, one query may be faster because there is less overhead going back and forth to the database. A scalar query returns one column and one row.

However, if the queries are returning multiple rows, then your little comma is doing a massive Cartesian product (which is why I always use CROSS JOIN rather than a comma in a FROM clause). In that case, all bets are off, because the data has to be processed after the results start returning.

Sign up to request clarification or add additional context in comments.

1 Comment

Even cross join is taking the same time for me...because the second query is returning only one row

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.