简体   繁体   English

寻求用于访问唯一 MySQL 条目的高性能解决方案

[英]Seeking a performant solution for accessing unique MySQL entries

I know very little about MySQL (or web development in general).我对 MySQL(或一般的 web 开发)知之甚少。 I'm a Unity game dev and I've got a situation where users (of a region the size of which I haven't decided yet, possibly globally) can submit entries to an online database.我是一名 Unity 游戏开发人员,我遇到这样一种情况,用户(我尚未决定的区域大小,可能是全球性的)可以将条目提交到在线数据库。 The users must be able to then locate their entry at any time.然后,用户必须能够随时找到他们的条目。

For this reason, I've generated a guid from.Net (System.Guid.NewGuid()) and am storing that in the database entry.出于这个原因,我从.Net (System.Guid.NewGuid()) 生成了一个 guid,并将其存储在数据库条目中。 This works for me.这对我有用。 However.., I'm no expert.但是..,我不是专家。 but my gut tells me that looking up a complex string in what could be a gargantuan table might have terrible performance.但我的直觉告诉我,在可能是一张巨大的桌子上查找一个复杂的字符串可能会有很糟糕的表现。

That said, it doesn't seem like anything other than a globally unique identifier will solve my problem.也就是说,除了全局唯一标识符之外,似乎没有其他任何东西可以解决我的问题。 Is there a more elegant solution that I'm not seeing, or a way to mitigate against any issues this design pattern might create?有没有我没有看到的更优雅的解决方案,或者有一种方法可以缓解这种设计模式可能产生的任何问题?

Thanks!谢谢!

Make sure you define the GUID column as the primary key in the MySQL table.确保将 GUID 列定义为 MySQL 表中的主键。 That will cause MySQL to create an index on it, which will enable MySQL to quickly find a row given the GUID.这将导致 MySQL 在其上创建索引,这将使 MySQL 能够快速找到给定 GUID 的行。 The table might be gargantuan but (assuming a regular B-tree index) the time required for a lookup will increase logarithmically relative to the size of the table.该表可能很大,但是(假设是常规 B 树索引)查找所需的时间将相对于表的大小以对数方式增加。 In other words, if it requires 2 reads to find a row in a 1,000-row table, finding a row in a 1,000,000-row table will only require 2 more reads, not 1,000 times as many.换句话说,如果在 1,000 行表中查找一行需要 2 次读取,那么在 1,000,000 行表中查找一行将只需要 2 次读取,而不是 1,000 倍。

As long as you have defined the primary key, the performance should be good.只要你定义了主键,性能应该是不错的。 This is what the database is designed to do.这就是数据库的设计目的。

Obviously there are limits to everything.显然,一切都是有限度的。 If you have a billion users and they're submitting thousands of these entries every second, then maybe a regular indexed MySQL table won't be sufficient.如果您有 10 亿用户并且他们每秒提交数千个此类条目,那么常规索引 MySQL 表可能不够用。 But I wouldn't go looking for some exotic solution before you even have a problem.但我不会 go 在您遇到问题之前寻找一些奇特的解决方案。

If you have a key of the row you want, and you have an index on that key, then this query will take less than a second, even if the table has a billion rows:如果你有一个你想要的行的键,并且你在那个键上有一个索引,那么这个查询将花费不到一秒钟的时间,即使表有十亿行:

SELECT ... FROM t WHERE id = 1234.

The index in question might be the PRIMARY KEY , or it could be a secondary key.有问题的索引可能是PRIMARY KEY ,也可能是辅助键。

GUIDs/UUIDs should be used only if you need to manufacture unique ids in multiple clients without asking the database for an id.仅当您需要在多个客户端中制造唯一 id 而无需向数据库询问 id 时,才应使用 GUID/UUID。 If you do use such, be aware that GUIDs perform poorly if the table is bigger than RAM.如果您确实使用了此类,请注意如果表大于 RAM,则 GUID 的性能会很差。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM