472,805 Members | 1,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Inner Join / Indexes Hell

Ike
Oh I have a nasty query which runs incredibly slowly. I am running MySQL
4.0.20-standard. Thus, in trying to expedite the query, I am trying to set
indexes in my tables.
My query requires four inner joins, as follows :

SELECT DISTINCT
upcards.id,statuskey.status,upcards.firstname,upca rds.lastname,originkey.ori
gin,associatekey.username,associatekey2.username,u pcards.deleted
FROM upcards,status,origins,associates
INNER JOIN status statuskey on upcards.statuskey=statuskey.id
INNER JOIN origins originkey on upcards.originkey=originkey.id
INNER JOIN associates associatekey on upcards.associatekey=associatekey.id
INNER JOIN associates associatekey2 on
upcards.associatekey2=associatekey2.id
WHERE (upcards.deleted is null OR upcards.deleted = 0) AND
(associatekey.branch IS NULL OR associatekey.branch LIKE '' OR
associatekey.branch LIKE '%');

The tables structure is:

table upcards
id
firstname
lastname
deleted
statuskey
originkey
associatekey
associatekey2

table status
id
status

table origins
id
origins

table associates
id
username
branch

Thus, to expedite my query, I am setting my indexes as follows: (id is a
PRIMARY key on all tables)
for the table upcards I set as indexes:
deleted
statuskey
originkey
associatekey
associatekey2

and for associates:
branch

In doing so, I have every column thus indexed, you would think this query
would run faster, but it does not. Is there a better means of setting my
indexes here to speed up this query? Perhaps there is a more efficient way
to write this query, i.e. perhaps the problem is not so much with my
indexing, as with the query itself? Thanks, Ike
Jul 20 '05 #1
3 3273
Ike wrote:
SELECT DISTINCT
upcards.id,statuskey.status,upcards.firstname,upca rds.lastname,originkey.ori
gin,associatekey.username,associatekey2.username,u pcards.deleted
FROM upcards,status,origins,associates
INNER JOIN status statuskey on upcards.statuskey=statuskey.id
INNER JOIN origins originkey on upcards.originkey=originkey.id
INNER JOIN associates associatekey on upcards.associatekey=associatekey.id
INNER JOIN associates associatekey2 on
upcards.associatekey2=associatekey2.id ...
1/ I would recommend using different aliases. Isnt the whole point of
aliases to reduce the amount of code to type and make the query easier
to read? Not only are your aliases longer than the original tablenames
but they're also confusing because they duplicate the names of fields!
How about using a and a2?
WHERE (upcards.deleted is null OR upcards.deleted = 0) AND
(associatekey.branch IS NULL OR associatekey.branch LIKE '' OR
associatekey.branch LIKE '%');


2/ What other value could associatekey.branch take? You seem to have
matched on null, empty and any value?

3/ Your 'from' clause is replicating tables that are already joined
within your INNER JOIN statements.
Jul 20 '05 #2
Ike
Ah.....when I take your point #3 into account, below....things run
beautifully. //Ike

"2metre" <2m****@xxxhersham.net> wrote in message
news:ck**********@titan.btinternet.com...
Ike wrote:
SELECT DISTINCT
upcards.id,statuskey.status,upcards.firstname,upca rds.lastname,originkey.ori gin,associatekey.username,associatekey2.username,u pcards.deleted
FROM upcards,status,origins,associates
INNER JOIN status statuskey on upcards.statuskey=statuskey.id
INNER JOIN origins originkey on upcards.originkey=originkey.id
INNER JOIN associates associatekey on upcards.associatekey=associatekey.id INNER JOIN associates associatekey2 on
upcards.associatekey2=associatekey2.id ...


1/ I would recommend using different aliases. Isnt the whole point of
aliases to reduce the amount of code to type and make the query easier
to read? Not only are your aliases longer than the original tablenames
but they're also confusing because they duplicate the names of fields!
How about using a and a2?
> WHERE (upcards.deleted is null OR upcards.deleted = 0) AND
> (associatekey.branch IS NULL OR associatekey.branch LIKE '' OR
> associatekey.branch LIKE '%');


2/ What other value could associatekey.branch take? You seem to have
matched on null, empty and any value?

3/ Your 'from' clause is replicating tables that are already joined
within your INNER JOIN statements.

Jul 20 '05 #3
Ike wrote:
SELECT DISTINCT upcards.id, statuskey.status,
upcards.firstname, upcards.lastname, originkey.origin,
associatekey.username, associatekey2.username,
upcards.deleted
FROM upcards, status, origins, associates
INNER JOIN status statuskey on upcards.statuskey = statuskey.id
INNER JOIN origins originkey on upcards.originkey = originkey.id
INNER JOIN associates associatekey
on upcards.associatekey = associatekey.id
INNER JOIN associates associatekey2
on upcards.associatekey2 = associatekey2.id
...

"2metre" wrote: ...
3/ Your 'from' clause is replicating tables that are already joined
within your INNER JOIN statements.

Ike wrote: 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.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
4
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
6
by: Thomas Beutin | last post by:
Hi, i've a speed problem withe the following statement: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id FROM ot_adresse AS a, ot_produkt AS p LEFT OUTER JOIN ot_kat_prod AS pz ON (...
3
by: moldster | last post by:
Hi, I'm at my wits end! I have two large tables one with 1.2mill one with 2.3 mill and they are very wide tables. I have a select with an inner join. All columns used in the join are contained...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of threshold. They query is somewhat responsive when...
12
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
2
by: MATTXtwo | last post by:
I have this store procedure to select data from table with join like this...SELECT tblPeribadi.Personel_No, tblPeribadi.Nama,tblCompany.Keterangan as Company_Code, tblPeribadi.Jawatan,...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.