sqlParameters VS的String.Format这是速度的提升更好? [英] sqlParameters vs string.Format which is better for speed increase?

查看:191
本文介绍了sqlParameters VS的String.Format这是速度的提升更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlParameters VS的String.Format这是速度的提升更好?

而不承担安全

  cmd.CommandText =INSERT INTO tbl_test VALUES(@a,@b);;
的SqlParameter的SqlParameter [] sqlParameters =新的SqlParameter [2];
sqlParameters [0] =新的SqlParameter(@一,SqlDbType.VarChar);
sqlParameters [0] .value的=翻;
sqlParameters [1] =新的SqlParameter(@ B,SqlDbType.VarChar);
sqlParameters [1]。价值= varB;
 

VS

 查询字符串=的String.Format(INSERT INTO tbl_test VALUES({0},{1});,翻,varB);
cmd.CommandText =查询;
 

解决方案

通过参数的更快的在几乎所有情况下:还有的同一个的SQL的关系数据库管理系统,以优化

  INSERT INTO tbl_test
       VALUES(@a,@b)
 

所以,服务器可以解析和优化查询的一次的,然后就的执行的吧。相反,如果不使用参数,关系数据库管理系统具有的解析和优化的一样,很多稍微不同的查询:

  INSERT INTO tbl_test
       值(1,2)
  ...
  INSERT INTO tbl_test
       VALUES(3,4)
  ...
  INSERT INTO tbl_test
       VALUES(100,101)
 

恒解析/优化的是耗费时间的。

sqlParameters vs string.Format which is better for speed increase?

Without assuming the security

cmd.CommandText = "INSERT INTO tbl_test VALUES(@a, @b);";
SqlParameter  SqlParameter[] sqlParameters = new SqlParameter[2];
sqlParameters[0] = new SqlParameter("@a", SqlDbType.VarChar);
sqlParameters[0].Value = varA;
sqlParameters[1] = new SqlParameter("@b", SqlDbType.VarChar);
sqlParameters[1].Value = varB;

vs

string query = string.Format("INSERT INTO tbl_test VALUES({0},{1});", varA, varB);
cmd.CommandText = query;

解决方案

Using parameters is faster in almost all cases: there's one and the same SQL for RDMS to optimize:

  INSERT INTO tbl_test 
       VALUES(@a, @b)

So server can parse and optimize the query once and then just run it. On the contrary, if you don't use parameters, RDMS has to parse and optimize many slightly different queries like that:

  INSERT INTO tbl_test 
       VALUES(1, 2)
  ...
  INSERT INTO tbl_test 
       VALUES(3, 4)
  ...
  INSERT INTO tbl_test 
       VALUES(100, 101)

and that constant parsing/optimizing is time consuming.

这篇关于sqlParameters VS的String.Format这是速度的提升更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆