您的位置:首页 > 数据库

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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql string 优化 数据库