mysql 失败事务的可伸缩性
我有一张表,用于存储从一个用户到另一个用户的消息。消息(用户 ID、朋友 ID、消息、创建日期)。我的主键是(friend_id,created_date)。这可以防止重复消息(据我所知),因为它们将无法插入。
现在这还可以,因为我的代码每次为每个用户生成大约 20 个这样的查询,而我只有一个用户。但是,如果有数百或数千个用户,这是否会因所有失败的事务而在我的数据库中造成瓶颈?如果我可以做哪些事情来改善这种情况?
编辑: 归根结底的问题是我应该使用主键约束,在 mysql 之外进行检查,还是使用其他一些 mysql 功能来将重复项保留在数据库之外?
I have a table that stores messages from one user to another. messages(user_id,friend_id,message,created_date). My primary key is (friend_id,created_date). This prevents duplicate messages (AFAIK) because they will fail to insert.
Right now this is ok because my code generates about 20 of these queries at a time per user and I only have one user. But if there were hundreds or thousands of users would this create a bottleneck in my database with all the failed transactions? And if what kinds of things could I do to improve the situation?
EDIT:
The boiled down question is should I use the primary key constraint,check outside of mysql, or use some other mysql functionality to keep duplicates out of the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该没问题,因为 mysql 只会在内部进行主键查找并忽略记录(我假设您正在使用 INSERT IGNORE)。如果你在插入之前检查它们是否存在,那么当你插入时mysql仍然会再次检查。这意味着如果大多数插入都会成功,那么您就可以节省额外的检查。如果绝大多数插入失败(不太可能),那么不发送不必要的数据所节省的成本可能会超过偶尔的重复检查。
Should be fine as mysql will just do a primary key lookup internally and ignore the record (I'm assuming you're using INSERT IGNORE). If you were checking if they exist before inserting, mysql will still check again when you insert. This means if most inserts are going to succeed, then you're saving an extra check. If the vast majority of inserts were failing (not likely) then possibly the savings from not sending unnecessary data would outweigh the occasional repeated check.