2016-07-25 5 views
0

ストアドプロシージャ内でテーブルを自己結合する際に、最大時間がかかります。自己結合テーブルからmax stop_dateを取得する方法

他の効率的な方法を提案してください。

クエリ

Select a.*,b.* from acct a join acct b 
On a.acctno=b.acctno 
Join finance c 
On a.acctno=c.acctno 
Where a.insertdate between start_date and end_date 
And b.stop_date=(select max(stop_date) from acct where acctno=b.acctno and addr_code=b.addr_code and unique_id<>a.unique_id and stop_date<=end_date) 
+0

DBにインデックスを設定しましたか? – Grommy

+0

stop_dateにインデックスがありません。私はthayカラムにインデックスを追加しないでください。私はSQL管理スタジオ2012 –

+0

を使用していますなぜあなたはそれ自体にACTを接合する必要がありますか?これは1対1のものか多対多のものですか?それが1対1の場合は、同じレコードを探しているだけです。多くの場合、ACTレコードのクロス結合マトリックスを作成しています – Cato

答えて

0

これを使用して、それに役立つことを願っています。

Select a.*,b.* from acct a,acct b,finance c 
where a.acctno=b.acctno 
and a.acctno=c.acctno 
and a.insertdate 
between a.start_date and a.end_date 
And b.stop_date=(select max(stop_date) from acct a,acct b 
where a.acctno=b.acctno 
and a.addr_code=b.addr_code 
and a.unique_id<>b.unique_id 
and a.stop_date<=a.end_date) 
関連する問題