Sql动态查询拼接字符串的优化
2012-03-15 09:27
537 查看
Sql动态查询拼接字符串的优化
最原始的 直接写:string sql="select * from TestTables where 1=1";
... 这样的代码效率很低的,这样影响了数据库的索引引用
如下所示:
private void TestOneMethod()
{
string querySql = "select * from TestTables where 1=1";
if (hasOneCondition)
{
querySql += "oneCondition='oneCondition'";
}
if (hasTwoCondition)
{
querySql += "twoCondition='twoCondition'";
}
if (hasThreeCondition)
{
querySql += "threeCondition='threeCondition'";
}
// ....其他条件
// ExcSql(querySql)
}
优化方案A:
去掉 string sql="where 1=1"
那么,在用的时候定义一个变量,用来标志是否应存在了hasWhere=false ,如果已经存在了则 hasWhere=!hasWhere.这样下一次在用的时候就
可以用下面的形式表示了:
private void TestOneMethod()
{
string querySql = "select * from TestTables";
bool _hasWhere = false;
if (hasOneCondition)
{
querySql += _hasWhere ? "where" : "and" + "oneCondition='oneCondition'";
_hasWhere = true;
}
if (hasTwoCondition)
{
querySql += _hasWhere ? "where" : "and" + "twoCondition='twoCondition'";
_hasWhere = true;
}
if (hasThreeCondition)
{
querySql += _hasWhere ? "where" : "and" + "threeCondition='threeCondition'";
_hasWhere = true;
}
// ....其他条件
// ExcSql(querySql)
}
....
经过优化后,sql的效率提高了,但是仍然存在问题,问题在哪里呢?如果我们每次都这样写的话是不是非常的费力,那么就提出一个通用的方
法,这个通用的方法首先得有一个bool类型的返回值,用来确认当前是否需要hasString。如下,在应用的时候:
private bool SeachHelper(string whereString, bool hasWhere)
{
if (!hasWhere)
whereString = "where" + whereString;
else
whereString = "and" + whereString;
return true;
}
private void TestTwoMethod()
{
string querySql = "select * from TestTables";
bool _hasWhere = false;
if (hasOneCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql += "oneCondition='oneCondition'";
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql += "twoCondition='twoCondition'";
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql += "threeCondition='threeCondition'";
}
// ....其他条件
// ExcSql(querySql);
}
代码简洁了不少,但是仍然粗只能问题,那么问题又是什么呢?
额,字符串来回的拼接非常的浪费资源,那么 ,用StringBuilder啊,好接下来继续。
private void TestThreeMethod()
{
StringBuilder querySql = new StringBuilder();
querySql.Append("select * from TestTables");
bool _hasWhere = false;
if (hasOneCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql.Append("oneCondition='oneCondition'");
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql.Append("twoCondition='twoCondition'");
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql.Append("threeCondition='threeCondition'");
}
// ....其他条件
// ExcSql(querySql.ToString());
最原始的 直接写:string sql="select * from TestTables where 1=1";
... 这样的代码效率很低的,这样影响了数据库的索引引用
如下所示:
private void TestOneMethod()
{
string querySql = "select * from TestTables where 1=1";
if (hasOneCondition)
{
querySql += "oneCondition='oneCondition'";
}
if (hasTwoCondition)
{
querySql += "twoCondition='twoCondition'";
}
if (hasThreeCondition)
{
querySql += "threeCondition='threeCondition'";
}
// ....其他条件
// ExcSql(querySql)
}
优化方案A:
去掉 string sql="where 1=1"
那么,在用的时候定义一个变量,用来标志是否应存在了hasWhere=false ,如果已经存在了则 hasWhere=!hasWhere.这样下一次在用的时候就
可以用下面的形式表示了:
private void TestOneMethod()
{
string querySql = "select * from TestTables";
bool _hasWhere = false;
if (hasOneCondition)
{
querySql += _hasWhere ? "where" : "and" + "oneCondition='oneCondition'";
_hasWhere = true;
}
if (hasTwoCondition)
{
querySql += _hasWhere ? "where" : "and" + "twoCondition='twoCondition'";
_hasWhere = true;
}
if (hasThreeCondition)
{
querySql += _hasWhere ? "where" : "and" + "threeCondition='threeCondition'";
_hasWhere = true;
}
// ....其他条件
// ExcSql(querySql)
}
....
经过优化后,sql的效率提高了,但是仍然存在问题,问题在哪里呢?如果我们每次都这样写的话是不是非常的费力,那么就提出一个通用的方
法,这个通用的方法首先得有一个bool类型的返回值,用来确认当前是否需要hasString。如下,在应用的时候:
private bool SeachHelper(string whereString, bool hasWhere)
{
if (!hasWhere)
whereString = "where" + whereString;
else
whereString = "and" + whereString;
return true;
}
private void TestTwoMethod()
{
string querySql = "select * from TestTables";
bool _hasWhere = false;
if (hasOneCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql += "oneCondition='oneCondition'";
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql += "twoCondition='twoCondition'";
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql += "threeCondition='threeCondition'";
}
// ....其他条件
// ExcSql(querySql);
}
代码简洁了不少,但是仍然粗只能问题,那么问题又是什么呢?
额,字符串来回的拼接非常的浪费资源,那么 ,用StringBuilder啊,好接下来继续。
private void TestThreeMethod()
{
StringBuilder querySql = new StringBuilder();
querySql.Append("select * from TestTables");
bool _hasWhere = false;
if (hasOneCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql.Append("oneCondition='oneCondition'");
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql.Append("twoCondition='twoCondition'");
}
if (hasThreeCondition)
{
_hasWhere = SeachHelper(querySql, _hasWhere);
querySql.Append("threeCondition='threeCondition'");
}
// ....其他条件
// ExcSql(querySql.ToString());
相关文章推荐
- Sql动态查询拼接字符串的优化
- Sql动态查询拼接字符串的优化
- mysql 字符串分割 和 动态执行拼接sql
- SQL字符串拼接动态SQL中的 引号问题
- 用jdbc做查询操作时动态拼接参数报SQL语法错误的问题
- 动态sql语句拼接查询条件
- 用SQL将查询出来的多列的值拼接成一个字符串
- C#拼接长字符串,根据是否查询到值来动态赋值。
- 前台传入多个参数(数组格式),拼接成字符串中间用逗号隔开,传入到sql中用in查询.
- SQL将查询出来的多列的值拼接成一个字符串
- 在ibatis框架中 拼接sql语句,动态查询 .
- 拼接字符串sql语句查询[WEB学习实例]
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- MySQL中limit使用动态参数的解决方法(拼接SQL字符串语句来执行SQL)
- sql查询字符串拼接
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- SQL优化一例,由于动态性能视图统计信息不准确引起的查询缓慢
- 在sql脚本中将查询结果集拼接成字符串
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 根据条件查询动态拼接sql语句