2016-03-22 5 views
0

私はテンポラリテーブルで選択クエリを最適化しようとしました(私はSQL Serverにもっと慣れています。1つのスクリプトでテンポラリテーブルを使用して複数の命令を実行

クエリは、基本的にはこれを少し単純化したものです。テーブルを作成し、塗りつぶして使用し、ドロップテーブルを使用します。様々な技術的な理由

create global temporary table trucks (
    nb_trucks int, 
    warehouse int 
) 
on commit preserve rows 
; 
insert into trucks (
    nb_trucks, warehouse 
) 
select 
    sum(pla.NB_TRUCKS) 
    ,pla.id_site 
from ASI_MAS_PLA_PLANIFICATION pla 
group by 
    pla.id_site 
; 
select distinct 
op.operation_id as OPERATION_CODE, 
pla.id_site as WAREHOUSE_ID, 
(
    select 
    coalesce(sum(nb_trucks), 0) 
    from trucks 
    where 
     and trucks.warehouse = pla.id_site 
) as NB_TRUCKS, 
from ASI_MAS_PLA_PLANIFICATION pla 
inner join ASI_MAS_PLA_CL_PLANIF_OP op 
    on op.planif_operation_id = pla.z_planif_operation_id 
where 
    pla.z_remainder = 0 
group by 
    op.operation_id 
    ,pla.id_site 
order by 
    op.operation_id 
; 
truncate table trucks; 
drop table trucks; 

は、私は(実際に私が唯一の「選択」の部分を実行する必要がありますが、一時テーブルを使用していないが屋根を突き破って実行時間を送信します)単一のスクリプトでこれらのアクションを実行する必要があります。

私はスクリプトをbegin ... end;ブロック内に配置しましたが、最初の命令としてテーブルを作成するのは好きではありません。

As I saw here私は現在、それぞれの文を「即時実行」like thisでラップしようとしていますが、私はこの解決策をあまり好んでいません。

「作成」を含む複数のステートメントを1つのSQLスクリプト内で実行するにはどうすればよいですか?

答えて

0

説明計画がないとわかりにくいです。たぶん「と」節のヘルプ:

WITH trucks AS 
(SELECT /*+ materialize */ 
    SUM(pla.nb_trucks) nb_trucks, 
    pla.id_site warehouse 
    FROM asi_mas_pla_planification pla 
    GROUP BY pla.id_site) 
SELECT DISTINCT op.operation_id AS operation_code, 
       pla.id_site AS warehouse_id, 
       (SELECT coalesce(SUM(nb_trucks), 0) 
        FROM trucks 
        WHERE trucks.warehouse = pla.id_site) AS nb_trucks 
    FROM asi_mas_pla_planification pla 
INNER JOIN asi_mas_pla_cl_planif_op op 
    ON op.planif_operation_id = pla.z_planif_operation_id 
WHERE pla.z_remainder = 0 
GROUP BY op.operation_id, 
      pla.id_site 
ORDER BY op.operation_id; 

が、私は、クエリのロジックを持ついくつかの問題があると思います。私はあなたがトラックを正確に数えていないと思う。しかし、これが正しいかどうかは分かります。

多分このクエリは優れていますか?

SELECT op.operation_id AS operation_code, 
     pla.id_site AS warehouse_id, 
     nvl(SUM(pla.nb_trucks), 0) nb_trucks 
    FROM (SELECT /*+ materialize*/ id_site, 
       z_planif_operation_id, 
       SUM(nb_trucks) nb_trucks 
      FROM asi_mas_pla_planification 
     WHERE z_remainder = 0 
     GROUP BY id_site, 
        z_planif_operation_id) pla 
INNER JOIN asi_mas_pla_cl_planif_op op 
    ON op.planif_operation_id = pla.z_planif_operation_id 
GROUP BY op.operation_id, 
      pla.id_site 
ORDER BY op.operation_id; 

よろしく

+0

私は、共通テーブル式を試してみましたが、実行時間が非常に長いです。 – thomasb

+0

私は私の答えを編集しました...助けてくれることを願っています。 – StefanG

関連する問題