选择数据的最佳方法是什么

o8x7eapl  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(527)

我有一个在线游戏,一切都很好,问题是,当你登录服务器需要太长的时间来选择数据,我使用mysql和我是一个新手在它,我不知道有没有办法优化这个或它只是很慢,这就是它。
这适用于c#和mysql

int UserId;
        DataRow dUserInfo = null;
        DataTable dAchievements = null;
        DataTable dFavouriteRooms = null;
        DataTable dIgnores = null;
        DataTable dBadges = null;
        DataTable dFriends = null;
        DataTable dRequests = null;
        DataTable dRooms = null;
        DataTable dQuests = null;
        DataTable dRelations = null;
        DataRow UserInfo = null;

        using (IQueryAdapter db = Progam.GetQueryReactor())
        {
            db.SetQuery("SELECT * " + "FROM users WHERE `auth_ticket` = @addr LIMIT 1");
            db.AddParameter("addr", SessionTicket);
            dUserInfo = db.getRow();

            UserId = Convert.ToInt32(dUserInfo["id"]);

            db.SetQuery("SELECT `group`,`level`,`progress` FROM `user_achievements` WHERE `userid` = '" + UserId + "'");
            dAchievements = db.getTable();

            db.SetQuery("SELECT room_id FROM user_favorites WHERE `user_id` = '" + UserId + "'");
            dFavouriteRooms = db.getTable();

            db.SetQuery("SELECT ignore_id FROM user_ignores WHERE `user_id` = '" + UserId + "'");
            dIgnores = db.getTable();

            db.SetQuery("SELECT `badge_id`,`badge_slot` FROM user_badges WHERE `user_id` = '" + UserId + "'");
            dBadges = db.getTable();

            db.SetQuery(
                "SELECT users.id,users.username,users.fb_name,users.motto,users.look,users.last_online,users.hide_inroom,users.hide_online " +
                "FROM users " +
                "JOIN messenger_friendships " +
                "ON users.id = messenger_friendships.user_one_id " +
                "WHERE messenger_friendships.user_two_id = " + UserId + " " +
                "UNION ALL " +
                "SELECT users.id,users.username,users.fb_name,users.motto,users.look,users.last_online,users.hide_inroom,users.hide_online " +
                "FROM users " +
                "JOIN messenger_friendships " +
                "ON users.id = messenger_friendships.user_two_id " +
                "WHERE messenger_friendships.user_one_id = " + UserId);
            dFriends = db.getTable();

            db.SetQuery("SELECT messenger_requests.from_id,messenger_requests.to_id,users.username FROM users JOIN messenger_requests ON users.id = messenger_requests.from_id WHERE messenger_requests.to_id = " + UserId);
            dRequests = db.getTable();

            db.SetQuery("SELECT * FROM rooms WHERE `user_id` = '" + UserId + "' ");
            dRooms = db.getTable();

            db.SetQuery("SELECT `q_id`,`progress` FROM user_quests WHERE `user_id` = '" + UserId + "'");
            dQuests = db.getTable();

            db.SetQuery("SELECT `id`,`user_id`,`target`,`type` FROM `user_relationships` WHERE `user_id` = '" + UserId + "'");
            dRelations = db.getTable();

            db.SetQuery("SELECT * FROM `user_info` WHERE `user_id` = '" + UserId + "' LIMIT 1");
            UserInfo = db.getRow();

            if (UserInfo == null)
            {
                db.RunQuery("INSERT INTO `user_info` (`user_id`) VALUES ('" + UserId + "')");

                db.SetQuery("SELECT * FROM `user_info` WHERE `user_id` = '" + UserId + "' LIMIT 1");
                UserInfo = db.getRow();
            }

            db.SetQuery("UPDATE `users` SET `online` = '1', `auth_ticket` = '', `machine_id` = @mid WHERE `id` = @hid LIMIT 1");
            db.AddParameter("mid", MachineId);
            db.AddParameter("hid", UserId);
            db.RunQuery();
        }

这需要很长时间(10秒或更长)

5m1hhzi4

5m1hhzi41#

不要使用“”.“”选择列返回所有列。如果你有大量的水滴,那么它会停下来,因为它会返回这些。通过列出所有列来选择各个列,即使有很多列。
另外,我看不到服务器的响应是什么,但是如果您只返回一行,那么可以考虑将多个查询合并到一个查询中。如果您正在运行所有这些查询,那么您的系统将一个接一个地执行它们,这意味着您在程序等待所有响应时将有一个延迟。将所有查询合并为一个查询,如下所示,因为sql server将一次性获得所有响应,然后返回它们。

SELECT
(SELECT Column1 FROM DatabaseName1 WHERE Column2 = 'Something') AS Info1,
(SELECT Column1 FROM DatabaseName2 WHERE Column2 = 'Something') AS Info2,
(SELECT Column1 FROM DatabaseName3 WHERE Column2 = 'Something') AS Info3

相关问题