mssqlserver2016内存表测试
测试环境:
windows2012, sqlserver2016, 64G内存的pc服务器,接口:c# .netframework4.6
内存表是:schema_only
测试程序和数据库服务器在同一主机
插入10万条guid字符串(ID列),纯单条insert语句插入:
磁盘表:57s
内存表:12s
根据表主键ID查询1000次ID并汇总时间:
磁盘表:170ms (直接select id, bh where id='333')
内存表:170ms(直接select id, bh where id='333')
内存表:250ms (本地编译的存储过程,"p1 {lst[m]} )
内存表:170ms (本地编译的存储过程, cmd.Parameters[0].Value = lst[0];)
结论:
插入速度提高很多,如果按照本地编译存储过程,可能插入更快,参考:https://blog.csdn.net/yenange/article/details/32705347
查询速度,是否用本地编译的存储过程,感觉差不多,我主要是想模拟key-value情况,查询也是按主键查询的,速度和磁盘表没有明显变化
考虑到内存表的诸多限制,如表长度不超过8000多,也不清楚内存何时释放,有人说只有drop表时也释放,delete 表中记录是不释放内存占用的。
还不如直接用磁盘表方便可控
代码参考:
private void button3_Click(object sender, EventArgs e)
{
lst.Clear();
for(int m=0;m<100000;m++)
{
lst.Add(Guid.NewGuid().ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
//普通表插入
this.插入表("table_1");
}
private void button2_Click(object sender, EventArgs e)
{
//普通表查询
SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnstr);
cn.Open();
SqlCommand cmd = cn.CreateCommand();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DateTime t = DateTime.Now;
for (int m=0;m<1000;m++)
{
cmd.CommandText = $"select * from table_1 where id ='{lst[m]}'";
da.Fill(ds, "t");
}
cn.Close();
this.textBox1.Text = (DateTime.Now - t).TotalMilliseconds.ToString();
}
private void button5_Click(object sender, EventArgs e)
{
//内存表插入
this.插入表("t_mem");
}
private void button4_Click(object sender, EventArgs e)
{
//内在表查询
SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnstr);
cn.Open();
SqlCommand cmd = cn.CreateCommand();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DateTime t = DateTime.Now;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = $"p1";
cmd.Parameters.Add("@id", SqlDbType.VarChar);
for (int m = 0; m < 1000; m++)
{
cmd.Parameters[0].Value = lst[0];
da.Fill(ds, "t");
}
cn.Close();
this.textBox1.Text = (DateTime.Now - t).TotalMilliseconds.ToString();
//SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnstr);
//cn.Open();
//SqlCommand cmd = cn.CreateCommand();
//SqlDataAdapter da = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//DateTime t = DateTime.Now;
//for (int m = 0; m < 1000; m++)
//{
// cmd.CommandText = $"p1 {lst[m]}'";
// da.Fill(ds, "t");
//}
//cn.Close();
//this.textBox1.Text = (DateTime.Now - t).TotalMilliseconds.ToString();
//SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnstr);
//cn.Open();
//SqlCommand cmd = cn.CreateCommand();
//SqlDataAdapter da = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//DateTime t = DateTime.Now;
//for (int m = 0; m < 1000; m++)
//{
// cmd.CommandText = $"select * from t_mem where id ='{lst[m]}'";
// da.Fill(ds, "t");
//}
//cn.Close();
//this.textBox1.Text = (DateTime.Now - t).TotalMilliseconds.ToString();
}
private void 插入表(string tab)
{
SqlConnection cn = new System.Data.SqlClient.SqlConnection(cnstr);
cn.Open();
SqlCommand cmd = cn.CreateCommand();
DateTime t = DateTime.Now;
foreach (string str in lst)
{
cmd.CommandText = $"insert into {tab}(id,bh) values('{str}','2')";
cmd.ExecuteNonQuery();
}
cn.Close();
this.textBox1.Text = (DateTime.Now - t).TotalMilliseconds.ToString();
}
}
}