注册

系统访问接口,请求获取数据库数据列表,返回时快时慢

麦田里的守望者 2026/04/30 86 1

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:dm8
【操作系统】:项目部署到linux环境
【CPU】:
【问题描述】*:项目后端使用C#编写,使用sqlSuger连接数据库
连接配置

"DbConfigs": [
    {
      "Conn": "dK2XC/1ILA6h+/eSq812kF6CP6Kflugfa8j6MQvjzrxeKE1rRncatAIIbe+WDFusuNg0c5fjmPMIhTrCF4s40MxfvjOSiNwpJM9oy00DgV0=",
      "ConnRelease": "dK2XC/1ILA6h+/eSq812kF6CP6Kflugfa8j6MQvjzrxeKE1rRncatAIIbe+WDFusuNg0c5fjmPMIhTrCF4s40MxfvjOSiNwpJM9oy00DgV0=",
      "ConfigId": "dm",
      "DbType": 5,
      "IsAutoCloseConnection": true
    }
  ]

连接信息解密后为:Data Source=xxx.xxx.xx.5:5236;User ID=WZDD_xxxx;Password=xxxxxxx@2026030910;

/// <summary>
        /// 初始化db
        /// </summary>
        /// <param name="services"></param>
        public static void AddDb(this IServiceCollection services)
        {
            List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("DbConfigs");
            var iocList = new List<IocConfig>();
            foreach (var item in dbConfigs)
            {
                iocList.Add(new IocConfig()
                {
                    ConfigId = item.ConfigId,
#if DEBUG
                    ConnectionString = AES.AesDecrypt(item.Conn, AppSettings.Get<string>("AesDBKey")),
#else
                    ConnectionString = AES.AesDecrypt(item.ConnRelease, AppSettings.Get<string>("AesDBKey")),
#endif
                    DbType = (IocDbType)item.DbType,
                    IsAutoCloseConnection = item.IsAutoCloseConnection
                });
            }

            SugarIocServices.AddSqlSugar(iocList);
            SugarIocServices.ConfigurationSugar(db =>
            {
                var showDbLog = AppSettings.Get<bool>("ShowDbLog");
                foreach (var item in iocList)
                {
                    string configId = Convert.ToString(item.ConfigId) ?? string.Empty;
                    if (string.IsNullOrWhiteSpace(configId))
                    {
                        continue;
                    }

                    var dbScope = db.GetConnectionScope(configId);
#if !DEBUG
                    var dbConfig = dbConfigs.FirstOrDefault(x => x.ConfigId == configId);
                    if (dbConfig != null)
                    {
                        dbScope.CurrentConnectionConfig.SlaveConnectionConfigs = BuildSlaveConnectionConfigs(dbConfig);
                    }
#endif

                    dbScope.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        if (showDbLog)
                        {
                            string log = $"【{configId} SQL语句】{UtilMethods.GetSqlString((DbType)item.DbType, sql, pars)}\n";
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.WriteLine(log);
                            Console.ResetColor();
                        }
                    };
                    dbScope.Aop.OnError = (ex) =>
                    {
                        string errorSql = "【错误SQL】" + UtilMethods.GetSqlString((DbType)item.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n";
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"{errorSql}\r\n{ex.Message}\r\n{ex.StackTrace}");
                        Console.ResetColor();
                    };
                }
            });
        }

由于是新手,向各位大牛询问下分析方法以及解决拌饭

回答 0
暂无回答
扫一扫
联系客服