Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why can't you use uuid as the database primary key

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "Why can't you use uuid as the database primary key". In the daily operation, I believe that many people have doubts about why they can not use uuid as the database primary key. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt that "Why can't you use uuid as the database primary key?" Next, please follow the editor to study!

1. Summary

In daily development, there are three main ways to generate the primary key id in the database.

Database self-increasing ID

Using random numbers to generate non-repetitive ID

Using uuid provided by jdk

For these three schemes, I found that in the case of a small amount of data, there is no special difference, but when the amount of data in a single table reaches more than one million, their performance is significantly different. Theory alone is not good, but also depends on the actual program testing. Today, the editor will take you to find out!

II. Program examples

First, we create three single tables tb_uuid_1, tb_uuid_2, tb_uuid_3 in the local database, and set the primary key of the tb_uuid_1 table to self-growing mode. The script is as follows:

CREATE TABLE `tb_uuid_ 1` (`id` bigint (20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar (20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT=' primary key ID self-growing'; CREATE TABLE `tb_uuid_ 2` (`id` bigint (20) unsigned NOT NULL, `name` varchar (20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT=' primary key ID random number generation CREATE TABLE `tb_uuid_ 3` (`id` varchar (50) NOT NULL, `name` varchar (20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT=' primary key is generated by uuid'

Next, we use Springboot + mybatis to implement the insertion test.

2.1. Database self-increment

Taking the self-increment of database as an example, we first write various entity and data persistence layer operations to facilitate subsequent testing.

/ * Table entity * / public class UUID1 implements Serializable {private Long id; private String name; / / omit set, get} / * * data persistence layer operations * / public interface UUID1Mapper {/ * * self-growing insert * @ param uuid1 * / @ Insert ("INSERT INTO tb_uuid_1 (name) VALUES (# {name})") void insert (UUID1 uuid1) } / * * self-increment ID, unit test * / @ Test public void testInsert1 () {long start = System.currentTimeMillis (); for (int I = 0; I < 1000000; iTunes +) {uuid1Mapper.insert (new UUID1 (). SetName ("Zhang San"));} long end = System.currentTimeMillis (); System.out.println ("time spent:" + (end-start));}

2.2.Using random numbers to generate ID

Here, we use twitter's snowflake algorithm to generate random number ID. The tool classes are as follows:

Public class SnowflakeIdWorker {private static SnowflakeIdWorker instance = new SnowflakeIdWorker (0J0); / * * start time cut (2015-01-01) * / private final long twepoch = 1420041600000L; / * * number of digits occupied by machine id * / private final long workerIdBits = 5L; / * * number of digits occupied by data ID id * / private final long datacenterIdBits = 5L / * the largest machine supported, id, the result is 31 (this shift algorithm can quickly calculate the maximum decimal number that several binary numbers can represent) * / private final long maxWorkerId =-1L ^ (- 1L)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report