+ -
当前位置:首页 → 问答吧 → 感觉使用存储过程没有加快查询的速度,各位大侠能帮忙说一下原因吗

感觉使用存储过程没有加快查询的速度,各位大侠能帮忙说一下原因吗

时间:2011-12-23

来源:互联网

存储过程代码(用于查询,传入一个参数,返回一个查询结果)
create or replace procedure selecttypecount(p_name in varchar2,
  original_count out number) as
begin

  select count(*)
  into original_count
  from (select nvl(b.realcasetype, a.casesubtype) as mycasesubtype
  from alarminfo_perman a,
  (select alarmid, realcasetype
  from returninfo_test
  where id in
  (select max(id) from returninfo_test group by alarmid)) b
  where a.id = b.alarmid(+)) y
  where y.mycasesubtype = p_name;

end selecttypecount;

C#循环调用代码
  OracleConnection conn = new OracleConnection(ClassVar.GlobalData.OracleConnect);  
try
{
  conn.Open();
 for (int x = 0; x < id1.Length; x++)//id1是一个字符串数组
  {
   
  OracleCommand cmd = conn.CreateCommand();
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.CommandText = "selecttypecount";
  cmd.Parameters.Add("p_name", OracleType.VarChar).Direction = ParameterDirection.Input;//指明传入的参数是输入给oracle存储过程用的
  cmd.Parameters["p_name"].Value = id1[x];
  cmd.Parameters.Add("original_count", OracleType.Number).Direction = ParameterDirection.Output;
   
  cmd.ExecuteNonQuery();
  string countNum = cmd.Parameters["original_count"].Value.ToString();
  cell = cells.Add(x + 2, 3, countNum);//excel中的,这个不用管。设定第x+1行,第3例单元格的值 
   
  }
}
catch(Exception ex)
{

}
finally
{
conn.Close();
}

以前我是把sql语句代码直接放在C#中的,觉得很慢。于是改成用存储过程,但是发现最后用的时间差不多,速度没有什么提升,是什么原因。请大侠们赐教

作者: lp244392323   发布时间: 2011-12-23

晕倒,速度的提升不是靠放到存储过程里就可以了,要进行SQL优化

作者: java3344520   发布时间: 2011-12-23

以前没怎么用过存储过程,但是我查到资料上面说“SQL存储过程执行起来比SQL命令文本快得多。当一个SQL语句包含在存储过程中时,服务器不必每次执行它时都要分析和编译它。”

作者: lp244392323   发布时间: 2011-12-23

相对会快一点,也没有快很多啊,SQL写得标准的话,也不会出现硬解析啊,真是的,具体情况具体分析。
看看你的执行计划就知道了

作者: java3344520   发布时间: 2011-12-23

存储过程只是减少交互,一次编译永久使用,提升是有的,不过不是肉眼看的出来的
具体优化还是要看执行计划,要么找你们DBA帮你看看语句

作者: geniuswjt   发布时间: 2011-12-23

热门下载

更多