Detailed explanation of new relational database features in MySQL 8.0

Detailed explanation of new relational database features in MySQL 8.0

Preface

The latest version of MySQL 8.0 is 8.0.4 rc, and the official version is expected to be released soon. This article introduces several major new features of 8.0 in relational databases.

You may already know that MySQL has provided NoSQL storage capabilities since version 5.7, and this feature has also been improved in 8.0. However, given that this is rarely used in practice and I have never used it, this article will not introduce this aspect, but will focus on its relational database aspect.

1. Hidden Index

The hidden index feature is very useful for performance debugging. In 8.0, indexes can be "hidden" and "shown". When an index is hidden, it is not used by the query optimizer.

That is, we can hide an index and then observe the impact on the database. If the database performance has declined, it means that the index is useful, so you can "restore it to display"; if the database performance has not changed, it means that the index is redundant and can be deleted.

The syntax for hiding an index is:

ALTER TABLE t ALTER INDEX i INVISIBLE;

The syntax to restore the index is:

ALTER TABLE t ALTER INDEX i VISIBLE;

When an index is hidden, we can see from the output of the show index command that the Visible property value of the index is NO.

Note: When an index is hidden, its content is still updated in real time like a normal index. This feature is specifically for optimization and debugging. If you hide an index for a long time, it is better to delete it, because after all, the existence of the index will affect the performance of insert, update, and delete.

2. Set persistence

MySQL settings can be changed at runtime using the SET GLOBAL command, but this change is only temporary and the database will read the configuration file again the next time it is started.

MySQL 8 adds the SET PERSIST command, for example:

SET PERSIST max_connections = 500;

MySQL will save the configuration of this command to the mysqld-auto.cnf file in the data directory. The next time it is started, it will read the file and use the configuration in it to overwrite the default configuration file.

3. UTF-8 encoding

Starting from MySQL 8, the default encoding of the database will be changed to utf8mb4, which includes all emoji characters. For many years, we have been very careful with encoding when using MySQL, for fear that we would forget to change the default latin and cause garbled characters. You don't have to worry about it from now on.

4. Common Table Expressions

Complex queries use embedded tables, for example:

SELECT t1.*, t2.* FROM 
 (SELECT col1 FROM table1) t1,
 (SELECT col2 FROM table2) t2;

With CTE, we can write:

WITH
 t1 AS (SELECT col1 FROM table1),
 t2 AS (SELECT col2 FROM table2)
SELECT t1.*, t2.* 
FROM t1, t2;

This way, the layers and areas appear clearer, and you will know more clearly which part to change.

For more detailed information about CTE, please see the official documentation.

5. Window Functions

One of the most complained features of MySQL is the lack of a rank() function. When you need to implement ranking in a query, you have to write the @ variable manually. However, starting from 8.0, MySQL added a new concept called window function, which can be used to implement several new query methods.

Window functions are a bit like aggregate functions such as SUM() and COUNT(), but they do not merge multiple rows of query results into one row, but put the results back into multiple rows. In other words, window functions do not require GROUP BY.

Suppose we have a table of "number of students in a class":

mysql> select * from classes;
+--------+-----------+
| name | stu_count |
+--------+-----------+
| class1 | 41 |
| class2 | 43 |
| class3 | 57 |
| class4 | 57 |
| class5 | 37 |
+--------+-----------+
5 rows in set (0.00 sec)

If I want to rank the class size from small to large, I can use the window function like this:

mysql> select *, rank() over was `rank` from classes
 -> window was (order by stu_count);
+--------+-----------+------+
| name | stu_count | rank |
+--------+-----------+------+
| class5 | 37 | 1 |
| class1 | 41 | 2 |
| class2 | 43 | 3 |
| class3 | 57 | 4 |
| class4 | 57 | 4 |
+--------+-----------+------+
5 rows in set (0.00 sec)

Here we create a window named w, specify it to sort the stu_count field, and then execute the rank() method on w in the select clause and output the result as the rank field.

In fact, the creation of window is optional. For example, if I want to add the total number of students in each row, I can do this:

mysql> select *, sum(stu_count) over() as total_count
 -> from classes;
+--------+-----------+-------------+
| name | stu_count | total_count |
+--------+-----------+-------------+
| class1 | 41 | 235 |
| class2 | 43 | 235 |
| class3 | 57 | 235 |
| class4 | 57 | 235 |
| class5 | 37 | 235 |
+--------+-----------+-------------+
5 rows in set (0.00 sec)

What's the point of doing this? In this way, we can find out the proportion of students in each class at one time:

mysql> select *,
 -> (stu_count)/(sum(stu_count) over()) as rate
 -> from classes;
+--------+-----------+--------+
| name | stu_count | rate |
+--------+-----------+--------+
| class1 | 41 | 0.1745 |
| class2 | 43 | 0.1830 |
| class3 | 57 | 0.2426 |
| class4 | 57 | 0.2426 |
| class5 | 37 | 0.1574 |
+--------+-----------+--------+
5 rows in set (0.00 sec)

In the past, this could only be achieved by writing a long and obscure paragraph! More information about window functions can be found here.

How about it? After reading the above introduction, do you have more expectations for MySQL 8.0?

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • MySQL 8.0 New Features - Introduction to Check Constraints
  • In-depth explanation of hidden fields, a new feature of MySQL 8.0
  • Implementation of check constraints in MySQL 8.0
  • Analysis of the new features of MySQL 8.0 - transactional data dictionary and atomic DDL
  • MySQL 8.0 New Features: Hash Join
  • A brief discussion on the pitfalls and solutions of the new features of MySQL 8.0 (summary)
  • MySQL 8.0 new features: support for atomic DDL statements
  • Solution to IDEA not being able to connect to MySQL port number occupation
  • Use MySQL to open/modify port 3306 and open access permissions in Ubuntu/Linux environment
  • Perfect solution to mysql cannot start after phpstudy is installed (no need to delete the original database, no need to change any configuration, no need to change the port) direct coexistence
  • Enable remote access rights for MySQL under Linux and open port 3306 in the firewall
  • MySQL 8.0 New Features - Introduction to the Use of Management Port

<<:  Vue implements 3 ways to switch tabs and switch to maintain data status

>>:  How to delete all contents in a directory using Ansible

Recommend

Priority analysis of and or queries in MySQL

This may be an issue that is easily overlooked. F...

Summary of several commonly used CentOS7 images based on Docker

Table of contents 1 Install Docker 2 Configuring ...

Web design tips on form input boxes

This article lists some tips and codes about form...

React new version life cycle hook function and usage detailed explanation

Compared with the old life cycle Three hooks are ...

How to use Nginx to handle cross-domain Vue development environment

1. Demand The local test domain name is the same ...

Mysql stores tree structure through Adjacency List (adjacency list)

The following content introduces the process and ...

Detailed process of upgrading glibc dynamic library in centos 6.9

glibc is the libc library released by gnu, that i...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

How to use time as a judgment condition in MySQL

Background: During the development process, we of...

A complete guide to CSS style attributes css() and width() in jQuery

Table of contents 1. Basic use of css(): 1.1 Get ...

How to store false or true in MySQL

MySQL Boolean value, stores false or true In shor...

Solutions to MySql crash and service failure to start

I have been in contact with PHP for so long, but ...

How to solve jQuery conflict problem

In front-end development, $ is a function in jQue...

Detailed explanation of the use of redux in native WeChat applet development

premise In complex scenarios, a lot of data needs...