For mysql help, could you tell me the difference in efficiency between wherea. id = B. id and joinona. id = B. id?

Source: Internet
Author: User
For mysql help, may I ask wherea. idb. id and joinona. idb. the difference in the efficiency of IDS is as follows: the ecshop product table and the brand Table Query. what is the difference in the query efficiency?
Another problem is the efficiency of left join and join.
Thank you !!
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS aLEFT JOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id` 

SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a, ecs_brand AS bWHERE a.`brand_id` = b.`brand_id`



Reply to discussion (solution)

Your first formula is left link, because there are no other filter conditions
All records of the left table (ecs_goods) will be displayed in the result set.

Your second formula is comma JOIN (abbreviated as inner join)
Only Records meeting the connection conditions will appear in the result set.

The roles of the two are different, and efficiency comparison is not allowed.

When the right table (ecs_brand) has filtering conditions
The left connection degrades to the inner connection. The two methods are the same and there is no difference.

Your first formula is left link, because there are no other filter conditions
All records of the left table (ecs_goods) will be displayed in the result set.

Your second formula is comma JOIN (abbreviated as inner join)
Only Records meeting the connection conditions will appear in the result set.

The roles of the two are different, and efficiency comparison is not allowed.

The moderator should delete left in the first SQL statement, which is exactly the same as that in the second SQL statement.
They have no difference in efficiency and index usage.

When the right table (ecs_brand) has filtering conditions
The left connection degrades to the inner connection. The two methods are the same and there is no difference.

Thank you for your answer. I have another question.

For example, a product must select a brand, that is, the product must have a brand id.
Then, the brand table must have the brand of the product.

Therefore, they use left join or join to return the same results. So can they compare the efficiency.

Mysql optimizes your query command to an internal connection because of filtering conditions outside the connection. Therefore, there is no comparison of efficiency.
Of course, the following can be compared:
Query the brand items in the product table and the products in the brand table
The efficiency of the two is different.
Because the brand table is obviously smaller than the product table

Mysql optimizes your query command to an internal connection because of filtering conditions outside the connection. Therefore, there is no comparison of efficiency.
Of course, the following can be compared:
Query the brand items in the product table and the products in the brand table
The efficiency of the two is different.
Because the brand table is obviously smaller than the product table
Moderator thank you for your answer

I summarized your answer:
First:

SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a, ecs_brand AS bWHERE a.`brand_id` = b.`brand_id`

And
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS aJOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id` 


The two SQL statements are exactly the same. mysql will optimize the first SQL statement to the second one.

Second point:

"Query brand products in the product table and products in the brand Table"
That is to say

SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS aJOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id` 

And
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  ecs_brand AS bJOIN `ecs_goods` AS a ON b.`brand_id`=a.`brand_id` 

The efficiency of the two SQL statements is different.
//////////////////////////////////////// //////////////////////
Are the above two correct descriptions? I don't understand what you mean.
If not, the second method is Cartesian product.
My understanding is that if it is ecs_goods 'AS a JOIN ecs_brand AS B
That is, a record of the product table scans all records of the brand table. If there are 10 products and 10 brands
That is, 10*10 = 100 scans for 100 times.

In turn, ecs_brand AS B JOIN 'ecs _ goods 'AS
A brand scans 10 items, and the number of scans is also 100. Why are they different in efficiency?

Oh, you have to steal the concept and set it for yourself! It's for your incorrect opinion.

SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a, ecs_brand AS bWHERE a.`brand_id` = b.`brand_id`
And
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a INNER JOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id`

Is equivalent
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a LEFT JOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id`WHERE b.`field_name`= 123

Will be optimized
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a, ecs_brand AS bWHERE a.`brand_id` = b.`brand_id`AND b.`field_name`= 123

Oh, you have to steal the concept and set it for yourself! It's for your incorrect opinion.

SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a, ecs_brand AS bWHERE a.`brand_id` = b.`brand_id`
And
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a INNER JOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id`

Is equivalent
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a LEFT JOIN ecs_brand AS b ON a.`brand_id` = b.`brand_id`WHERE b.`field_name`= 123

Will be optimized
SELECT a.`goods_id` , a.`goods_name` , b.brand_nameFROM  `ecs_goods` AS a, ecs_brand AS bWHERE a.`brand_id` = b.`brand_id`AND b.`field_name`= 123



I tried the two statements of the moderator, as if they were not equal.
-- TABLE structure 'good _ TBL' -- create table 'good _ TBL' ('good _ id' int (10) unsigned not null auto_increment, 'brand _ id' int (10) unsigned not null, primary key ('good _ id') ENGINE = MyISAM default charset = gbk AUTO_INCREMENT = 4; -- export the data in the table 'good _ TBL' -- insert into 'good _ TBL' VALUES (1, 2); insert into 'good _ TBL' VALUES (2, 3 ); insert into 'good _ TBL' VALUES (3, 2 );

-- TABLE structure 'brand _ TBL' -- create table 'brand _ TBL' ('brand _ id' int (10) unsigned not null auto_increment, 'brand _ name' varchar (50) not null, primary key ('brand _ id') ENGINE = MyISAM default charset = gbk AUTO_INCREMENT = 4; -- export the data in the table 'brand _ TBL' -- insert into 'brand _ TBL' VALUES (1, 'Nokia '); insert into 'brand _ TBL' VALUES (3, 'Samsung ');




Where can I find these mysql documents? for example, how can I know that some statements will be optimized to other statements in mysql.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.