.net - ASP.NET、MySQL 与 SQL Server 中的数据库连接

标签 .net asp.net mysql sql-server connection-pooling

我在我目前正在处理的 ASP.NET 项目中使用 MySQL,我做了一些测试来测试 MySQL .NET 提供程序的性能,但遗憾的是我对结果不是很满意。

一个只打开连接的非常简单的循环在 SQL Server 中快了 10 倍:

// MySQL

const string CONNECTION_STRING = 
"server=localhost;database=testdb;user id=root;password=mypassword;max pool size=250;";
for (int i = 0; i < 5000; i++)
{
  using (MySqlConnection con = new MySqlConnection(CONNECTION_STRING))
  {
    con.Open();
  }
}

// SQL Server

const string CONNECTION_STRING = "Data Source=localhost;Initial Catalog=testdb;Integrated Security=True;max pool size=250;";
for (int i = 0; i < 5000; i++)
{
  using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
  {
    con.Open();
  }
}

SQL Server 在其他方面也快得多(选择、更新、插入等)。我做错了什么吗?是否有任何我应该更改的服务器变量?

更多信息:
- 我在 Windows 上运行 MySQL (5.0.51a-community-nt)
- 测试中使用了SQL Server 2005
- 规范:Windows XP SP2,CPU Intel 1.6GHz 双核,1024 MB RAM

这是MySQL的配置:

[client]
port        = 3306
socket      = /tmp/mysql.sock

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
skip-locking
key_buffer = 384M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8

server-id   = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[isamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

感谢任何建议...

最佳答案

SQL 服务器默认使用连接池:每个使用完全相同的连接字符串打开的连接在关闭时返回到池中。从池中返回连接比从头开始创建连接要高效得多。我假设 MySql 默认不提供连接池

关于.net - ASP.NET、MySQL 与 SQL Server 中的数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/491484/

相关文章:

c# - 用户定义的转换错误

c# - "Failed to load view state"动态加载用户控件时出错

c# - 尝试更改 asp.net 中默认 login.aspx 页面内的标签文本

mysql - SQL:查找计算字段中的最大行数

mysql - SQL : Using a variable within a text field

c# - 如何将子元素作为解码字符串添加到 XElement?

.net - Powershell将Excel日期读取为5位数字

c# - 创建 FileStream.Open c# 的接口(interface)

c# 将 int 缩短为区分大小写的代码

php - 统计与论坛相关的帖子总数 - PHP Logic