问题
Hi I want to know which kind of queries are good with multiple tables.
For eg:
select table1.id, table1.name from table1,table2,table3
where table1.id=table2.id and table2.id=table3.id
or
select table1.id, table1.name
from table1
inner join table2 on table1.id=table2.id
inner join table3 on table2.id=table3.id
where 1
or
select table1.id, table1.name
from table1
join table2 on table1.id=table2.id
join table3 on table2.id=table3.id
where 1
Which kind of query is best for performance?
回答1:
They should be exactly the same. Probably you might want to read the corresponding section from the MySQL manual (which is only about syntax, not about performance, however).
回答2:
I would expect all 3 of these to be parsed as exactly equivalent, and will therefore perform identically. The where
clauses in the latter two queries are redundant.
Many developers prefer the explicit, ANSI-92 syntax (as used in the latter 2 queries) over the implicit syntax in the first query - however, this should have no impact on performance.
回答3:
As it has already been pointed out, all forms are the same regarding efficiency. They are different, however, in an also very important aspect: readability.
That's why I prefer to use the inner join
syntax. It's also the notation prevalent in the standards.
You might find the answers to this question useful: SQL Inner Join syntax
回答4:
join
is just a shortcut for inner join
and doing from table1,table2 where
is the same as an inner join too (see mysql documentation and mysql forum). they all should be treated the same way by mysql (the parsing-time for the second one might be a tiny bit lower, but thats negligible).
at least you should choose the one thats more radable (and maintainable that way) wich would be the second one (in my opinion, some guys might like the first one).
来源:https://stackoverflow.com/questions/7686190/mysql-query-perfomance-which-one-is-best