mysqlconnection ado.net高cpu使用率

2nbm6dog  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(409)

我有一个多线程应用程序,其中n个线程正在访问mysql数据库。每个查询将创建自己的mysqlconnection维护,以避免共享mysqldatareader。但是,我们的数据库服务器遇到了cpu峰值。我已经尝试运行show processlist,但是结果中没有慢查询。我怀疑根本原因是线程如何处理连接。
下面是我的查询方法示例。

  1. public List<Configuration> GetAlertConfigByDeviceID(string deviceID)
  2. {
  3. List<Configuration> configurations = new List<Configuration>();
  4. try
  5. {
  6. using (MySqlConnection dbConnection = new MySqlConnection(WinAlerterConfig.WebConnectionString))
  7. {
  8. dbConnection.Open();
  9. using (MySqlCommand dbCommand = dbConnection.CreateCommand())
  10. {
  11. dbCommand.CommandText = StoredProcedures.SqlGetAlertCfgDetailByDeviceID;
  12. dbCommand.Parameters.Add(new MySqlParameter("DeviceID", deviceID));
  13. using (MySqlDataReader dbResultSet = dbCommand.ExecuteReader())
  14. {
  15. if (dbResultSet.HasRows)
  16. {
  17. while (dbResultSet.Read())
  18. {
  19. configurations.Add(new Configuration()
  20. {
  21. ConfigID = dbResultSet.GetUInt64Safe(0),
  22. AlertType = dbResultSet.GetUInt64Safe(1),
  23. ZoneID = dbResultSet.GetUInt64Safe(2),
  24. Timeout = dbResultSet.GetInt64Safe(3),
  25. DeviceID = dbResultSet.GetStringSafe(4),
  26. FromZoneID = dbResultSet.GetUInt64Safe(5),
  27. OtherZoneAlert = dbResultSet.GetBooleanSafe(6),
  28. Remarks = dbResultSet.GetStringSafe(7)
  29. });
  30. }
  31. }
  32. dbResultSet.Close();
  33. dbResultSet.Dispose();
  34. }
  35. dbCommand.Dispose();
  36. }
  37. dbConnection.Close();
  38. dbConnection.Dispose();
  39. }
  40. }
  41. catch (MySqlException ex)
  42. {
  43. Logger.LogErrorLine("GetAlertConfigByDeviceID: {0} {1} {2}", ex.Message, Environment.NewLine, ex.StackTrace);
  44. }
  45. catch (Exception ex)
  46. {
  47. Logger.LogErrorLine("GetAlertConfigByDeviceID: {0} {1} {2}", ex.Message, Environment.NewLine, ex.StackTrace);
  48. }
  49. return configurations;
  50. }

我还包括“polling=true在我们的连接字符串中。所以我假设mysql或ado.net将处理连接池。
我的方法错了吗?连接池是否已经由ado.net处理?

myzjeezk

myzjeezk1#

删除所有关闭和处置呼叫。正在使用的块已经在处理了。
还要确保deviceid列上有索引。
然后再试一次。

相关问题