51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1724|回复: 3
打印 上一主题 下一主题

[原创] 大家给点优化意见

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-1-24 17:50:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面是页面一个点击查询之后,后台执行的语句,我觉得单从sql优化已经不现实了,大家给我点建议
下面只是截取的一部分,共有45个sql:
        public List getData(Date beginDate, Date endDate, String brch_id) {

                // -------------------买入业务start---------------------------
                // 直贴
                sql[0] ="select '直贴',sum(table.num),sum(coalesce(table.all_money,0)) from "
                        +"((select count(*) as num,sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=1 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ") union all select count(*) as num,sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=1 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all "
                        +"(select count(*) as num,-sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=1 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all select count(*) as num,-sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill  "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=1 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")))) table";
               
                // 票据包买
                sql[1] ="select '票据包买',sum(table.num),sum(coalesce(table.all_money,0)) from "
                        +"((select count(*) as num,sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=2 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ") union all select count(*) as num,sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=2 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all "
                        +"(select count(*) as num,-sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=2 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all select count(*) as num,-sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill  "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=2 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")))) table";
                // 商票保贴
                sql[2] ="select '商票保贴',sum(table.num),sum(coalesce(table.all_money,0)) from "
                        +"((select count(*) as num,sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=3 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ") union all select count(*) as num,sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=3 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all "
                        +"(select count(*) as num,-sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=3 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all select count(*) as num,-sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill  "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=3 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")))) table";
                // 代理贴现
                sql[3] ="select '代理贴现',sum(table.num),sum(coalesce(table.all_money,0)) from "
                        +"((select count(*) as num,sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=4 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ") union all select count(*) as num,sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=4 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (1,2) and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all "
                        +"(select count(*) as num,-sum(bnfo_bill_money) all_money "
                        + "from buybatch ,re_buba_bnfo "
                        + "where buybatch.buba_id=re_buba_bnfo.buba_id and buybatch.bupr_id=4 and rebb_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")) union all select count(*) as num,-sum(bk_bill_money) all_money "
                        + "from Back_deleted_bill  "
                        + "where upper(table_name)='RE_BUBA_BNFO' and bk_prod_type=4 and re_bill_id "
                        + "in (select re_bill_id "
                        + "from busiflow where busitype='BUY' and bufi_status in (2) and bufl_no in(select pre_bufl_no from busiflow where bufi_code='CZ' and bufi_status=4 and bufl_dt='"
                        + beginDate + "' and brch_id='" + brch_id + "'"
                        + ")))) table";
//                 转贴现买断
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

  • TA的每日心情
    奋斗
    2022-5-8 19:23
  • 签到天数: 137 天

    连续签到: 1 天

    [LV.7]测试师长

    2#
    发表于 2011-1-24 19:55:46 | 只看该作者
    那就从业务上来优化
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
    发表于 2011-1-24 23:08:38 | 只看该作者
    什么数据库?一般需要从索引、表结构、SQL语句几个方面入手优化
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2011-1-25 12:36:05 | 只看该作者
    看到查询嵌套那么问题就来了,可以考虑中间过程用视图来解决或者你要考虑索引和设计了
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-10-8 06:20 , Processed in 0.080574 second(s), 33 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表