mysql使用UUID自增主键随机主键性能测试

Posted 代码改变的世界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql使用UUID自增主键随机主键性能测试相关的知识,希望对你有一定的参考价值。

根据 这个帖子,对mysql使用UUID主键、自增主键和随即主键进行了一下插入性能测试,创建了三个表:

自增主键:

CREATE TABLE user_key_auto(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT(64) NOT NULL DEFAULT 0,
user_name VARCHAR(64) NOT NULL DEFAULT \'\',
sex INT(2) NOT NULL,
address VARCHAR(255) NOT NULL DEFAULT \'\',
city VARCHAR(64) NOT NULL DEFAULT \'\',
email VARCHAR(64) NOT NULL DEFAULT \'\',
state INT (6) NOT NULL DEFAULT 0, PRIMARY KEY (id), KEY user_name_key(user_NAME) 

) ENGINE = INNODB;

UUID主键:

CREATE TABLE user_uuid(
id VARCHAR(36) NOT NULL ,
user_id BIGINT(64) NOT NULL DEFAULT 0,
user_name VARCHAR(64) NOT NULL DEFAULT \'\',
sex INT(2) NOT NULL,
address VARCHAR(255) NOT NULL DEFAULT \'\',
city VARCHAR(64) NOT NULL DEFAULT \'\',
email VARCHAR(64) NOT NULL DEFAULT \'\',
state INT (6) NOT NULL DEFAULT 0, PRIMARY KEY (id), KEY user_name_key(user_NAME) 

) ENGINE = INNODB;

随机数主键

CREATE TABLE user_random_key(
id BIGINT(64) NOT NULL DEFAULT 0,
user_id BIGINT(64) NOT NULL DEFAULT 0,
user_name VARCHAR(64) NOT NULL DEFAULT \'\',
sex INT(2) NOT NULL,
address VARCHAR(255) NOT NULL DEFAULT \'\',
city VARCHAR(64) NOT NULL DEFAULT \'\',
email VARCHAR(64) NOT NULL DEFAULT \'\',
state INT (6) NOT NULL DEFAULT 0, PRIMARY KEY (id), KEY user_name_key(user_NAME) 

) ENGINE = INNODB;

使用mybatis,写了个test方法,插入10w条数据,测试结果如下:
UUID主键,耗时87秒

随机数主键,耗时58秒

自增主键,耗时35秒

public class MybatisTest {
    private static InputStream inputStream;

    private static SqlSessionFactory sqlSessionFactory;

    private static SqlSession sqlSession;

    private static Instant instant;

    @BeforeAll
    static void init() throws IOException {
        String resource = "mybatis.xml";
        inputStream = Resources.getResourceAsStream(resource);

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();

        instant = Instant.now();
    }

    @AfterAll
    static void close() {
        sqlSession.commit();
        sqlSession.close();
        Instant instantLast = Instant.now();
        System.out.println("Duration: " + Duration.between(instant, instantLast).toMillis() + " ms");
    }

    @Test
    public void testAutoInsert() throws IOException {
        List<UserKeyAuto> userKeyAutoList = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            UserKeyAuto userKeyAuto = UserKeyAuto.builder()
                .userId((long) i)
                .userName("xiaoming")
                .address("金鹰")
                .city("南京")
                .email("qq@qq.com")
                .sex(1)
                .state(1)
                .build();
            userKeyAutoList.add(userKeyAuto);
        }

        String statement = "site.lksky.dao.UserKeyAutoDao.insert";
        userKeyAutoList.forEach(item -> {
            sqlSession.insert(statement, item);
        });
        System.out.println("自增主键插入");
    }

    @Test
    public void testRandomInsert() throws IOException {
        List<UserRandomKey> userKeyAutoList = new ArrayList<>();
        for (int i = 99; i < 100000 + 99; i++) {
            UserRandomKey userKeyAuto = UserRandomKey.builder()
                .id(new Random().nextLong() + 999L)
                .userId((long) i + 1)
                .userName("xiaoming")
                .address("金鹰")
                .city("南京")
                .email("qq@qq.com")
                .sex(1)
                .state(1)
                .build();
            userKeyAutoList.add(userKeyAuto);
        }

        String statement = "site.lksky.dao.UserRandomKeyDao.insert";
        userKeyAutoList.forEach(item -> {
            sqlSession.insert(statement, item);
        });
        System.out.println("随机主键插入");
    }

    @Test
    public void testUuidInsert(){
        List<UserUuid> userKeyAutoList = new ArrayList<>();
        for (int i = 99; i < 100000 + 99; i++) {
            UserUuid userKeyAuto = UserUuid.builder()
                    .id(UUID.randomUUID().toString())
                    .userId((long) i + 1)
                    .userName("xiaoming")
                    .address("金鹰")
                    .city("南京")
                    .email("qq@qq.com")
                    .sex(1)
                    .state(1)
                    .build();
            userKeyAutoList.add(userKeyAuto);
        }

        String statement = "site.lksky.dao.UserUuidDao.insert";
        userKeyAutoList.forEach(item -> {
            sqlSession.insert(statement, item);
        });
        System.out.println("随机主键插入");
    }
}

以上是关于mysql使用UUID自增主键随机主键性能测试的主要内容,如果未能解决你的问题,请参考以下文章

mysql自增主键怎么用

mybatis自增主键返回

MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)

杂谈自增主键用完了怎么办

Mysql 用UUID做主键可行么

MySQL 使用自增ID主键和UUID 作为主键的优劣比較具体过程(从百万到千万表记录測试)