This article brings you relevant knowledge that MySQL cannot use uuid as the primary key. MySQL officially recommends not to use uuid or non-continuous and non-repeating snowflake IDs, but recommends continuous self-increasing primary key IDs. The official recommendation is auto_increment, then why is it not recommended to use uuid? I hope it will be helpful to everyone.
Preface
When designing tables in mysql, mysql officially recommends not to use uuid or not Continuous non-repeating snowflake IDs (long-shaped and unique, single-machine increment) are recommended, but continuous self-increasing primary key IDs are recommended. The official recommendation is auto_increment, so why is it not recommended to use uuid? What are the disadvantages of using uuid?
1. MySQL and program examples
##1.1. To explain this problem, let’s first create three tables
They are user_auto_key, user_uuid, and user_random_key respectively, which represent the automatically growing primary key, uuid is used as the primary key, random key is used as the primary key, and we keep the rest completely unchanged.According to For the control variable method, we only generate the primary key of each table using different strategies, while the other fields are exactly the same, and then test the insertion speed and query speed of the table: Note: The random key here is actually Refers to the non-continuous, non-repeating and irregular ids calculated using the snowflake algorithm: a string of 18-bit long values Technical framework: springboot jdbcTemplate junit hutool, program The principle is to connect your own test database, and then write the same amount of data in the same environment to analyze the insert time to comprehensively analyze its efficiency. In order to achieve the most realistic effect, all data are randomly generated. For example, names, emails, and addresses are all randomly generated.
package com.wyq.mysqldemo; import cn.hutool.core.collection.CollectionUtil; import com.wyq.mysqldemo.databaseobject.UserKeyAuto; import com.wyq.mysqldemo.databaseobject.UserKeyRandom; import com.wyq.mysqldemo.databaseobject.UserKeyUUID; import com.wyq.mysqldemo.diffkeytest.AutoKeyTableService; import com.wyq.mysqldemo.diffkeytest.RandomKeyTableService; import com.wyq.mysqldemo.diffkeytest.UUIDKeyTableService; import com.wyq.mysqldemo.util.JdbcTemplateService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.util.StopWatch; import java.util.List; @SpringBootTest class MysqlDemoApplicationTests { @Autowired private JdbcTemplateService jdbcTemplateService; @Autowired private AutoKeyTableService autoKeyTableService; @Autowired private UUIDKeyTableService uuidKeyTableService; @Autowired private RandomKeyTableService randomKeyTableService; @Test void testDBTime() { StopWatch stopwatch = new StopWatch("执行sql时间消耗"); /** * auto_increment key任务 */ final String insertSql = "INSERT INTO user_key_auto(user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?)"; List<UserKeyAuto> insertData = autoKeyTableService.getInsertData(); stopwatch.start("自动生成key表任务开始"); long start1 = System.currentTimeMillis(); if (CollectionUtil.isNotEmpty(insertData)) { boolean insertResult = jdbcTemplateService.insert(insertSql, insertData, false); System.out.println(insertResult); } long end1 = System.currentTimeMillis(); System.out.println("auto key消耗的时间:" + (end1 - start1)); stopwatch.stop(); /** * uudID的key */ final String insertSql2 = "INSERT INTO user_uuid(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)"; List<UserKeyUUID> insertData2 = uuidKeyTableService.getInsertData(); stopwatch.start("UUID的key表任务开始"); long begin = System.currentTimeMillis(); if (CollectionUtil.isNotEmpty(insertData)) { boolean insertResult = jdbcTemplateService.insert(insertSql2, insertData2, true); System.out.println(insertResult); } long over = System.currentTimeMillis(); System.out.println("UUID key消耗的时间:" + (over - begin)); stopwatch.stop(); /** * 随机的long值key */ final String insertSql3 = "INSERT INTO user_random_key(id,user_id,user_name,sex,address,city,email,state) VALUES(?,?,?,?,?,?,?,?)"; List<UserKeyRandom> insertData3 = randomKeyTableService.getInsertData(); stopwatch.start("随机的long值key表任务开始"); Long start = System.currentTimeMillis(); if (CollectionUtil.isNotEmpty(insertData)) { boolean insertResult = jdbcTemplateService.insert(insertSql3, insertData3, true); System.out.println(insertResult); } Long end = System.currentTimeMillis(); System.out.println("随机key任务消耗时间:" + (end - start)); stopwatch.stop(); String result = stopwatch.prettyPrint(); System.out.println(result); }
1.3. Program writing results
It can be seen that in When the amount of data is about 100W, the insertion efficiency of uuid is at the bottom, and when 130W of data is added in the subsequent sequence, the time of uudi plummets again.
The overall efficiency ranking of time usage is: auto_key>random_key>uuid, uuid has the lowest efficiency. When the amount of data is large, the efficiency plummets. So why does this happen? With doubts, let’s discuss this issue:
2. Comparison of index structures using uuid and auto-increment id
2.1. Use the internal structure of the auto-incrementing id
#The values of the auto-incrementing primary key are sequential, so Innodb stores each record after a record . When the maximum fill factor of the page is reached (the default maximum fill factor of InnoDB is 15/16 of the page size, 1/16 of the space will be left for future modifications):
①The next record will be When writing to a new page, once the data is loaded in this order, the primary key page will be filled with nearly sequential records, increasing the maximum fill rate of the page, and there will be no page waste
② The newly inserted row will definitely be in the next row of the original largest data row. MySQL positioning and addressing are very fast, and there will be no extra consumption for calculating the position of the new row.
③Reduce page splits and fragmentation Generation
2.2. Use the index internal structure of uuid
Because uuid has nothing to do with the sequential auto-incrementing id As a rule, the value of a new row is not necessarily greater than the value of the previous primary key, so innodb cannot always insert the new row to the end of the index, but needs to find a new suitable position for the new row so as to to allocate new space.
This process requires a lot of additional operations. The disordered data will cause the data to be scattered and scattered, which will lead to the following problems:
①The target page written is likely to have been refreshed to on disk and removed from the cache, or has not been loaded into the cache, InnoDB has to find and read the target page from disk into memory before inserting it, which will cause a lot of random IO
② Because writing is out of order, InnoDB has to perform page splitting operations frequently in order to allocate space for new rows. Page splitting results in moving a large amount of data, and at least three pages need to be modified for one insertion.
③Due to frequent page splits, pages will become sparse and filled irregularly, which will eventually lead to data fragmentation
Loading random values (uuid and snowflake id) into the cluster After indexing (the default index type of innodb), sometimes you need to do an OPTIMEIZE TABLE to rebuild the table and optimize page filling, which will take a certain amount of time.
Conclusion: When using innodb, you should insert as much as possible in the order of increasing primary keys, and try to use monotonically increasing clustering key values to insert new rows
2.3. Disadvantages of using self-increasing ID
So there is no disadvantage at all in using self-increasing ID? No, self-increasing IDs will also have the following problems:
① Once others crawl your database, they can obtain your business growth information based on the self-increasing IDs of the database, and it is easy to analyze your business growth information. Business situation
② For high concurrent loads, innodb will cause obvious lock contention when inserting according to the primary key. The upper bound of the primary key will become a hot spot for contention, because all insertions occur within Here, concurrent insertion will lead to gap lock competition
③Auto_Increment lock mechanism will cause auto-increment lock grabbing, with a certain performance loss
Attachment: Auto_increment lock competition problem, if you want to improve it Tuning the configuration of innodb_autoinc_lock_mode
3. Summary
This blog first starts with asking questions at the beginning, creating tables, and using jdbcTemplate to test different The id generation strategy performs well in data insertion of large amounts of data, and then analyzes the different mechanisms of id in the index structure and advantages and disadvantages of mysql, and explains in depth why uuid and random non-repeating id have a performance loss in data insertion, in detail explained the problem.
In actual development, it is best to use self-increasing ID according to the official recommendation of mysql. Mysql is broad and profound, and there are many internal points worthy of optimization that we need to learn.
Recommended learning: mysql video tutorial
The above is the detailed content of Let's talk about why MySQL cannot use uuid as the primary key. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.