MySqlConnector icon indicating copy to clipboard operation
MySqlConnector copied to clipboard

Performance degradation when create a connection per each small request

Open stretyak opened this issue 1 year ago • 1 comments
trafficstars

Software versions MySqlConnector version: 2.3.7 Server type (MySQL, MariaDB, Aurora, etc.) and version: MySQL 5.6.43-log .NET version: netcoreapp3.1 (Optional) ORM NuGet packages and versions:

Describe the bug Open a MySqlConnection connection, create a command, excecute a reader repeate the same step 10000 times see how slow it is.

at the same time if you open a connection once and then create 10000 commands using the same connection the job is done dramatically faster.

The connection string is simple. Polling is activated. the test is a simple one thread test.

At my laptop the difference between 2 below sample is a minute the slow sample takes 1 minute the fast sample takes 2 seconds.

What am I doing wrong?

Code sample

the below is very **fast**:
		using (var connection = new MySqlConnection(connStr))
		{
			connection.Open();

			for (int i = 0; i < 10000; i++)
			{
				var cmd = new MySqlCommand("a_proc_that_returns_no_rows", connection)
				{
					CommandType = System.Data.CommandType.StoredProcedure,
				};

				using (var r = cmd.ExecuteReader())
				{
				}
			}
		
but the below one is much **slower**

		for (int i = 0; i < 10000; i++)
		{
			using (var connection = new MySqlConnection(connStr))
			{
				connection.Open();
				var cmd = new MySqlCommand("a_proc_that_returns_no_rows", connection)
				{
					CommandType = System.Data.CommandType.StoredProcedure,
				};

				using (var r = cmd.ExecuteReader())
				{
				}
			}
		}


Expected behavior The results should be almost the same because the polling is activated and it is advised to create a connection each time and not to keep it opened and to reuse.

stretyak avatar Oct 24 '24 18:10 stretyak