【Oracle】sql循环遍历查询结果

轨迹表特点:

一天一张轨迹表,存储武汉市全市渣土车数据,名称以 GA_GPS_HISTORY_开头,大概一天会有几千台车,每天数据量大概几百万条记录至千万条记录,目前存储有5个月的数据。

需求:

想要查询从轨迹表产生以来,有多少车出现过轨迹

利用sql,实现sql语句的产生,因为人工操作的话,表的数量大,手动写sql比较麻烦

因此采用Oracle自带的sql,生成相应的sql语句

  • 实例:
declare  s_sql  clob := '' ;--    此处需要定义一个clob字段,若定义一个varchar2类型或者nvarchar2类型的,则会显示字符串长度超出(varchar2最大长度4000)
begin
for  wf  in ( select  tname  from  tab  where  tname  like  'GA_GPS_HISTORY_%' )--    该sql用于查询当前用户下的表格
   loop
    s_sql:=s_sql|| 'select distinct hp from ' ||wf.tname|| ' union all ' ;
   end  loop ;
    dbms_output.put_line(s_sql);
--    select s_sql from dual;
end ;

运行上面的sql语句,最终生成一个如下的sql语句:

select count ( distinct hp) from ( select distinct hp from GA_GPS_HISTORY_2016_11_02 union all select distinct hp from GA_GPS_HISTORY_2016_11_03 union all select distinct hp from GA_GPS_HISTORY_2016_11_04 union all select distinct hp from GA_GPS_HISTORY_2016_11_05 union all select distinct hp from GA_GPS_HISTORY_2016_11_06 union all select distinct hp from GA_GPS_HISTORY_2016_11_07 union all select distinct hp from GA_GPS_HISTORY_2016_11_08 union all select distinct hp from GA_GPS_HISTORY_2016_11_09 union all select distinct hp from GA_GPS_HISTORY_2016_11_10 union all select distinct hp from GA_GPS_HISTORY_2016_11_11 union all select distinct hp from GA_GPS_HISTORY_2016_11_12 union all select distinct hp from GA_GPS_HISTORY_2016_11_13 union all select distinct hp from GA_GPS_HISTORY_2016_11_14 union all select distinct hp from GA_GPS_HISTORY_2016_11_15 union all select distinct hp from GA_GPS_HISTORY_2016_11_16 union all select distinct hp from GA_GPS_HISTORY_2016_11_17 union all select distinct hp from GA_GPS_HISTORY_2016_11_18 union all select distinct hp from GA_GPS_HISTORY_2016_11_19 union all select distinct hp from GA_GPS_HISTORY_2016_11_20 union all select distinct hp from GA_GPS_HISTORY_2016_11_21 union all select distinct hp from GA_GPS_HISTORY_2016_11_22 union all select distinct hp from GA_GPS_HISTORY_2016_11_23 union all select distinct hp from GA_GPS_HISTORY_2016_11_24 union all select distinct hp from GA_GPS_HISTORY_2016_11_25 union all select distinct hp from GA_GPS_HISTORY_2016_11_26 union all select distinct hp from GA_GPS_HISTORY_2016_11_27 union all select distinct hp from GA_GPS_HISTORY_2016_11_28 union all select distinct hp from GA_GPS_HISTORY_2016_11_29 union all select distinct hp from GA_GPS_HISTORY_2016_11_30 union all select distinct hp from GA_GPS_HISTORY_2016_12_01 union all select distinct hp from GA_GPS_HISTORY_2016_12_02 union all select distinct hp from GA_GPS_HISTORY_2016_12_03 union all select distinct hp from GA_GPS_HISTORY_2016_12_04 union all select distinct hp from GA_GPS_HISTORY_2016_12_05 union all select distinct hp from GA_GPS_HISTORY_2016_12_06 union all select distinct hp from GA_GPS_HISTORY_2016_12_07 union all select distinct hp from GA_GPS_HISTORY_2016_12_08 union all select distinct hp from GA_GPS_HISTORY_2016_12_09 union all select distinct hp from GA_GPS_HISTORY_2016_12_10 union all select distinct hp from GA_GPS_HISTORY_2016_12_11 union all select distinct hp from GA_GPS_HISTORY_2016_12_12 union all select distinct hp from GA_GPS_HISTORY_2016_12_13 union all select distinct hp from GA_GPS_HISTORY_2016_12_14 union all select distinct hp from GA_GPS_HISTORY_2016_12_15 union all select distinct hp from GA_GPS_HISTORY_2016_12_16 union all select distinct hp from GA_GPS_HISTORY_2016_12_17 union all select distinct hp from GA_GPS_HISTORY_2016_12_18 union all select distinct hp from GA_GPS_HISTORY_2016_12_19 union all select distinct hp from GA_GPS_HISTORY_2016_12_20 union all select distinct hp from GA_GPS_HISTORY_2016_12_21 union all select distinct hp from GA_GPS_HISTORY_2016_12_22 union all select distinct hp from GA_GPS_HISTORY_2016_12_23 union all select distinct hp from GA_GPS_HISTORY_2016_12_24 union all select distinct hp from GA_GPS_HISTORY_2016_12_25 union all select distinct hp from GA_GPS_HISTORY_2016_12_26 union all select distinct hp from GA_GPS_HISTORY_2016_12_27 union all select distinct hp from GA_GPS_HISTORY_2016_12_28 union all select distinct hp from GA_GPS_HISTORY_2016_12_29 union all select distinct hp from GA_GPS_HISTORY_2016_12_30 union all select distinct hp from GA_GPS_HISTORY_2016_12_31 union all select distinct hp from GA_GPS_HISTORY_2017_01_01 union all select distinct hp from GA_GPS_HISTORY_2017_01_02 union all select distinct hp from GA_GPS_HISTORY_2017_01_03 union all select distinct hp from GA_GPS_HISTORY_2017_01_04 union all select distinct hp from GA_GPS_HISTORY_2017_01_05 union all select distinct hp from GA_GPS_HISTORY_2017_01_06 union all select distinct hp from GA_GPS_HISTORY_2017_01_07 union all select distinct hp from GA_GPS_HISTORY_2017_01_08 union all select distinct hp from GA_GPS_HISTORY_2017_01_09 union all select distinct hp from GA_GPS_HISTORY_2017_01_10 union all select distinct hp from GA_GPS_HISTORY_2017_01_11 union all select distinct hp from GA_GPS_HISTORY_2017_01_12 union all select distinct hp from GA_GPS_HISTORY_2017_01_13 union all select distinct hp from GA_GPS_HISTORY_2017_01_14 union all select distinct hp from GA_GPS_HISTORY_2017_01_15 union all select distinct hp from GA_GPS_HISTORY_2017_01_16 union all select distinct hp from GA_GPS_HISTORY_2017_01_17 union all select distinct hp from GA_GPS_HISTORY_2017_01_18 union all select distinct hp from GA_GPS_HISTORY_2017_01_19 union all select distinct hp from GA_GPS_HISTORY_2017_01_20 union all select distinct hp from GA_GPS_HISTORY_2017_01_21 union all select distinct hp from GA_GPS_HISTORY_2017_01_22 union all select distinct hp from GA_GPS_HISTORY_2017_01_23 union all select distinct hp from GA_GPS_HISTORY_2017_01_24 union all select distinct hp from GA_GPS_HISTORY_2017_01_25 union all select distinct hp from GA_GPS_HISTORY_2017_01_26 union all select distinct hp from GA_GPS_HISTORY_2017_01_27 union all select distinct hp from GA_GPS_HISTORY_2017_01_28 union all select distinct hp from GA_GPS_HISTORY_2017_01_29 union all select distinct hp from GA_GPS_HISTORY_2017_01_30 union all select distinct hp from GA_GPS_HISTORY_2017_01_31 union all select distinct hp from GA_GPS_HISTORY_2017_02_01 union all select distinct hp from GA_GPS_HISTORY_2017_02_02 union all select distinct hp from GA_GPS_HISTORY_2017_02_03 union all select distinct hp from GA_GPS_HISTORY_2017_02_04 union all select distinct hp from GA_GPS_HISTORY_2017_02_05 union all select distinct hp from GA_GPS_HISTORY_2017_02_06 union all select distinct hp from GA_GPS_HISTORY_2017_02_07 union all select distinct hp from GA_GPS_HISTORY_2017_02_08 union all select distinct hp from GA_GPS_HISTORY_2017_02_09 union all select distinct hp from GA_GPS_HISTORY_2017_02_10 union all select distinct hp from GA_GPS_HISTORY_2017_02_11 union all select distinct hp from GA_GPS_HISTORY_2017_02_12 union all select distinct hp from GA_GPS_HISTORY_2017_02_13 union all select distinct hp from GA_GPS_HISTORY_2017_02_14 union all select distinct hp from GA_GPS_HISTORY_2017_02_15 union all select distinct hp from GA_GPS_HISTORY_2017_02_16 union all select distinct hp from GA_GPS_HISTORY_2017_02_17 union all select distinct hp from GA_GPS_HISTORY_2017_02_18 union all select distinct hp from GA_GPS_HISTORY_2017_02_19 union all select distinct hp from GA_GPS_HISTORY_2017_02_20 union all select distinct hp from GA_GPS_HISTORY_2017_02_21 union all select distinct hp from GA_GPS_HISTORY_2017_02_22 union all select distinct hp from GA_GPS_HISTORY_2017_02_23 union all select distinct hp from GA_GPS_HISTORY_2017_02_24 union all select distinct hp from GA_GPS_HISTORY_2017_02_25 union all select distinct hp from GA_GPS_HISTORY_2017_02_26 union all select distinct hp from GA_GPS_HISTORY_2017_02_27 union all select distinct hp from GA_GPS_HISTORY_2017_02_28 union all select distinct hp from GA_GPS_HISTORY_2017_03_01 union all select distinct hp from GA_GPS_HISTORY_2017_03_02 union all select distinct hp from GA_GPS_HISTORY_2017_03_03 union all select distinct hp from GA_GPS_HISTORY_2017_03_04 union all select distinct hp from GA_GPS_HISTORY_2017_03_05 union all select distinct hp from GA_GPS_HISTORY_2017_03_06) where hp like '鄂A%'

本次试验中,因为每张轨迹表的数据量动辄几百万,甚至千万,因此distinct查询很慢,整个过程持续了15分钟左右
待解决问题:
怎么提高查询效率?



  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Oracle中,可以使用FOR循环遍历数据或执行某些操作。在存储过程中,可以使用FOR循环遍历拼接好的SQL语句,并执行。下面是一个示例代码: ```sql CREATE OR REPLACE PROCEDURE SP_OA_SL AS BEGIN FOR ARR IN (SELECT 'UPDATE SCHOOL_ROll SET ROLL = ' || S.ROLL || ' WHERE ID = ' || S.ID AS SS FROM STUDNETS S) LOOP -- 控制台打印(正式运行时,建议删除或注释) DBMS_OUTPUT.PUT_LINE(ARR.SS); -- 执行SQL EXECUTE IMMEDIATE ARR.SS; END LOOP; END; ``` 在上述代码中,FOR循环通过SELECT语句获取拼接好的SQL语句,并将结果存储在游标ARR中。然后,通过循环遍历游标中的每一条记录,将拼接好的SQL语句打印到控制台,并使用EXECUTE IMMEDIATE语句执行该SQL语句。 请注意,这只是一个示例,实际使用时需要根据具体的需求进行修改。 #### 引用[.reference_title] - *1* *2* [Oracle 存储过程之遍历](https://blog.csdn.net/Wen__Fei/article/details/125636754)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [oracle中的for循环](https://blog.csdn.net/fengchao2016/article/details/59712039)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值