zoukankan      html  css  js  c++  java
  • Sql动态查询拼接字符串的优化

    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());
            }

    等一下,那个公用的方法也得改,当然要改!
     
            private bool SeachHelper(StringBuilder whereString, bool hasWhere)
            {
                if (!hasWhere)
                    whereString.Append("where");
                else
                    whereString.Append("and");
                return true;
            }

    上面就是执行动态查询时的逐步优化,说道优化,其实是有一定的前提的,除了第一种情况最低能,其余的要根据实际的情况来进行确定。
    如果查询条件非常少,甚至只有两种的情况的时候,则无所谓了,但是如果查询条件越多,那么后面的方法的效率越高。同时对于代码的可读

    性越强,想一下,一个项目通常有多少人在写sql查询,如果没给人的风格不同,那么以后的维护人员就要遭罪了,通过使用通用的方法,那么

    以后给代码的维护人员带来方便,同时拼接字符串的带来的额外开销也是不容忽视的,好的代码习惯很重要,一个系统从小的地方去注意,那

    么最后一定是一个优秀的系统。

  • 相关阅读:
    Spring Cloud Stream 使用延迟消息实现定时任务(RabbitMQ)
    rocketmq发送消息的三种方式
    windows下RocketMQ安装部署
    idea多设备自动同步配置
    idea复制springboot的maven项目后,修改了maven名称,但maven工具里的maven名称没改变,不生效
    SpringBoot图文教程16—SpringBoot 多模块开发「web」「打包」
    spring-boot-starter-parent的作用
    JDK8 从永久代到元空间
    Spring 生命周期
    mesos Failed to connect to
  • 原文地址:https://www.cnblogs.com/mannixiang/p/6819814.html
Copyright © 2011-2022 走看看