MySQL 主键和 VARCHAR(255)

发布于 2024-12-26 10:54:56 字数 63 浏览 2 评论 0原文

我有一个使用 VARCHAR(255) 作为表主键的数据库,它们看起来像 GUID。 Int 的性能不是更好吗?

I have a database using VARCHAR(255) as its primary key on tables and they look like GUIDs. Wouldnt an Int be better for performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2025-01-02 10:54:56

这取决于您的存储引擎,但一般来说 int/bigint 会更好。如果您使用 innodb,则由于聚集索引的工作方式,uuid/guid 作为主键是一个糟糕的选择。阅读此博客以了解更多信息。总而言之,键是按范围存储的,并且由于 uuid 是随机的,因此它们会降低插入和查找的效率,因为您会通过读取和写入每行的整个内存块来破坏缓存。

It depends on your storage engine, but generally speaking an int/bigint would be better. If you are using innodb, a uuid/guid is a bad choice for a primary key because of the way a clustered index works. read this blog to learn more about it. To sum it up, keys are stored by range and since uuids' are random they would make inserts and lookups less efficient since you would thrash the cache with reading and writing whole memory blocks for each row.

甜点 2025-01-02 10:54:56

整数占用的磁盘空间更少,因此搜索时需要的 I/O 更少。只要范围满足您的需要,我会说 int 会更快。

Ints take less space on disk so you need less I/O when searching. As long as the range suits your need I would say that an int would be faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文