简体   繁体   English

如何在MySQL数据库上以高查询率优化Java程序

[英]How to optimize Java program with high rate of queries on MySQL Database

I am currently developing a game for education purposes. 我目前正在开发用于教育目的的游戏。

The game is a small chat where each player has a figure. 游戏是一个小型聊天,每个玩家都有一个人物。 This figure has its position written in a MySQL database, and updates every time a players moves his figure. 该角色的位置已写入MySQL数据库,并且每次玩家移动其角色时都会更新。

Currently, every 60 frames, the game updates all the players positions and changes them in the gameclient. 当前,游戏每60帧更新一次所有玩家位置,并在gameclient中更改它们。 The performance of the database is not my only concern, as this causes the game framerate to drop to around 30-50 fps. 数据库的性能不是我唯一关心的问题,因为这会使游戏帧率下降到30-50 fps左右。

Obviously, this generates 1 query for position updates a second (Game runs at 60fps), per player. 显然,这会为每个玩家生成1个位置更新查询(游戏以60fps运行)。 Thinking a little larger than me and a few friends as users, i could imagine this may be a problem. 作为用户,我的想法比我和几个朋友要大,我可以想象这可能是个问题。

Would a standard rented webserver be able to handle this? 标准租用的网络服务器可以处理此问题吗? How could i improve it? 我该如何改善? (It can't update the positions less than once a second) (它不能少于每秒更新一次位置)

I hope you have some ideas :-) 我希望你有一些想法:-)

This sort of data is not really saved to a database after every single change, instead it's kept in some kind of in-memory context so that it's much faster to access. 每次更改后,此类数据实际上并没有真正保存到数据库中,而是保留在某种内存中上下文中,以便更快地进行访问。 What you store in a database is things that change less frequently, like on the order of once per minute. 您存储在数据库中的事物发生变化的频率较低,例如每分钟一次。

What you might do is snapshot the current in-memory context to the database on a regular schedule. 您可能要做的是定期将当前的内存上下文快照到数据库。 That is to say every minute or so you would "persist" the game state so that if the engine crashed it could restore from that point. 也就是说,每分钟左右,您将“保持”游戏状态,以便在引擎崩溃时可以从该点恢复。 If you create the proper architecture, this would be no more difficult than calling a method periodically. 如果创建适当的体系结构,这将比定期调用方法困难。

Certain things may be persisted immediately to avoid people exploiting this lag between an action occurring and the record of that action being permanent. 某些事情可能会立即保留下来,以避免人们利用发生的动作与该动作的记录永久之间的时间差。 For example, any transactions with in-game currency would be recorded immediately and the in-memory cache would not be trusted for these. 例如,任何具有游戏内货币的交易都将立即被记录下来,并且内存缓存将不被信任。

For low numbers of players, like under a hundred, you should have no problem with using a commodity VPS. 对于数量不多的玩家(例如不到100名),使用商品VPS应该没有问题。 To host more players you would have to be very careful to be efficient. 要接待更多的玩家,您必须非常小心才能提高效率。

You can use an in-memory Java database, which allows you to update the players more frequently. 您可以使用内存Java数据库,该数据库使您可以更频繁地更新播放器。 Changes are then stored in the MySQL database at a less frequent rate. 然后将更改以较低的频率存储在MySQL数据库中。

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

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