Home Database Mysql Tutorial 关于子查询和连接的趣味小题目

关于子查询和连接的趣味小题目

Jun 07, 2016 pm 04:39 PM
about classmate Inquire connect

今天有位同学出来一个趣味小题。 A: select title from simplified where id in (select id from analysis where word = something); B: select b.title from analysis a join simplified b on (a.id=b.id) where a.word=something; C: select simplified

今天有位同学出来一个趣味小题。

A:
select title
from simplified
where id in (select id
                  from analysis
                  where word = ‘something’);
B:
select b.title
from  analysis a
join simplified b
on (a.id=b.id)
where a.word=’something’;
C:
select simplified.title
from  analysis
join simplified
on (analysis.id=simplified.id)
where analysi.word=’something’;

上面的3段sql,在MYSQL中执行,哪一个效率最优,请讲出原因

看到这个问题,我想起了之前作面试官的时候,也问过这里的问题。从另一个角度,我收集了大家面对这题目的时候一些思考,总结成下面的一个面试小故事。

这类题目真得蛮适合面试的时候考察对sql的掌握程度。

模拟一个场景,面试者:小白,面试官:小二,下面开始这段面试过程…..

小二:…. 我这里有个小问题,你看看(给出上面3段sql),balabala。。。。

小白:这2个表数据量如何了,join ,子查询的优化根据数据量可能还有所不一样吧。子查询适合外结果集大,子查询结果集小的情况,最好是能保证子查询所返回的结果集尽量的小。

小二:假设2表,数据量差不多,都是万条记录左右。

小白:如果是这种的话,A应该会慢一点吧。 如果是在mysql上执行的话,A中子查询语句会认为与外面的simolified表进行关联比较有效率。这样的话A其实就回被翻译成:

select title from simplified where exists (select simplified.title from analysis where word= ‘something’ and id =simplified .id);

这种in子查询的形式,在外部表(比如上面的simplified )数据量较大的时候效率是很差的.

小二: 恩,那么B和C之间有什么区别呢?

小白:B和C在我看来,应该是差不多的,其主要区别就是B采用了别名。但是我认为应该是没多大影响的。 

因为数据库本身执行时,都会再把表名给换成自己的别名。但我影响中好像记得有一条数据库的优化建议是尽量多表连接,最好是使用表的别名来引用列。

对了,有对id建索引吧。

小二: 2表id都是主键,这算是建了索引吗?

小白: 是的,主键可以保证记录的唯一和主键域非空,数据库管理系统对于主键自动生成唯一索引,所以主键也是一个特殊的索引。

小二:  恩,一张表只能有一个主键,是不是就可以说只能有一个唯一的索引呢?

小白:  不对,主键一定是唯一性索引,唯一性索引并不一定就是主键,主键就是能够唯一标识表中某一行的属性或属性组,一个表只能有一个主键,但可以有多个候选索引。

小二: 主键列和唯一索引列还有区别吗?

小白:应该有,我知道的主键列不允许空值,而唯一性索引列允许空值。一个表中可以有多个唯一性索引,但只能有一个主键。

小二: 恩,回到上面的问题,你记得多表连接,最好使用表的别名来引用列,哪你有想过会是什么原因导致的呢?

小白:我估计应该是Mysql内部处理逻辑有关吧,有可能是使用了别名,就会再建一个临时表放入内存,这样后面的命中会更高。

小二:  差不多吧,mysql引擎自动生成temp表,造成缓存的效果。从这个sql,你大致应该能明白它的意图了,你想想有没有更优的写法,可以试试看…

小白: 好的,我想想…..

小白:  

select b.title from
(select id from  analysis where word=’something’)a,
 simplified b
where a.id=b.id;

如果这么去写,应该是最快的。放弃子查询和join。

小二: 恩,不错…..

。。。。。。

如果我在面试中碰到这样的小白,绝对会留下深刻的影响。

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
3 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)

How to connect keep body fat scale How to connect keep body fat scale Mar 07, 2024 pm 04:50 PM

How to connect the keep body fat scale? Keep has a specially designed body fat scale, but most users do not know how to connect the keep body fat scale. Next is the graphic tutorial on the connection method of the keep body fat scale that the editor brings to users. , interested users come and take a look! How to connect the keep body fat scale 1. First open the keep software, go to the main page, click [My] in the lower right corner, and select [Smart Hardware]; 2. Then on the My Smart Devices page, click the [Add Device] button in the middle; 3 , then select the device you want to add interface, select [Smart Body Fat/Weight Scale]; 4. Then on the device model selection page, click the [keep body fat scale] option; 5. Finally, in the interface shown below, finally [Add Now] at the bottom

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to connect OnePlus watch to Bluetooth headset_How to connect OnePlus watch to Bluetooth headset How to connect OnePlus watch to Bluetooth headset_How to connect OnePlus watch to Bluetooth headset Mar 23, 2024 pm 01:16 PM

1. Place the earphones in the earphone box and keep the lid open. Press and hold the button on the box to enter the pairing state of the earphones. 2. Turn on the watch music function and select Bluetooth headphones, or select Bluetooth headphones in the watch settings function. 3. Select the headset on the watch to pair successfully.

Steps to connect game controller to Gohan Arcade Steps to connect game controller to Gohan Arcade Mar 19, 2024 pm 03:55 PM

How to connect Bluetooth controller to Gohan Arcade? Gohan Game Center is a game box used by many mobile game players. It contains a large number of popular game resources and rich game-related functions. Below, the editor will introduce the game controller connection method. Players, please take a look. 1. First go to the homepage of Gohan Game Center APP, and then click the "My" option in the lower right corner of the homepage; 2. Find the [Controller] function in the My page, the location is shown in the picture below, and click to go to settings; 3. Select to turn it on For the Bluetooth function of the mobile phone, confirm that the power of the controller is on; 4. Finally, follow the instructions of the controller to make a matching connection. If the connection is successful, you can use the mobile game to experience various games.

How to check the activation date on Apple mobile phone How to check the activation date on Apple mobile phone Mar 08, 2024 pm 04:07 PM

If you want to check the activation date using an Apple mobile phone, the best way is to check it through the serial number in the mobile phone. You can also check it by visiting Apple's official website, connecting it to a computer, and downloading third-party software to check it. How to check the activation date of Apple mobile phone Answer: Serial number query, Apple official website query, computer query, third-party software query 1. The best way for users is to know the serial number of their mobile phone. You can see the serial number by opening Settings, General, About This Machine. . 2. Using the serial number, you can not only know the activation date of your mobile phone, but also check the mobile phone version, mobile phone origin, mobile phone factory date, etc. 3. Users visit Apple's official website to find technical support, find the service and repair column at the bottom of the page, and check the iPhone activation information there. 4. User

How to connect keep to Huawei bracelet How to connect keep to Huawei bracelet Mar 07, 2024 pm 09:46 PM

How to connect keep to Huawei bracelet? You can connect Huawei bracelet in keep software. Most users don’t know how to connect Huawei bracelet. Next is the graphic tutorial of how to connect keep to Huawei bracelet brought by the editor. Interested users come and take a look! How to connect keep to Huawei bracelet 1. First open the keep application, click [Me] in the lower right corner of the main page to enter the special area, and select [Smart Hardware]; 2. Then challenge to the My Smart Device function page, click [Add Device] in the middle; 3. Then on the page of selecting the device you want to add, select the [Smart Bracelet/Watch] function; 4. Finally, on the interface shown below, click on the Huawei watch model to connect.

Three Ways to Fix Edge Your Connection Isn't Private Three Ways to Fix Edge Your Connection Isn't Private Mar 13, 2024 pm 01:30 PM

When you use the Edge browser to access web pages, have you ever encountered a prompt that your connection is not a dedicated connection, causing web browsing to fail? How is this going? Many friends don’t know how to deal with this problem. You can take a look at the following three solutions. Method 1 (simple and crude): In the edge browser, you can try to solve the problem of the website being inaccessible by entering the settings and turning off the security function, and then blocking location permissions in the website permissions. It is important to note that the effectiveness and duration of this approach may vary, and specific effects cannot be determined. After restarting your browser, you can try visiting the website to see if the issue is resolved. Method 2: Adjust the keyboard to English input

See all articles