Select a,b,c
From
(
select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx
)union on
Select a,b,c
From
(
select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx
)
....重复10次(其实每次的逻辑和判断条件很复杂,这里方便看懂,随便写的)
因为SQL中的select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx 是一个大概2000行的SQL,
而每一次union on 都是从这个2000行的子表中取数据,这样下来,重复10次就是至少20000行SQL了。如果把这个SQL写成一个视图,就再好不过了。但是貌似oracle视图不支持传参吧?我需要web前台传入的起始时间后结束时间。网上貌似有支持传参的代码,但是没有具体使用方法,不知道有人用过不?我想过把这些sql都写成一个存储过程,这个2000行的sql写成一个函数供调用,但是每次只能fetch其中一行数据,然后就不知道怎么办了。
-----------------
总之,求一解决思路。最坏的结果,是将这些代码重复10次,写成一个巨大的存储过程了。

解决方案 »

  1.   

    Select a,b,c
    From
    (
    select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx
    )
    其中from里面的sql,是一个2000行左右的sql拼凑起来的,里面有一个条件,是where starttime>=xxxx and endtime <xxxx.现在需要重复10次左右上面的代码,通过union all(上面写错了,写成了union on)将他们连接起来。如何减少这个重复的2000行sql,尽量减少。本来如果里面没有where的参数的话,就可以写成一个视图,调用十次就可以了,sql可以减少到几十行。
      

  2.   


    把复杂的sql的静态部分写成一个view,把这个startend 和 endtime 可变的部分放到view外面查询,这样行么。
      

  3.   

    使用with as
    试试,也许对减少代码有帮助
      

  4.   

    with 
     v as (select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx)
     Select a,b,c from v
     union 
     Select a,b,c from v
    .....
      

  5.   

    可以把你的2000多行的SQL用with替换掉
      

  6.   

    这样行不行,存储过程调用函数-> 在函数内进行数据查询把结果放在一个临时表中 -> 存储过程中用游标返回临时表中数据
    (多用户下每次都要保证临时表中你只能对自己的数据进行统计,如果单用户就每次先清空表就可以了)
      

  7.   

    with tablea as
    (select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx
    )
    select * from tablea connect by level < 10 10就是9次数
      

  8.   

    with 
    v as (select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx) 
    Select a,b,c from v 
    union 
    Select a,b,c from v 
    ..... 
      

  9.   

    -----------
    不行,starttime和endtime 在这个view里出现了至少10多次,而不是一次。
    ---------------------现在使用with:
    with 
    v as (select a,b,c,d,e from table where starttime>=xxxx and endtime <xxxx) 
    Select a,b,c from v
    -----------------------在这里运行正常, 
    union all
    Select a,b,c from v 
    -------------------------添加了这个就报错了。
    报:无法分配83000字节的共享内存("shared pool"),"With reconciliation As(Sel... ",sql area","qks3tstr:qks3tGenSingleTahleIDL")"我这里不方便更改共享内存大小或者说打补丁(都要重启),因为数据库是远程的,而且每时每刻都有访问。看来只能写成存储过程了?
      

  10.   

    算了,还是通过在JAVA代码里面分割数据算了,sql写成了一个存储过程,刚刚测试分割成功了。这样就没有使用到union all。这么说这个问题也算是解决了,使用with。