简体   繁体   English

仅当从代码调用时,极其缓慢的mysql连接建立

[英]Extremely slow mysql connection establishment only when called from code

I am currently trying to do something that should be simple and straight-forward - connect to a database server, run a query, see if I get anything back and if so send it back to the user. 我目前正在尝试做一些简单明了的事情-连接到数据库服务器,运行查询,看是否能得到任何东西,如果可以,将其发送回用户。 This is the code I'm using to do it: 这是我正在使用的代码:

MySqlDataReader reader = MySqlHeaper.ExecuteReader(connectionString, $"SELECT * FROM table WHERE insertDateTime > '{DateTime.Now.AddSeconds(-1800).ToString("yyyy-MM-ddTHH:mm:ss")}'";

I have also tried this with a MySqlCommand and MySqlConnection object pair, and either way the result is the same - it takes approximately 7100ms to connect to the MySql server. 我也用MySqlCommandMySqlConnection对象对进行了尝试,两种方法的结果都是相同的-连接到MySql服务器大约需要7100毫秒。 I know that sounds like a problem that should be on ServerFault, but my testing tells me otherwise. 我知道这听起来像应该是ServerFault上的问题,但是我的测试告诉我否则。 When I use the command line MySql client to connect to my database server using exactly the same credentials and run exactly the same query I get my connection established and my data back in nothing flat. 当我使用命令行MySql客户端使用完全相同的凭据连接到我的数据库服务器并运行完全相同的查询时,我建立了连接,数据也恢复了原样。 I don't know at this stage if it's a server setting or not, but here's what I've tried so far: 我目前不知道这是否是服务器设置,但到目前为止,这是我尝试过的操作:

  • Rebooting the server 重新启动服务器
  • Restarting the MySQL server 重新启动MySQL服务器
  • Setting the skip_name_resolve setting to 1 in order to prevent reverse name lookups on connect 将skip_name_resolve设置设置为1,以防止在连接时进行反向名称查找
  • Using alternative means of querying the server (mysql command line client and MySQL Workbench) 使用其他方式查询服务器(MySQL命令行客户端和MySQL Workbench)
  • Opening all AWS IAM permissions on the RDS instance to allow everything from the server 在RDS实例上打开所有AWS IAM权限以允许服务器中的所有内容

Nothing seems to be making any difference, so I'm at a loss to explain this terrible performance. 似乎什么都没有改变,所以我很茫然地解释这种糟糕的表现。 It's also only happening when I open the connection. 这也仅在我打开连接时发生。 Running queries, inserts, what have you is lightning fast. 运行查询,插入内容,您很快就发现了什么。 Any suggestions anyone might have would be most helpful. 任何人可能提出的任何建议都是最有帮助的。

I would not expect IAM permissions to have any impact on performance. 我不希望IAM权限对性能有任何影响。 I would expect them to be either successful or not successful. 我希望他们成功或失败。

I would execute some diagnostic protocols to get more information. 我将执行一些诊断协议以获取更多信息。

1) Try a subsequent query, to see if it is an issue with the stack being initialized. 1)尝试进行后续查询,以查看初始化栈是否有问题。 Are subsequent queries faster? 后续查询是否更快? 2) Try a query that is just an identity query. 2)尝试仅是身份查询的查询。 Something that doesn't require any sort of IO. 不需要任何IO的东西。 3) Try a query from a different platform (maybe a scripting language like ruby or php) 3)尝试从其他平台查询(也许是脚本语言,如ruby或php)

Once you answer those it should help you narrow it down. 一旦回答了这些问题,它就会帮助您缩小范围。

This is most likely caused by Connector/NET executing a slow WMI query to query connection attributes when opening the connection; 这很可能是由于Connector / NET在打开连接时执行慢速WMI查询以查询连接属性所致; this is logged as MySQL bug 80030 . 这被记录为MySQL错误80030

As far as I know, this isn't fixed in newer versions of the driver, but you can work around it by switching to MySqlConnector , an OSS MySQL ADO.NET library. 据我所知,在较新版本的驱动程序中未解决此问题,但您可以通过切换到OSS MySQL ADO.NET库MySqlConnector来解决。

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

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