简体   繁体   English

插入 UUID 主键索引的性能

[英]Inserting performance for UUID primary_key index

I have read that the insert performance for UUID as a primary_key depends on how many records exists in the database.我读到 UUID 作为 primary_key 的插入性能取决于数据库中存在的记录数。 With the greater number of records the more time it taking to insert records记录数量越多,插入记录所需的时间就越多

https://kccoder.com/mysql/uuid-vs-int-insert-performance/ https://kccoder.com/mysql/uuid-vs-int-insert-performance/

The author for the above article has run experiments, he thinks its because there are greater number of reads for UUID primary_key insert than other auto incremented integers.上面文章的作者进行了实验,他认为这是因为 UUID primary_key insert 的读取次数比其他自增整数要多。

I wanted to ask whether databases automatically create an index for the the chosen primary_key If the UUID primary_key index is the problem, would the insert problem be solved by using an integer as the primary_key index while still keeping the UUID field, then switching the primary keys to UUID, the problem then is would the foreign key references to integer ID propagate to the UUID field upon the switching?我想问一下数据库是否自动为选择的primary_key创建索引如果UUID的primary_key索引有问题,插入问题是否可以通过使用整数作为primary_key索引同时保留UUID字段,然后切换主键来解决到 UUID,那么问题是对整数 ID 的外键引用是否会在切换时传播到 UUID 字段?

whether databases automatically create an index for the the chosen primary_key数据库是否自动为选定的 primary_key 创建索引

Yes.是的。 Primary keys are always indexed.主键总是被索引。 In some databases, the index is a clustered index (meaning that the data on the data pages is actually sorted by the key).在某些数据库中,索引是聚集索引(意味着数据页上的数据实际上是按键排序的)。 In other databases, the index is to enforce the uniqueness constraint.在其他数据库中,索引是强制执行唯一性约束的。

The issue with UUIDs and indexes is that new values are not necessarily larger than older values. UUID 和索引的问题在于新值不一定大于旧值。 That means that an insert is typically going "between" two existing records.这意味着插入通常在两个现有记录之间进行。 When this happens you have fragmentation -- that is, existing data pages need to make room for new records and sometimes even split into two pages.发生这种情况时,您会产生碎片——也就是说,现有数据页需要为新记录腾出空间,有时甚至分成两页。

An auto-incremented integer does not have this problem.自增整数没有这个问题。 It to is indexed (and clustered in MySQL).它被索引(并在 MySQL 中聚集)。 However, the newer values are bigger than any existing value -- the definition of auto-incrementing.但是,较新的值比任何现有值都大——自动递增的定义。 So, new values go at the end.所以,新的价值会在最后。 Fragmentation is only an issue when rows are deleted or if you manually set the value.碎片仅在行被删除或手动设置值时才会出现。

I would recommend that you use auto-incremented primary keys unless you have a specific reason for UUIDs.我建议您使用自动递增的主键,除非您有使用 UUID 的特定原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 脱机同步(将性能UUID作为主键) - Offline synchronization (Performance UUID as a primary key) golang gin gorm插入并设置primary_key但primary_key为空 - golang gin gorm insert and set primary_key but primary_key got null 字段 'index_id' 没有默认值”在尝试使用 primary_key 作为 Hibernate 中的复合键的一部分时 - Field 'index_id' doesn't have a default value" when trying to use the primary_key as part of composite key in Hibernate 对 UUID 主键使用字符串类型与 uuid 类型相比,对性能有何影响? - What is the performance hit of using a string type vs a uuid type for a UUID primary key? SQL Alchemy:表 primary_key - 意外的关键字参数错误 - SQL Alchemy : Table primary_key - unexpected keyword argument error 如何使用Python获取数据库中最后插入的记录的primary_key - How to get primary_key of last inserted record in database with Python 如何在数据库级别获取primary_key的列名? - How to get column name for primary_key at database level? 在Laravel 5中使用UUID作为主键 - Using a UUID as primary key with Laravel 5 Laravel 4使用UUID作为主键 - Laravel 4 using UUID as primary key TypeORM MariaDB 作为 UUID 的主键 - TypeORM MariaDB Primary Key as UUID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM