2017-06-15 4 views
4

サブクエリに相当する内部結合を作成しました。誰かが私にそれが正しいやり方であり、より効率的な方法であるかどうかを知らせてもらえますか? ONキーワードが内側句内部結合とサブクエリの照合が必要

サブクエリ

select 
     companyId, 
     fiscalYear, 
     fiscalQuarter, 
     periodenddate 
    into #PeriodTbl 
    from( 
     select 
      fp.companyId, 
      fp.fiscalYear, 
      fp.fiscalQuarter, 
      fi.periodenddate, 
      ROW_NUMBER() OVER (PARTITION BY fp.companyId, fp.fiscalYear, fp.fiscalQuarter ORDER BY fi.periodEndDate DESC) rowno 
     from ciqfinperiod fp 
      join ciqfininstance fi on fi.financialperiodid = fp.financialperiodid 
     where fp.periodtypeid = 4 
      and fi.periodenddate > @date 
      and fi.latestforfinancialperiodflag = 1 
      and latestfilingforinstanceflag = 1 
      and fp.companyId in (select id from #companyId) 

     ) a 
    where a.rowno = 1 

は、一般に

select 
     companyId, 
     fiscalYear, 
     fiscalQuarter, 
     periodenddate 
    into #PeriodTbl 
    from( 
     select 
      fp.companyId, 
      fp.fiscalYear, 
      fp.fiscalQuarter, 
      fi.periodenddate, 
      ROW_NUMBER() OVER (PARTITION BY fp.companyId, fp.fiscalYear, fp.fiscalQuarter ORDER BY fi.periodEndDate DESC) rowno 
     from ciqfinperiod fp inner join #companyId ci on fp.companyId = ci.id 
      join ciqfininstance fi on fi.financialperiodid = fp.financialperiodid 
     where fp.periodtypeid = 4 
      and fi.periodenddate > @date 
      and fi.latestforfinancialperiodflag = 1 
      and latestfilingforinstanceflag = 1 
      --and fp.companyId in (select id from #companyId) 

     ) a 
    where a.rowno = 1 

答えて

5

INNER JOINとサブクエリとINに参加する場合と同じようにフィルタリングない正確ではありません同じ。問題は、INNER JOINが重複行を導入する可能性があることです。

idsを選択しているため、あなたのケースではこれが起こりそうにないと思われます。

皮肉なことに、#companyIdに重複があっても、パフォーマンス上の違いはありません。これは、会社ごとに1つの行しか選択していないためです。

したがって、INNER JOINに切り替えると問題はありません。

関連する問題