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

时间:2018-08-23 22:12:59

标签: c# mysql sql

我有一个在线游戏,一切都很好,问题是,当您登录服务器时,TOO LONG选择数据时,我使用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秒或更长时间)

1 个答案:

答案 0 :(得分:0)

请勿使用“ *”选择列。 '*'返回所有列。如果您有很多BLOB列,则它将停止运行,因为它将返回那些BLOB。通过列出所有列来选择各个列,即使数目很大也是如此。

此外,我看不到服务器的响应,但是,如果仅返回一行,则考虑将多个查询合并为一个查询。如果您正在运行所有这些查询,则系统将一个接一个地执行这些查询,这意味着程序等待所有响应时会有延迟。将所有查询合并到一个类似于以下查询的查询中,因为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