Ah.....when I take your point #3 into account, below....things run
beautifully. //Ike
> > SELECT DISTINCT
upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori
> > gin,associatekey.username,associatekey2.username,upcards.deleted
> > FROM upcards,status,origins,associates
[quoted text clipped - 19 lines]
> 3/ Your 'from' clause is replicating tables that are already joined
> within your INNER JOIN statements.
justaguy - 16 Oct 2004 22:47 GMT
>>> SELECT DISTINCT upcards.id, statuskey.status,
>>> upcards.firstname, upcards.lastname, originkey.origin,
[quoted text clipped - 8 lines]
>>> on upcards.associatekey2 = associatekey2.id
>>> ...
"2metre" wrote:
>> ...
>> 3/ Your 'from' clause is replicating tables that are already joined
>> within your INNER JOIN statements.
> Ah.....when I take your point #3 into account, ...
> things run beautifully. //Ike
But, of course! The FROM clause with four tables listed makes a cross
join of all four, which itself is then joined with each of the other
four views. This is huge and slow.
Changing the FROM ... JOIN ... clauses to what you really meant
eliminates the cross product, simplifies the query and speeds it up
geometrically.
SELECT DISTINCT upcards.id, status.status,
upcards.firstname, upcards.lastname, origins.origin,
associates.username, a2.username, upcards.deleted
FROM upcards
INNER JOIN status on upcards.statuskey = status.id
INNER JOIN origins on upcards.originkey = origins.id
INNER JOIN associates on upcards.associatekey = associates.id
INNER JOIN associates a2 on upcards.associatekey2 = a2.id
;
Others have addressed the logic of table aliases.
A google on SQL CROSS JOIN found, for example,
http://www.sql-server-performance.com/tuning_joins.asp
excerpt:
... I ran across a slow-performing query from an ERP program. ... I
noticed that instead of creating a LEFT JOIN, the developer had
accidentally created a CROSS JOIN instead. In this particular example,
less than 10,000 rows should have resulted from the LEFT JOIN, but
because a CROSS JOIN was used, over 11 million rows were returned
instead. Then the developer used a SELECT DISTINCT to get rid of all the
unnecessary rows created by the CROSS JOIN. As you can guess, this made
for a very lengthy query.