472,811 Members | 1,102 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,811 software developers and data experts.

Strange results when optimizing LEFT JOIN in MySQL

Dear Sirs,

I am experiencing strange results when trying to optimize a LEFT JOIN
on 3 tables using MySQL.

Given 3 tables A, B, C such as the following:

create table A (
uniqueId int not null default 0 auto_increment,
a1 varchar(64) not null default '',
a2 varchar(64) not null default '',
a3 varchar(64) not null default '',
...
primary key(uniqueId)
)

create table B (
uniqueId int not null default 0,
language varchar(2) not null default '',
b1 varchar(64) not null default '',
b2 varchar(64) not null default '',
b3 varchar(64) not null default '',
primary key (uniqueId, language)
)

create table C (
uniqueId int not null default 0,
language varchar(2) not null default '',
c1 varchar(64) not null default '',
c2 varchar(64) not null default '',
c3 varchar(64) not null default '',
primary key (uniqueId, language)
)

And given that for any value of the uniqueId in table A, there may
exist 0, 1, or more corresponding rows with the same uniqueId in
tables B and C.

I would like to query these tables using a LEFT JOIN for a given value
of the uniqueId. (Say, for uniqueId = 188)

This is easy enough using the following LEFT JOIN ~

SELECT A.*, B.*, C.* from
A LEFT JOIN B ON A.uniqueId = B.uniqueId
LEFT JOIN C on B.uniqueId = C.uniqueId
WHERE A.uniqueId = 188
AND B.lang = C.lang
However, this does not seem very efficient since ALL of the results
from the first join (A LEFT JOIN B) are used in the second join (on
table C) which is surely unnecessary since I know which row(s) from
the first join I really want to use (those with A.uniqueId = 188)

So, I tried selecting the specific record much earlier, with the
following

SELECT A.*, B.*, C.* from
A LEFT JOIN B ON (A.uniqueId = B.uniqueId AND A.uniqueId = 188)
LEFT JOIN C on B.uniqueId = C.uniqueId
WHERE B.lang = C.lang

But this seems to actually take longer. . how can that be ?

Even mysql's EXPLAIN seems to suggest that the former is faster:

This is the first LEFT JOIN query ~

+-------+-------+---------------+------------+---------+-------+------+------------+
| table | type | possible_keys | key | key_len | ref | rows
| Extra |
+-------+-------+---------------+------------+---------+-------+------+------------+
| A | const | PRIMARY | PRIMARY | 4 | const | 1
| |
| B | ref | uniqueId | uniqueId | 4 | const | 1
| |
| C | ref | uniqueId | uniqueId | 4 | const | 1
| where used |
+-------+-------+---------------+------------+---------+-------+------+------------+

And this is when I try to optimize it ~

+-------+------+---------------+------------+---------+--------------+------+---
| table | type | possible_keys | key | key_len | ref |
rows | Extra |
+-------+------+---------------+------------+---------+--------------+------+---
| A | ALL | NULL | NULL | NULL | NULL |
9387 | |
| B | ref | uniqueId | uniqueId | 4 | A.uniqueId |
1 | |
| C | ref | uniqueId | uniqueId | 4 | A.uniqueId |
1 | where used |
+-------+------+---------------+------------+---------+--------------+------+------------+
I'm not very conversant with the EXPLAIN output but looking at the
Rows above, it seems that the first query is faster since it uses left
rows. I don't understand why the second (my 'optimized') query can not
use the keys either.

Thank you very much if anybody can shed light on this,

Soefara.
Jul 19 '05 #1
0 2301

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

Similar topics

1
by: LRW | last post by:
I have a SQL string with a couple of JOINs that works perfectly in command prompt. I get results for all the fields selected just fine. But when I put it into a PHP page, I can't make variables(?)...
4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
0
by: Petre Agenbag | last post by:
Hi List Me again. I'm trying to return from multiple tables, the records that have field "information_sent" between two dates. The tables are all related by means of the id of the entry in the...
0
by: Apollo | last post by:
1.2Ghz Pentium, with 1/2Gig of ram, 4.0.14MySQL, RedHat9.0 I have about 20K records that result from the following query. Front end for the database is ACCESS97 and pulling up 20K records makes a...
9
by: Dave M | last post by:
All I've got a database that keeps track of sales of widgets. Each company that belongs to my organiztion is to report their widget sales or no sales every month. There are several different...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Justin | last post by:
Hi. I decided to tidy up some of my queries, but I came across something that stumpt me: mysql> SELECT -> jobs.jobId, -> jobs.active, -> jobs.title, -> jobs.listed, -> ...
16
by: Justin Koivisto | last post by:
I am trying to create a query to use as a report record source. Below is what I want to do (this was tested and works with a MySQL web script): SELECT contacts.id, contacts.email,...
0
by: Luongo | last post by:
Hi, I'm working on a mysql-based survey program. On the user page I display the survey form, and on the admin page I display the results for each question. The problem, which I find puzzling, is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 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
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
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...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
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.