2016-07-19 7 views
0

私には次のような声明があります。問題は、実行に約20分かかります:concatキーを持つ同じテーブルのSQL「Not In」節

select fallid, schreibdatum, fachloe, autor, titel from tbltext 
where schreibdatum >= '01.01.2015' 
and stornierer is null 
and texttyp like 'Entl%' 
and (titel like '%vorl.%' or titel like '%Vorl.%' or titel like '%vorläu%' or titel like '%Vorläu%') 
and concat(fallid, fachloe) not in 
(
select concat(fallid, fachloe) from tbltext 
where schreibdatum >= '01.01.2015' 
and stornierer is null 
and texttyp like 'Entl%' 
and (titel not like '%vorl.%' and titel not like '%Vorl.%' and titel not like '%vorläu%' and titel not like '%Vorläu%') 
and (titel like '%Entlas%' or titel like '%entlas%') 
); 

私はそれを遅くするいくつかの部分があることを知っています。私は多くの好きが悪いことを知っているが、私はそれを避ける方法を見ていない。それに加えて、私は遅い "not-in-clauses"について他の質問を検索しましたが、私は同じテーブルの "concat-key"をjoinまたはexists not節にする方法を知らない。

ご協力いただければ幸いです。

+0

理由だけではなく、条件を以下の場所でエーション'%vorl。%'のようなタイトルや '%vorläu%'のようなタイトルや '%Vorläu%'のようなタイトル) ( '%Entlas%'や '%entlasのようなタイトル% ')を単一のクエリ内に含めることができます –

+0

単一のfallidに対して複数のエントリが存在する可能性があるためです。私は "vorl"のエントリがあるが、 "Entlas"のエントリはないすべてのfallidを知りたい。 – KaneZ

答えて

1

月次データが散在しているかに応じて、同様に狭い間隔でこのクエリ速く

  1. 火災の問い合わせを行い、結果ではなく、1つのだけのクエリで
  2. チェックをやって取得するためのいくつかのアプローチが存在することができあなたはあなたが自分のために行くことができますwhere句
  3. で使用している列に対するインデックスが句

にない使用するのではなく、参加して、連結OPERをスキップします(TITEL - 句

あなたは自己clause..likeではない使用するのではなく、参加してみてくださいすることができますが、 (クエリが期待される結果が得られない場合があります)

select tab1.fallid, tab1.schreibdatum, tab1.fachloe, tab1.autor, tab1.titel from 
(
select concat(fallid, fachloe) concatedTxt1, fallid, schreibdatum, fachloe, autor, titel from tbltext 
where schreibdatum >= '01.01.2015' 
and stornierer is null 
and texttyp like 'Entl%' 
and (titel not like '%vorl.%' and titel not like '%Vorl.%' and titel not like '%vorläu%' and titel not like '%Vorläu%') 
and (titel like '%Entlas%' or titel like '%entlas%') 
) tab1, 
(
select concat(fallid, fachloe) concatedTxt2, fallid, schreibdatum, fachloe, autor, titel from tbltext 
where schreibdatum >= '01.01.2015' 
and stornierer is null 
and texttyp like 'Entl%' 
and (titel like '%vorl.%' or titel like '%Vorl.%' or titel like '%vorläu%' or titel like '%Vorläu%') 
) tab2 
where tab1.fallid =tab2.fallid and concatedTxt1 != concatedTxt2 
+0

これを試しましたが、「列があいまいに定義されました」というエラーが発生しました。なぜドノ? – KaneZ

+0

クエリでtab aliasを追加 - tab1.fallid、tab1.schreibdatum、tab1.fachloe、tab1.autor、tab1.titelを選択 –

0
WITH temp AS 
    (SELECT allid, schreibdatum, fachloe, autor, titel, concat(fallid, fachloe) keyText, 
    CASE 
     WHEN titel LIKE '%vorl.%' OR titel LIKE '%Vorl.%' OR titel LIKE '%vorläu%' OR titel LIKE '%Vorläu%' 
     THEN 'Y' ELSE 'N' 
    END "HAS_VALUE_VORL", 
    CASE 
      WHEN lower(titel) LIKE '%entlas%' THEN 'Y' ELSE 'N' 
    END "HAS_VALUE_ENTLAS" 
    FROM tbltext 
    WHERE schreibdatum >= '01.01.2015' 
      AND stornierer  IS NULL 
      AND texttyp LIKE 'Entl%' 
) 
SELECT * 
FROM temp t1 
WHERE HAS_VALUE_VORL='Y' 
AND NOT EXISTS 
    (SELECT 'X' FROM temp t2 WHERE HAS_VALUE_ENTLAS='Y' AND t1.keyText=t2.keyText 
); 
関連する問題