减少动态SQL语句的预编译时间
尽量使用绑定变量的方式执行动态的SQL语句,减少SQL语句预编译次数,例如在拼凑SQL语句时,将语句书写为下面的方式:
- "select col1, col2 from tbname where col1=? and col3=?"
- "insert into tbname values(?,?,?)"
- "update tbname set col1=? where col2=?"
- "delete from tbname where col3=?"
如果使用CLI编程,请使用SQLBindParameter()函数绑定变量;如果使用SQL/C,请使用execute s1 using <变量1>;, <变量2>;。
这种编程方式对性能十分重要,例如我们在存储过程stored procedure中往一个数据表中插入数据,如果使用绑定变量的insert语句,可以在一秒钟插入5000条记录;如果使用非绑定变量的方式,只能在一秒钟插入不到150条记录。
|