Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial mysql求助 请问where a.id=b.id 和join on a.id=b.id 在效率上的区别

mysql求助 请问where a.id=b.id 和join on a.id=b.id 在效率上的区别

Jun 23, 2016 pm 02:38 PM

下面是ecshop 的商品表和品牌表的查询,请问它们的查询效率有什么区别呢? 
还有一个问题是 left join 和join的效率哪个高一点呢。
谢谢 !!

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` 
Copy after login

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



回复讨论(解决方案)

你的第一式是左链接,因无其他过滤条件
结果集中将会有左表(ecs_goods)的全部记录

你的第二式是逗号连接(INNER JOIN 的简写)
结果集中只会出现符合连接条件的记录

两者的作用是不同的,不能做效率比较

当右表(ecs_brand)有过滤条件时
左连接退化为内连接,两式就一样了,没有差别

你的第一式是左链接,因无其他过滤条件
结果集中将会有左表(ecs_goods)的全部记录

你的第二式是逗号连接(INNER JOIN 的简写)
结果集中只会出现符合连接条件的记录

两者的作用是不同的,不能做效率比较

版主 那是不是第一个sql删去left,就和第二个sql完全一样的呢。
它们在效率和索引使用方面有没区别的呢。

当右表(ecs_brand)有过滤条件时
左连接退化为内连接,两式就一样了,没有差别

版主 谢谢你的回答。我还有个问题

比如商品必须选择品牌,就是商品必定存在品牌id。
然后是品牌表肯定有商品的品牌。

所以它们用left join,或是join 返回的结果是一样的。 那这样它们可以进行效率对比吗。

由于连接外有过滤条件,所以 mysql 会将你的查询指令优化成内连接。因此就不存在效率的对比了
当然这里可以对比的是:
在商品表中查品牌商品 和 在品牌表中查商品
两者的效率是不一样的
因为品牌表显然要比商品表小

由于连接外有过滤条件,所以 mysql 会将你的查询指令优化成内连接。因此就不存在效率的对比了
当然这里可以对比的是:
在商品表中查品牌商品 和 在品牌表中查商品
两者的效率是不一样的
因为品牌表显然要比商品表小
版主 谢谢你的回答

我总结了一下你的回答:
第一点:

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


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` 
Copy after login
Copy after login


两句sql是完全一样的 mysql会将第一条sql优化成第二条。

第二点:

“在商品表中查品牌商品 和 在品牌表中查商品”
就是说

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` 
Copy after login
Copy after login


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` 
Copy after login

两句sql的效率是不一样的。
//////////////////////////////////////////////////////////////
以上两点描述正确吗 ?我有没理解错你的意思呢。
如果没有 那第二点大家处理方式都是笛卡尔积。
我的理解是 如果是 ecs_goods` AS a JOIN ecs_brand AS b
那就是商品表的一条记录 扫描品牌表的所有记录。如果有10个商品 10个品牌
那就是10*10=100 扫描了100次

反过来ecs_brand AS b JOIN `ecs_goods` AS a
一个品牌扫描10个商品 那扫描次数也是100次。 那为什么它们的效率不一样呢

呵呵,你自己偷换概念,给自己上个套!完全是为了你的错误观点

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

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`
Copy after login
Copy after login

是等效的
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
Copy after login
Copy after login

会被 mysql 优化为
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
Copy after login
Copy after login

呵呵,你自己偷换概念,给自己上个套!完全是为了你的错误观点

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

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`
Copy after login
Copy after login

是等效的
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
Copy after login
Copy after login

会被 mysql 优化为
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
Copy after login
Copy after login



版主 这两条语句我试了一下,好像是不相等的阿。
-- -- 表的结构 `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 ;-- -- 导出表中的数据 `good_tbl`-- INSERT INTO `good_tbl` VALUES (1, 2);INSERT INTO `good_tbl` VALUES (2, 3);INSERT INTO `good_tbl` VALUES (3, 2);
Copy after login

-- -- 表的结构 `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 ;-- -- 导出表中的数据 `brand_tbl`-- INSERT INTO `brand_tbl` VALUES (1, '诺基亚');INSERT INTO `brand_tbl` VALUES (3, '三星');
Copy after login




版主 请问这些mysql的资料在哪里可以找到的呢,比如怎样知道某些语句在mysql里面会优化成另外的语句呢。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles