0

Oracle テーブルのサンプルを取得したいのですが、別のテーブルのエントリは含めません。現在機能しているクエリがありますが、サブセレクトが 1000 を超えるレコードを取得すると、クエリが爆発することは間違いありません。

select user_key from users sample(5)
where active_flag = 'Y'
and user_key not in (
    select user_key from user_validation where validation_state <> 'expired'
);

.なしでこれをどのように書き直すことができますかnot in。を使用することを考えminusましたが、新しいエントリが user_validation テーブルに追加されると、サンプル サイズが減少し続けます。

4

3 に答える 3

3

でこれを行うことができますleft outer join

select *
from (select u.user_key,
             count(*) over () as numrecs
      from users u left outer join
           user_validation uv
           on u.user_key = uv.user_key and
              uv.validation_state <> 'expired'
      where u.active_flag = 'Y' and uv.user_key is null
     ) t
where rownum <= numrecs * 0.05

サンプル句を使用しています。選択した 5% の不一致だけが必要なのか、それとも不一致のデータの 5% が必要なのかは明確ではありません。これは後者です。

編集:著者のコメントに基づいて例を追加:

select user_key from (
  select u.user_key, row_number() over (order by dbms_random.value) as randval
  from users u 
    left outer join user_validation uv 
    on u.user_key = uv.user_key 
    and uv.validation_state <> 'expired'
  where u.active_flag = 'Y' 
  and uv.user_key is null
) myrandomjoin where randval <=100;
于 2013-04-03T17:38:50.540 に答える
2
select us.user_key
from users us -- sample(5)
where us.active_flag = 'Y'
and NOT EXISTS (
    SELECT *
    from user_validation nx
    where nx.user_key = us.user_key
    AND nx.validation_state <> 'expired'
    );

ところで: sample(5) の意味がわからないのでコメントアウトしました。(関係ないと強く思いますが)

于 2013-04-03T17:03:28.993 に答える