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("隨機主鍵插入");
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM