当前位置:网站首页>Other new features of mysql18-mysql8
Other new features of mysql18-mysql8
2022-07-06 10:43:00 【Protect our party a Yao】
One . MySQL8 Overview of new features
MySQL from 5.7 The version was released directly 8.0 edition , It can be seen that this is an exciting milestone version .MySQL 8 The version has made significant improvements and enhancements in function , Developer pair MySQL The source code is reconstructed , The most prominent point is more MySQL Optimizer The optimizer has been improved . Not only has it improved in speed , It also brings users better performance and better experience .
1.1. MySQL8.0 New features
- Easier NoSQL Support NoSQL Generally refers to non relational database and data storage . With the rapid development of Internet platform , The traditional relational database can not meet the demand . from 5.6 Version start ,MySQL Start supporting simple NoSQL Storage function .MySQL 8 This function has been optimized , Achieve... In a more flexible way NoSQL function , No longer rely on patterns (schema).
- Better index In the query , Using index correctly can improve the efficiency of query .MySQL 8 Added in Hide index and descending index . Hidden indexes can be used to test the impact of removing indexes on query performance . When multiple column indexes are mixed in the query , Using descending index can improve the performance of query .
- More perfect JSON Support MySQL from 5.7 Start supporting native JSON Data storage ,MySQL 8 This function has been optimized , Added aggregate function JSON_ARRAYAGG() and JSON_OBJECTAGG() , Aggregate parameters into JSON Array or object , New inline operator ->>, Is the column path operator -> The enhancement of , Yes JSON The ranking has been improved , And optimized JSON Update operation for .
- Security and account management MySQL 8 Added in caching_sha2_password Licensing plug-ins 、 role 、 Password history and FIPS Mode support , These features improve the security and performance of the database , Make the database administrator more flexible in account management .
- InnoDB The change of InnoDB yes MySQL The default storage engine , Is the preferred engine for transactional databases , Support transaction security (ACID), Support row locking and foreign keys . stay MySQL 8 In the version ,InnoDB Increasing 、 Indexes 、 encryption 、 Deadlock 、 A lot of work has been done on sharing locks and so on Improve and optimize , And support atomic data definition language (DDL), Improved data security , Better support for transactions .
- The data dictionary Prior to MySQL In the version , Dictionary data is stored in metadata files and non transaction tables . from MySQL 8 The transaction data dictionary has been added , Database object information is stored in this dictionary , These data dictionaries are stored in internal transaction tables .
- Atomic data definition statement MySQL 8 Start supporting atomic data definition statements (Automic DDL), namely atom DDL . at present , Only InnoDB The storage engine supports atomic DDL. Atomic data definition statement (DDL) Will work with DDL Operation related data dictionary update 、 Storage engine operation 、 Binary log writes are combined into a single atomic transaction , This makes even if the server crashes , Transactions are also committed or rolled back . Tables created using a storage engine that supports atomic operations , In execution DROP TABLE、CREATE TABLE、ALTER TABLE、RENAME TABLE、TRUNCATE TABLE、CREATE TABLESPACE、DROP TABLESPACE When waiting for operation , All support atomic operations , That is, the transaction is either fully successful , Or roll back after failure , No more partial submissions . From MySQL 5.7 Copied to the MySQL 8 Statements in version , You can add IF EXISTS or IF NOT EXISTS Statement to avoid errors .
- Resource management MySQL 8 Start supporting the creation and management of resource groups , Allows threads running in the server to be assigned to specific groups , So that the thread can execute according to the resources available in the group . Group properties can control the resources in the group , Enable or limit resource consumption within the group . The database administrator can change these properties appropriately according to different workloads . at present ,CPU Time is a controllable resource , from “ fictitious CPU” This concept represents , The term contains CPU The number of core , hyper-threading , Hardware threads and so on . The server determines which virtual machines are available at startup CPU Number . The database administrator with corresponding permissions can transfer these data CPU Associated with a resource group , And assign threads to the resource group . The resource group component is MySQL Resource group management in provides SQL Interface . The properties of the resource group are used to define the resource group .MySQL There are two default groups in , System group and user group , The default group cannot be deleted , Its properties cannot be changed . For user-defined groups , All properties can be initialized when creating a resource group , Remove names and types , Other properties can be changed after creation . On some platforms , Or some MySQL The configuration of , The function of resource management will be limited , Not even available . for example , If the thread pool plug-in is installed , Or use macOS System , Resource management will be unavailable . stay FreeBSD and Solaris In the system , Resource thread priority will be invalidated . stay Linux In the system , Only configuration CAP_SYS_NICE attribute , Resource management priorities work .
- Character set support MySQL 8 The default character set in consists of latin1 Change to utf8mb4 , And for the first time, the collection used specifically by Japanese ,utf8mb4_ja_0900_as_cs.
- Optimizer enhancement MySQL The optimizer began to support hidden and descending indexes . Hidden indexes are not used by the optimizer , There is no need to drop the index when verifying the necessity of the index , Hide the index first , If the optimizer has no performance impact, you can actually delete the index . A descending index allows the optimizer to sort multiple columns , And allow sorting order .
- Common table expression Common table expression (Common Table Expressions) Referred to as CTE,MySQL Recursive and non recursive forms are now supported CTE.CTE By means of SELECT Statement or other specific statements Use WITH Statement on the temporary result set Name it .
- Window function MySQL 8 Start supporting window functions . Most existing in previous versions Aggregate functions stay MySQL 8 Can also be used as a window function .
The name of the function | describe |
---|---|
CUME_DIST() | Cumulative distribution value |
DENSE_RANK() | Continuously sort the current record |
FIRST_VALUE() | Return the corresponding field value of the first line of the window |
LAG() | Return to the front of the corresponding field N rows |
LAST_VALUE() | Return the corresponding field value of the record at the end of the window |
LEAD() | Return the last... Of the corresponding field N rows |
NTH_VALUE() | Back to page N Field values of records |
NTILE() | Divide the area into N Group , And return the number of values |
PERCENT_RANK() | return 0 To 1 Decimal between , Indicates the ranking of a field value in the data partition |
RANK() | Return the corresponding ranking of each record in the partition |
ROW_NUMBER() | Return the serial number corresponding to each record , And not repeated |
- Regular expression support MySQL stay 8.0.4 Later versions will support Unicode The international component library implements regular expression operation , This way can not only provide complete Unicode Support , And it's multi byte security coding .MySQL Added REGEXP_LIKE()、EGEXP_INSTR()、REGEXP_REPLACE() and REGEXP_SUBSTR() And other functions to improve performance . in addition ,regexp_stack_limit and regexp_time_limit System variables can control resource consumption through the matching engine .
- Internal temporary watch TempTable The storage engine replaces MEMORY The storage engine becomes the default storage engine for internal temporary tables .TempTable The storage engine is VARCHAR and VARBINARY Columns provide efficient storage .internal_tmp_mem_storage_engine The session variable defines the storage engine of the internal temporary table , There are two optional values ,TempTable and MEMORY, among TempTable Is the default storage engine .temptable_max_ram The system configuration item defines TempTable The maximum amount of memory that the storage engine can use .
- logging stay MySQL 8 The error log subsystem in consists of a series of MySQL Components . These components are made up of system variables log_error_services To configure the , It can filter and write log events .
- Backup the lock The new backup lock allows data manipulation statements to be executed during online backup , At the same time, prevent operations that may cause inconsistent snapshots . The new backup lock is by LOCK INSTANCE FOR BACKUP and UNLOCK INSTANCE Syntax support , Performing these operations requires backup administrator privileges .
- Enhanced MySQL Copy MySQL 8 Replication support for JSON file Partially updated Binary logging , This record Use a compact binary format , So as to save complete records JSON Document space . When using statement based logging , This compact logging is done automatically , And by putting new binlog_row_value_options The system variable value is set to PARTIAL_JSON To enable .
1.2. MySQL8.0 Remove old features
stay MySQL 5.7 If the application developed on version uses MySQL8.0 Removed features , The statement may fail , Or produce different execution results . To avoid these problems , For applications that use the remove feature , Every effort should be made to correct and avoid the use of these features , And do
Alternative methods may be used .
- The query cache
The query cache has been removed , The deleted items are :
(1) sentence :FLUSH QUERY CACHE and RESET QUERY CACHE.
(2) System variables :query_cache_limit、query_cache_min_res_unit、query_cache_size、
query_cache_type、query_cache_wlock_invalidate.
(3) State variables :Qcache_free_blocks、Qcache_free_memory、Qcache_hits、Qcache_inserts、Qcache_lowmem_prunes、Qcache_not_cached、Qcache_queries_in_cache、Qcache_total_blocks.
(4) Thread state :checking privileges on cached query、checking query cache for query、invalidating query cache entries、sending cached result to client、storing result in query cache、waiting for query cache lock. - Encryption related The encryption related contents deleted are :ENCODE()、DECODE()、ENCRYPT()、DES_ENCRYPT() and DES_DECRYPT() function , Configuration item des-key-file, System variables have_crypt,FLUSH Of the statement DES_KEY_FILE Options ,HAVE_CRYPT CMake Options . For removed ENCRYPT() function , Consider using SHA2() replace , For other removed functions , Use AES_ENCRYPT() and AES_DECRYPT() replace .
- Spatial function correlation stay MySQL 5.7 In the version , The space is marked obsolete by more than one function . These outdated functions are MySQL 8 All have been removed , Only the corresponding ST_ and MBR function .
- \N and NULL stay SQL In the sentence , The parser will no longer \N As NULL, So in SQL Statement should use NULL Instead of \N. This change will not affect the use of LOAD DATA INFILE perhaps SELECT…INTO OUTFILE Import and export operation files . In such operations ,NULL Still equivalent to \N.
- mysql_install_db stay MySQL In distribution , Removed mysql_install_db Program , The initialization of data dictionary needs to be called with –initialize perhaps –initialize-insecure Option mysqld Instead of realizing . in addition ,–bootstrap and INSTALL_SCRIPTDIRCMake Has also been deleted .
- General partition handler The universal partition handler has been removed from MySQL Removed from service . In order to achieve a given table partition , The storage engine used by the table needs its own partition handler . Provide local partition support MySQL The storage engine has two , namely InnoDB and NDB, And in the MySQL 8 China only supports InnoDB.
- System and state variable information stay INFORMATION_SCHEMA In the database , The system and state variable information is no longer maintained .GLOBAL_VARIABLES、SESSION_VARIABLES、GLOBAL_STATUS、SESSION_STATUS All tables have been deleted . in addition , System variables show_compatibility_56 Has also been deleted . The deleted state variables are Slave_heartbeat_period、Slave_last_heartbeat,Slave_received_heartbeats、Slave_retried_transactions、Slave_running. The above deleted contents can be replaced by the corresponding contents in the performance mode .
- mysql_plugin Tools mysql_plugin Tools are used to configure MySQL Server plug-ins , Has now been deleted , You can use –plugin-load or –plugin-load-add Option loads the plug-in when the server starts or is used at run time INSTALL PLUGIN Statement to load the plug-in to replace the tool .
Two . New characteristics 1: Window function
2.1. Use the window function to compare before and after
Suppose I have such a data table now , It shows the sales of a shopping website in each city and each district :
CREATE TABLE sales(
id INT PRIMARY KEY AUTO_INCREMENT,
city VARCHAR(15),
county VARCHAR(15),
sales_value DECIMAL
);
INSERT INTO sales(city,county,sales_value)
VALUES
(' Beijing ',' haidian ',10.00),
(' Beijing ',' The rising sun ',20.00),
(' Shanghai ',' Whampoa ',30.00),
(' Shanghai ',' Changning ',10.00);
Inquire about :
select * from sales;
demand : Now calculate the total sales of this website in each city 、 Total sales across the country 、 The ratio of sales in each district to sales in the city , And as a percentage of total sales .
If you use grouping and aggregation functions , It takes several steps to calculate .
First step , Calculate the total sales amount , And save it in the temporary form a:
create table a
select sum(sales_value) as sales_value
from sales;
Check the temporary table a :
select * from a ;
The second step , Calculate the total sales of each city and store it in the temporary table b:
create table b
select city,sum(sales_value) as sales_value
from sales
group by city;
Check the temporary table a :
select * from b;
The third step , Calculate the proportion of sales in each district to the total amount of the city , And proportion in total sales amount . We can get the required results through the following connection query :
SELECT s.`city` AS City ,s.`county` AS District ,s.`sales_value` AS District Sales ,
b.`sales_value` AS City Sales , s.`sales_value`/b.`sales_value` AS City proportion ,
a.`sales_value` AS Total sales , s. `sales_value` /a.`sales_value` AS Total proportion
FROM
sales s JOIN b ON s.`city` = b.`city`
JOIN a
ORDER BY s.`city`,b.`sales_value`
Results show : City sales amount 、 Proportion of market sales 、 Total sales amount 、 The proportion of total sales has been calculated .
Same query , If you use the window function , It's much simpler . We can use the following code to implement :
SELECT city AS City ,county AS District ,sales_value AS Area sales area ,
SUM(sales_value) OVER(PARTITION BY city) AS City Sales ,
sales_value/SUM(sales_value) OVER (PARTITION BY city) AS Market to market ratio ,
SUM(sales_value) OVER() AS Total sales ,
sales_value/SUM(sales_value) OVER() AS Total ratio
FROM sales s
ORDER BY city,county;
Results show , We got the same result as the query above .
Use window functions , It takes only one step to complete the query . and , Since no temporary table is used , The efficiency of execution is also higher . Obviously , In this scenario, the results of grouping statistics are used to calculate each record , Using window functions is better .
2.2. Window function classification
MySQL from 8.0 The version began to support window functions . The window function is similar to grouping data in a query , The difference is , Grouping operation will aggregate the results of grouping into a record , The window function is to put the result in each data record .
Window functions can be divided into Static window function and Dynamic window function .
- The window size of the static window function is fixed , It won't be different because of different records ;
- The window size of dynamic window function will change with different records .
Mysql Official window function address :https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html#function_row-number
Window functions can be divided into sequence number functions as a whole 、 Distribution function 、 Before and after function 、 Head and tail functions and other functions , The following table :
2.3. Grammatical structure
The syntax structure of window function is :
function OVER([PARTITION BY Field name ORDER BY Field name ASC|DESC])
Or is it :
function OVER Window name ... WINDOW Window name AS ([PARTITION BY Field name ORDER BY Field name ASC|DESC])
- OVER Keyword specifies the scope of the function window .
- If you omit the contents in the following brackets , The window will contain WHERE All records of conditions , The window function will be based on all that meet WHERE The conditions are recorded for calculation .
- If OVER The bracket after the keyword is not empty , You can use the following syntax to set the window .
- Window name : Set an alias for the window , Used to identify windows .
- PARTITION BY Clause : Specifies which fields window functions are grouped by . After grouping , The window function can be executed separately in each group .
- ORDER BY Clause : Specifies which fields the window function sorts by . Execute the sorting operation to make the window function number in the order of the sorted data records .
- FRAME Clause : Define rules for a subset of partitions , Can be used as a sliding window .
2.4. Classified explanation
Create table :
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
category_id INT,
category VARCHAR(15),
NAME VARCHAR(30),
price DECIMAL(10,2),
stock INT,
upper_time DATETIME
);
Add data :
INSERT INTO goods(category_id,category,NAME,price,stock,upper_time)
VALUES
(1, ' Women's wear / Women's Boutique ', 'T T-shirt ', 39.90, 1000, '2020-11-10 00:00:00'),
(1, ' Women's wear / Women's Boutique ', ' dress ', 79.90, 2500, '2020-11-10 00:00:00'),
(1, ' Women's wear / Women's Boutique ', ' fleece ', 89.90, 1500, '2020-11-10 00:00:00'),
(1, ' Women's wear / Women's Boutique ', ' A pair of jeans ', 89.90, 3500, '2020-11-10 00:00:00'),
(1, ' Women's wear / Women's Boutique ', ' Pleated skirt ', 29.90, 500, '2020-11-10 00:00:00'),
(1, ' Women's wear / Women's Boutique ', ' Woolen coat ', 399.90, 1200, '2020-11-10 00:00:00'),
(2, ' Outdoor sports ', ' Bicycle ', 399.90, 1000, '2020-11-10 00:00:00'),
(2, ' Outdoor sports ', ' Mountain Bike ', 1399.90, 2500, '2020-11-10 00:00:00'),
(2, ' Outdoor sports ', ' Alpenstocks ', 59.90, 1500, '2020-11-10 00:00:00'),
(2, ' Outdoor sports ', ' Riding equipment ', 399.90, 3500, '2020-11-10 00:00:00'),
(2, ' Outdoor sports ', ' Sport coat ', 799.90, 500, '2020-11-10 00:00:00'),
(2, ' Outdoor sports ', ' Skate ', 499.90, 1200, '2020-11-10 00:00:00');
The following for goods Table to verify the function of each window function .
2.5. Ordinal function
2.5.1.ROW_NUMBER() function
ROW_NUMBER() The function can display the sequence numbers in the data in order .
give an example : Inquire about goods The information of each commodity in descending order under each commodity category in the data table .
SELECT ROW_NUMBER() OVER(PARTITION BY category_id ORDER BY price)
AS row_num,id,category_id,category,NAME,price,stock
FROM goods;
give an example : Inquire about goods The highest price under each commodity category in the data sheet 3 Product information .
select * from (
select row_number() over(partition by category_id order by price desc)
as row_num,id,category_id,category,name,price,stock
from goods) t
where row_num<=3;
In the name of “ Women's wear / Women's Boutique ” In the commodity category of , The price of two products is 89.90 element , They are sweaters and jeans . The serial number of both products should be 2, Not one for 2, For another 3. here , have access to RANK() Functions and DENSE_RANK() Function solution .
2.5.2. RANK() function
Use RANK() Function can sort serial numbers in parallel , And it will skip the repeated serial numbers , For example, the serial number is 1、1、3.
give an example : Use RANK() Function to obtain goods The price of each category in the data table is sorted from high to low .
SELECT RANK() OVER(PARTITION BY category_id ORDER BY price DESC)
AS rank_number,id,category_id,category,NAME,price,stock
FROM goods;
give an example : Use RANK() Function to obtain goods The category in the data table is “ Women's wear / Women's Boutique ” The highest price 4 Product information .
select * from (
select rank() over (partition by category_id order by price desc)
as rank_number,id,category_id,category,name,price,stock
from goods) t
where t.rank_number<=4
and t.category_id=1;
You can see , Use RANK() The sequence number obtained by the function is 1、2、2、4, The serial number of goods with the same price is the same , The subsequent item serial number is discontinuous , Skipped duplicate sequence numbers .
2.5.3. DENSE_RANK() function
DENSE_RANK() Function to sort serial numbers in parallel , And won't skip repeated sequence numbers , For example, the serial number is 1、1、2.
give an example : Use DENSE_RANK() Function to obtain goods The price of each category in the data table is sorted from high to low .
SELECT DENSE_RANK() OVER (PARTITION BY category_id ORDER BY price DESC)
AS dense_rank_number,id,category_id,category,NAME,price,stock
FROM goods;
give an example : Use DENSE_RANK() Function to obtain goods The category in the data table is “ Women's wear / Women's Boutique ” The highest price 4 Product information .
SELECT * FROM (
SELECT DENSE_RANK() OVER (PARTITION BY category_id ORDER BY price DESC)
AS dense_rank_number,id,category_id,category,NAME,price,stock
FROM goods) t
WHERE t.dense_rank_number<=4
AND t.category_id=1;
You can see , Use DENSE_RANK() The line number obtained by the function is 1、2、2、3, The serial number of goods with the same price is the same , The subsequent item numbers are continuous , And didn't skip the repeated serial numbers .
2.6. Distribution function
2.6.1. PERCENT_RANK() function
PERCENT_RANK() The function is a percentage function of the rank value . Calculate as follows .
(rank - 1) / (rows - 1)
among ,rank The value of is used RANK() The sequence number generated by the function ,rows The value of is the total number of records in the current window .
give an example : Calculation goods The name in the data table is “ Women's wear / Women's Boutique ” Of goods under the category of PERCENT_RANK value .
SELECT RANK() OVER(PARTITION BY category_id ORDER BY price DESC) AS r,
PERCENT_RANK() OVER(PARTITION BY category_id ORDER BY price DESC) AS pr,
id, category_id, category, NAME, price, stock
FROM goods
WHERE category_id=1;
perhaps
select rank() over w as r,
percent_rank() over w as pr,
id, category_id, category, NAME, price, stock
from goods
where category_id=1 window w as(PARTITION BY category_id ORDER BY price
DESC);
2.6.2. CUME_DIST() function
CUME_DIST() Function is mainly used to query the proportion less than or equal to a value .
give an example : Inquire about goods The proportion in the data sheet that is less than or equal to the current price .
SELECT CUME_DIST() OVER(PARTITION BY category_id ORDER BY price ASC) AS cd,
id, category, NAME, price
FROM goods
2.7. Before and after function
2.7.1. LAG(expr,n) function
LAG(expr,n) Function returns the first... Of the current line n Yes expr Value .
give an example : Inquire about goods The difference between the previous commodity price and the current commodity price in the data table .
SELECT id,category,NAME,price,pre_price,price-pre_price AS diff_price
FROM (
SELECT id,category,NAME,price,LAG(price,1) OVER w AS pre_price
FROM goods
WINDOW w AS (PARTITION BY category_id ORDER BY price)) t;
2.7.2. LEAD(expr,n) function
LEAD(expr,n) The function returns the last... Of the current line n Yes expr Value .
give an example : Inquire about goods The difference between the price of the last commodity in the data table and the current commodity price .
SELECT id, category, NAME, behind_price, price,behind_price - price AS
diff_price
FROM(
SELECT id, category, NAME, price,LEAD(price, 1) OVER w AS behind_price
FROM goods WINDOW w AS (PARTITION BY category_id ORDER BY price)) t;
2.8. Head and tail functions
2.8.1. FIRST_VALUE(expr) function
FIRST_VALUE(expr) The function returns the first expr Value .
give an example : Sort by price , Query the first 1 Price information of a commodity .
SELECT id, category, NAME, price, stock,FIRST_VALUE(price) OVER w AS
first_price FROM goods WINDOW w AS (PARTITION BY category_id ORDER BY price);
2.8.2. LAST_VALUE(expr) function
LAST_VALUE(expr) The function returns the last expr Value .
give an example : Sort by price , Query the price information of the last commodity .
SELECT id, category, NAME, price, stock,LAST_VALUE(price) OVER w AS last_price
FROM goods
WINDOW w AS (PARTITION BY category_id ORDER BY price);
2.9. Other functions
2.9.1. NTH_VALUE(expr,n) function
NTH_VALUE(expr,n) Function returns the second n individual expr Value .
give an example : Inquire about goods Ranked... In the data table 2 And the 3 Price information for .
SELECT id, category, NAME, price,NTH_VALUE(price,2) OVER w AS second_price,
NTH_VALUE(price,3) OVER w AS third_price
FROM
goods WINDOW w AS (PARTITION BY category_id ORDER BY price);
2.9.2. NTILE(n) function
NTILE(n) The function divides the ordered data in the partition into n A barrel , Record the bucket number .
give an example : take goods The goods in the table are divided into 3 Group .
SELECT NTILE(3) OVER w AS nt,id, category, NAME, price
FROM goods WINDOW w AS (PARTITION BY category_id ORDER BY price);
2.10. Small junction
The characteristic of window function is that it can be grouped , Sort within groups . in addition , The window function will not reduce the number of rows in the original table due to grouping , This is very useful for us to make statistics and sort on the basis of the original table data .
3、 ... and . New characteristics 2: Common table expression
Common table expression ( Or universal table expression ) Referred to as CTE(Common Table Expressions).CTE Is a named temporary result set , The scope is the current statement .CTE It can be understood as a reusable sub query , Of course, it is a little different from sub query ,CTE You can quote other CTE, But subqueries cannot reference other subqueries . therefore , Consider replacing subqueries . According to the grammatical structure and execution mode , Common table expressions are divided into Common table expressions and Recursive common table expression 2 Kind of .
3.1. Common table expressions
The syntax structure of a common table expression is :
WITH CTE name
AS ( Subquery )
SELECT|DELETE|UPDATE sentence ;
Common table expressions are similar to subqueries , however , Unlike subqueries , It can be referenced many times , And can be referenced by other common table expressions .
give an example : Query the details of the employee's Department .
SELECT * FROM dapart
WHERE department_id IN (
SELECT DISTINCT department_id
FROM employees);
This query can also be completed in the form of ordinary common table expressions :
with emp_dept_id
as (select distinct department_id from employees)
select * from dapart d join emp_dept_id e
on d.department_id = e.department_id;
Example illustrate , Common table expressions can act as subqueries . In the future, if you need to use sub query , You can check before , First define the common table expression , Replace it with a subquery . and , Compared with sub query , Common table expressions have one advantage , Is the query after defining the common table expression , Common table expressions can be referenced multiple times like a table , Subqueries cannot .
3.2. Recursive common table expression
Recursive common table expression is also a common table expression , It's just , In addition to the characteristics of common table expressions , It also has its own characteristics , That is, you can call yourself . Its grammatical structure is :
WITH RECURSIVE
CTE name AS ( Subquery )
SELECT|DELETE|UPDATE sentence ;
The recursive common table expression consists of 2 Part of it is made up of , They are seed query and recursive query , Through the keyword in the middle UNION [ALL] Connect . The seeds here , It means to get the initial value of recursion . This query will only run once , To create the initial dataset , After that, the recursive query will always execute , Until no new query data is generated , Recursively returns .
Case study : Needles are often used for employees surface , contain employee_id,last_name and manager_id Three fields . If a yes b The manager of , that , We can b be called a Subordinates of , If at the same time b again c The manager of , that c Namely b Subordinates of , yes a My subordinates .
Let's try to list the information of all employees with subordinate identity with query statement .
If we use the knowledge we have learned before to solve , It's more complicated , At least 4 Only one query can be done :
- First step , First find the first generation of managers , People who don't take anyone else as their manager , Store the results in a temporary table ;
- The second step , Find out all the people who take the first generation of managers as managers , Get a subordinate set , Store the results in a temporary table ;
- The third step , Find all the following people who are managers , Get a subordinate set , Store the results in a temporary table .
- Step four , Find out who all the following subordinates are managers , Get a result set .
If the result set of step 4 is empty , Then the calculation is over , The result set of the third step is the subordinate set we need , Otherwise, you must continue with step 4 , Until the result set is empty . For example, the data table above , You need to go to step five , To get an empty result set . and , Finally, there is the sixth step : Merge the result set of step 3 and step 4 , In this way, we can finally get the result set we need .
If you use a recursive common table expression , It's very simple . Introduce the specific ideas . - Use the seed query in the recursive common table expression , Find the first generation of managers . Field n Means generation , The initial value is 1, First generation Manager .
- Use recursive query in recursive common table expression , Find out who is the manager in this recursive common table expression , And the value of generation plus 1. Until no one takes the person in the recursive common table expression as the manager , Recursively returns .
- In the final query , Select all generations greater than or equal to 3 People who , They must be subordinates of the third generation and above , That is, subordinates . So we get the result set we need .
It seems that here too 3 Step , It's actually a query 3 Parts of , It only needs to be executed once . And there is no need to use temporary tables
Save intermediate results , It's much simpler than the method just now .
Code implementation :
WITH RECURSIVE cte
AS
(
SELECT employee_id,last_name,manager_id,1 AS n FROM employees WHERE employee_id = 100
-- Seed query , Find the first generation of leaders
UNION ALL
SELECT a.employee_id,a.last_name,a.manager_id,n+1 FROM employees AS a JOIN cte
ON (a.manager_id = cte.employee_id) -- recursive query , Find people who are led by people who use recursive common table expressions
)
SELECT employee_id,last_name FROM cte WHERE n >= 3;
All in all , Recursive common table expressions are useful for querying tree structured data with common root nodes , Very useful . It can be unrestricted by hierarchy , Easily find out the data of all nodes . If you use other query methods , It's more complicated .
3.3. Small junction
The function of common table expression is to replace sub query , And can be quoted many times . Recursive common table expressions are very efficient for querying tree structured data with a common root node , You can easily handle queries that are difficult to handle by other query methods .
边栏推荐
- Adaptive Bezier curve network for real-time end-to-end text recognition
- Ueeditor internationalization configuration, supporting Chinese and English switching
- [Li Kou 387] the first unique character in the string
- First blog
- 基于Pytorch肺部感染识别案例(采用ResNet网络结构)
- Security design verification of API interface: ticket, signature, timestamp
- Have you mastered the correct posture of golden three silver four job hopping?
- Yum prompt another app is currently holding the yum lock; waiting for it to exit...
- MySQL21-用戶與權限管理
- CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
猜你喜欢
35 is not a stumbling block in the career of programmers
A necessary soft skill for Software Test Engineers: structured thinking
CSDN问答标签技能树(一) —— 基本框架的构建
Complete web login process through filter
Ueeditor internationalization configuration, supporting Chinese and English switching
Case identification based on pytoch pulmonary infection (using RESNET network structure)
Mysql36 database backup and recovery
Mysql35 master slave replication
CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
[Li Kou 387] the first unique character in the string
随机推荐
MNIST implementation using pytoch in jupyter notebook
Technology | diverse substrate formats
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
[programmers' English growth path] English learning serial one (verb general tense)
Mysql23 storage engine
MySQL21-用戶與權限管理
MySQL35-主从复制
Mysql28 database design specification
Windchill configure remote Oracle database connection
[untitled]
使用OVF Tool工具从Esxi 6.7中导出虚拟机
C language string function summary
C语言标准的发展
MySQL33-多版本并发控制
MySQL21-用户与权限管理
MySQL底层的逻辑架构
Global and Chinese market for intravenous catheter sets and accessories 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL24-索引的数据结构
Solve the problem of remote connection to MySQL under Linux in Windows
Time in TCP state_ The role of wait?