Is there a reason this simple SQL query should be so slow?

This query takes about a minute to produce results:

SELECT MAX(d.docket_id), MAX(cus.docket_id) FROM docket d, Cashup_Sessions cus 

But this one:

 SELECT MAX(d.docket_id) FROM docket d UNION MAX(cus.docket_id) FROM Cashup_Sessions cus 

gives your results instantly. I do not see what the first does, which will take much more time. I mean, they both just check the same two lists of numbers for the largest and return them. What else could be done that I do not see?

I am using jet SQL in an MS Access database through Java.

+6
sql
source share
3 answers

the first cross-connects between the two tables, and the second does not.
that everything is there.

+13
source share

The first uses the Cartesian product to form the source data, which means that each row from the first table is associated with each row from the second. After that, he searches for the source to find the maximum values ​​from the columns.

The second does not join tables. It just finds max from the fist table and maximum from the second table and returns two rows.

+9
source share

The first query cross-connects between tables until maximums are reached, which means that each record in one table is merged with each record in another table.

If you have two tables of 1000 pieces each, you will get a result with 1,000,000 points to find the maximum values.

+3
source share

All Articles