473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

to optimize a select join

Hi,

which is the faster query ? (of course, in my case the real queries are more
complex)

1/
select *
from file1 a join file2 b on b.key=a.key
where b.data=123
and b.name='TEST'

2/
select *
from file1 a join file2 b on b.key=a.key and b.data=123
where b.name='TEST'

Or is it the same ?
Thanks.

Jean-Claude
Apr 29 '06 #1
4 1924
NC
Jean-Claude wrote:

which is the faster query ? (of course, in my case the real queries
are more complex)
Which means exactly one thing: you must benchmark your real queries.
Read up on EXPLAIN statement.
1/
select *
from file1 a join file2 b on b.key=a.key
where b.data=123
and b.name='TEST'

2/
select *
from file1 a join file2 b on b.key=a.key and b.data=123
where b.name='TEST'


Off the top of my head, this is not going to make a lot of difference.
You might, however, want to experiment with "file1 AS a LEFT JOIN file2
AS b" vs. "file2 AS b LEFT JOIN file1 AS a". In your case, since you
filter by b.data and b.test, "file2 AS b LEFT JOIN file1 AS a" may be
faster, because MySQL will not be doing a full scan of file2.

Also, be sure that file1.key, file2.key, file2.data, and file2.name are
indexed.

Cheers,
NC

Apr 29 '06 #2
On Sat, 29 Apr 2006 17:55:36 +0200, Jean-Claude wrote:
1/
select *
from file1 a join file2 b on b.key=a.key
where b.data=123
and b.name='TEST'


I cannot understand why do people use that idiotic ANSI join syntax.
Relational databases model naive set theory. That means defining subsets
by setting rules on elements. Your query should be best written like
this:

select a.*,b.*
from file1 a,file2 b
where a.key=b.key and
b.data=123 and
b.name='TEST'

That way, the database optimizer doesn't have to contend with
idiotic things like "join". You should define your data and your
selection properly, index the proper columns and enjoy.

--
http://www.mgogala.com

Apr 29 '06 #3
Mladen Gogala wrote:
On Sat, 29 Apr 2006 17:55:36 +0200, Jean-Claude wrote:

1/
select *
from file1 a join file2 b on b.key=a.key
where b.data=123
and b.name='TEST'

I cannot understand why do people use that idiotic ANSI join syntax.
Relational databases model naive set theory. That means defining subsets
by setting rules on elements. Your query should be best written like
this:

select a.*,b.*
from file1 a,file2 b
where a.key=b.key and
b.data=123 and
b.name='TEST'

That way, the database optimizer doesn't have to contend with
idiotic things like "join". You should define your data and your
selection properly, index the proper columns and enjoy.


Maybe because the "rules of elements" don't allow all possibilities?

For instance - try this. List all records in table A, along with their matching
records in table B if those records exist. Impossible without a Union, but very
easy with a LEFT OUTER JOIN.

There is minimal additional overhead to the JOIN syntax method. And much more
flexibility. Additionally, you only need to learn one basic syntax for all JOIN
cases.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 29 '06 #4
On Sat, 29 Apr 2006 16:42:03 -0400, Jerry Stuckle wrote:
Maybe because the "rules of elements" don't allow all possibilities?

For instance - try this. List all records in table A, along with their matching
records in table B if those records exist. Impossible without a Union, but very
easy with a LEFT OUTER JOIN.
My preferred garden variety of databases (Oracle) has a syntax extension
(+) for such cases. I find it to be much more understandable. In addition
to that, internally the database does perform a union, it only uses the
syntax to hide it.

There is minimal additional overhead to the JOIN syntax method. And much more
flexibility. Additionally, you only need to learn one basic syntax for all JOIN
cases.


Unfortunately, it makes the whole thing much less understandable and much
less aesthetic. Here is the aesthetic problem:

SELECT * FROM
emp e JOIN dept d ON e.deptno=d.deptno
What is the result of that join? Rows form EMP? Rows from DEPT?
Rows from both? It turns out that it is rows from both. It doesn't even
save much space:

SELECT ename,job,dname,loc
FROM emp e,dept d
WHERE e.deptno=d.deptno

The statement above performs the same thing as the statement using the
ANSI join. Saving space is especially dubious if we need an outer join.
The statement
SELECT ename,job,dname,loc FROM
emp e RIGHT OUTER JOIN dept d ON e.deptno=d.deptno

gives exactly the same result as

SELECT ename,job,dname,loc
FROM emp e,dept d
WHERE e.deptno(+)=d.deptno

The second SQL is much more clear, logical and pleasing to the eye.

Every database has its own syntax for outer joins. Database optimizers are
finely tuned to its own syntax and usually have many more problems (and
related bugs) with ANSI joins then with the usual, mathematically logical
ones. In mathematical terms, "relation" is a subset of Cartesian product.
Any subset. That means that we get to pick the elements of Cartesian
product that we want in our relation. You can put even further conditions
on relations. If the presence of pair (a,b) in the relation means that the
pair (b,a) is not in the relation and the presence of pairs (a,b) and
(b,c) implies the presence of the pair (a,c) then we are talking about the
ordering relation. That can be refined further into strict ordering and
well ordered sets (every set can be well ordered, provided we accept so
called "axiom of choice", but that's slightly, just slightly, outside the
scope of PHP group). That is the notation I love and understand. ANSI
joins are ugly, support the dangerous illusion about "portable database
applications" and make the statement much likely to encounter a bug in the
database code. To make long story short, I hate ANSI joins, with passion.

--
http://www.mgogala.com

Apr 30 '06 #5

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

Similar topics

0
by: rg | last post by:
Hello, I use invisionboard on a bid web site (20 000 visitors each day) But my server has high load average. Could you help me to optimize these select command. 1) SELECT p.*,...
4
by: mjuricek | last post by:
I'm having some problems to optimize my stored procedure (select statement with joins) What I'm trying to do is calculate total work. My situation: I have 3 tables I'm using -Input (char...
1
by: malcolm | last post by:
This one's kind of hard to explain, so I've opted to post a simplified version of our view that prompted me to ask this question: The question is re-asked after the view... create view MainView...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
19
by: octangle | last post by:
This code is attempting to find records that have a RegJrnID that does not occur more than one time in the table. The reason that I want to find records with non-duplicated RegJrnID values is to...
1
by: rajeshkapadi | last post by:
Please help me optimize this: I have a table with columns: headlineid, keyword. headlineid+keyword combination is unique. Relationship between headline and keyword is many-to-many. i.e.,...
1
by: kmugunda | last post by:
Hi below is a query where certain set of data is fetched from a table based on certain condition, but the execution takes lot of time , is there a way we can optimize the code below SELECT...
2
by: bravo | last post by:
hi everyone , i am trying to join two tables , the query is working fine when the records in the tables are less but when the records are in huge numbers say 1-2 million then the system hangs... i am...
11
by: bravo | last post by:
hi i need to optimize the join query, which joins three tables say table1 ,table2 , table3 each having huge volume of records... the query is as select table1.id,table2.time,table3.Status from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.