当前位置:网站首页>The SQL query statement executes select (1) first, and the ByteDance algorithm engineer is interviewed
The SQL query statement executes select (1) first, and the ByteDance algorithm engineer is interviewed
2022-06-21 13:53:00 【Programmer hyperspace】
SELECT
< Back to the data list > # The returned single column must be in group by clause , Except aggregate functions
DISTINCT
# Data De duplication
ORDER BY
< Sorting conditions > # Sort
LIMIT
< Row limit >
- Actually , When the engine performs each of these steps , Will form a virtual table in memory , And then do the following operation on the virtual table , And free the memory of the unused virtual table , And so on .
Specific explanation :( notes : below “VT” Express → Virtual table virtual )
from:**select * from table_1, table_2; And select * from table_1 join table_2; The result is the same , They all mean Cartesian product ;** It is used to directly calculate the Cartesian product of two tables , Get the virtual table VT1, This is all. select Statement is the first operation to be performed , Other operations are performed on this table , That is to say from What the operation accomplished
on: from VT1 Filter the data that meets the criteria in the table , formation VT2 surface ;
join: Will be join Type of data added to VT2 In the table , for example left join The remaining data from the left table will be added to the virtual table VT2 in , formation VT3 surface ; If the number of tables is greater than 2, It will repeat 1-3 Step ;
where: Perform screening ,( You can't use aggregate functions ) obtain VT4 surface ;
group by: Yes VT4 Tables are grouped , obtain VT5 surface ; After processing the statement , Such as select,having, The columns used must be included in group by In the condition , Those that don't appear need aggregate functions ;
having: Filter grouped data , obtain VT6 surface ;
select: Return the column to get VT7 surface ;
distinct: Used to get rid of duplication VT8 surface ;
order by: Used to sort to get VT9 surface ;
limit: Returns the number of rows required , obtain VT10;
It should be noted that :
- group by In the condition , Each column must be a valid column , It can't be an aggregate function ;
- null Values are also returned as a group ;
- Except for aggregate functions ,select Columns in clause must be in group by In the condition ;
The above shows us what a query will return , meanwhile , Also answer the following questions :
Can be in GRROUP BY Then use WHERE Do you ?( no way ,GROUP BY Is in WHERE after !)
Can I filter the results returned by window functions ?( no way , The window function is SELECT In the sentence , and SELECT Is in WHERE and GROUP BY after )
Can be based on GROUP BY What's going on in ORDER BY Do you ?( Sure ,ORDER BY Basically at the end of the execution , So it can be based on anything ORDER BY)
LIMIT When is the execution ?( In the end !)
however , The database engine does not have to be executed strictly in this order SQL Inquire about , Because in order to execute queries faster , They will make some optimizations , These questions are explained below ↓↓↓.
SQL The alias in will affect SQL Execution order ?
======================
The following parties SQL Shown :
SELECT CONCAT(first_name, ' ', last_name) AS full_name, count(*)
FROM table
GROUP BY full_name
From this statement , As if GROUP BY Is in SELECT After that , Because it quotes SELECT One of the aliases in . But it doesn't have to be , The database engine will rewrite the query like this ↓↓↓:
SELECT CONCAT(first_name, ' ', last_name) AS full_name, count(*)
FROM table
GROUP BY CONCAT(first_name, ' ', last_name)
therefore , such GROUP BY Still execute first .
in addition , The database engine will also do a series of checks , Make sure SELECT and GROUP BY The things in are effective , So we will check the query before generating the execution plan .
The database is likely to perform queries out of the normal order ( Optimize )
====================
In practice , The database does not necessarily follow JOIN、WHERE、GROUP BY Order to execute the query , Because they do a series of optimizations , Disorganize the order of execution , So that the query can execute faster , As long as the query results are not changed .
This query shows why queries need to be executed in different order :
SELECT * FROM
dept d LEFT JOIN student s ON d.student_id = s.id
# Pleasantly surprised
Finally, I prepared a set of interview questions corresponding to the above materials ( There's an answer ) And the high-frequency interview algorithm questions during the interview ( If the interview preparation time is not enough , Then concentrate on these algorithm problems , Hit rate 85%+)


I also prepared a set of interview questions corresponding to the above materials ( There's an answer ) And the high-frequency interview algorithm questions during the interview ( If the interview preparation time is not enough , Then concentrate on these algorithm problems , Hit rate 85%+)
[ Outside the chain picture transfer in ...(img-o24VTF6k-1628591532162)]
[ Outside the chain picture transfer in ...(img-KvLHOxFR-1628591532164)]
**[ Data acquisition method : Get it free of charge here ](https://gitee.com/vip204888/java-p7)**
边栏推荐
- MySQL - table join and join
- Detailed explanation of dictionary source code in C #
- SCCM creates a client collection based on the installed app and periodically pushes application updates
- Chapter IX Cisco ASA application nat
- 3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
- How to use search engine?
- Redis学习(1)——概述和常用命令
- [Goo Goo donkey takeout rebate system] customer service configuration tutorial of takeout CPS project - (attached with picture and text building tutorial)
- Which futures trading platform is more secure. Ask for recommendation.
- Swift return button
猜你喜欢

C language elementary level (IX) enumeration

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log

3D slicer saves segmentation results

618 Nuggets digital collection? Burberry and other luxury brands fight against metauniverse

Visualization strategy of Seaborn data overall distribution

IMU selection, calibration error analysis, AHRS integrated navigation

Configuration of oracle19c under alicloud lightweight application server linux-centos7
MySQL constraints (descriptions of various conditions when creating tables)

Explanation of vim, makefile and GDB tools

Detailed explanation of dictionary source code in C #
随机推荐
Use of MySQL 8.0.19 under alicloud lightweight application server linux-centos7
Atguigu---- conditional rendering
A blazor webassembly application that can automatically generate page components based on objects or types
8 most common SQL error usages
Test the interface between app and wechat applet
Oracle client11 and pl/sql12 installation
MySQL - table constraints
Is the micro securities account opening regular and safe? How to open an account?
[untitled]
Making my footprint map using API
Lamp architecture 5 - MySQL Cluster and master-slave structure
What is Devops in an article?
Automatic operation and maintenance 3 - using playbook in ansible
Navigation bar switching, message board, text box losing focus
Swift return button
Alibaba cloud link tracking is on the Net project (Jaeger trace)
4. procedure flow structure
MySQL - adding, deleting, querying and modifying tables
Consultation: what kind of securities company is micro securities? Is it safe to open an account?
【课程作业】浮点运算分析及精度改进