我应该如何索引我的表格

时间:2014-06-19 12:59:11

标签: sql sql-server performance sql-server-2008 indexing

我有这个问题:

select distinct 
    be.sk_loan_id, 
be.deal_id, 
val.amount as rest_balance, 
lo.value_date as effective_start_date, 
lo.maturity_date as effective_maturity_date, 
lo.currency_id as currency, 
'0' as process_status, 
sub.char_cust_element1 as installment_date, 
sub.char_cust_element2 as installment_type, 
sub.char_cust_element5 as rest_balance_freq, 
lo.repayment_principal_freq, 
lo.next_principal_sch_date, 
lo.next_interest_sch_date, 
lo.repayment_interest_freq,
lo.interest_basis, 
lo.interest_rate, 
lo.base_rate, 
s.amount as principal_amount, 
isnull(prevVal.prev_amount_change, 0) as prev_amount_change 
from 
    t_trn_loan_business_event be 
inner join 
    t_trn_loan lo on be.sk_loan_id = lo.sk_id 
inner join 
    t_trn_loan_valuation val on lo.sk_id = val.sk_loan_id 
inner join 
    t_deal_subtype sub on lo.deal_subtype = sub.deal_subtype 
inner join 
    t_trn_loan_schedule s on be.deal_id = s.deal_id 
                             and s.amount_type <> 'INT' 
                             and '2012-1-1' between s.start_validity_date and s.schedule_end_date 
left join 
    (select 
        sk_loan_id, 
        sum(case when v.sk_loan_valuation_type = 24  then amount else -amount end) as prev_amount_change 
     from 
        t_trn_loan_valuation v 
     where 
        v.sk_loan_valuation_type in (24, 27) 
        and v.sk_time_id <  4384
     group by 
        sk_loan_id) prevVal on be.sk_loan_id = prevVal.sk_loan_id 
where 
   be.sk_business_event_type_id in (17, 18, 7, 3, 8, 14, 19, 20) 
   and val.sk_loan_valuation_type = 25
   and be.sk_time_id =  4384
   and process_flag1 = '0'

我想提高此查询的性能。我应该在索引中添加哪些列才能获得最佳性能?但是,我还在t_trn_loan_business_eventt_trn_loan valuation中插入了大量数据。

例如,在表t_trn_loan_valuation中,现在我有3个索引:

  • sk_loan_id(群集)
  • sk_loan_valuation_type
  • sk_time id

我应该对列sk_loan_id使用非聚集索引吗?或者我应该将它们放在1个索引(sk_loan_id, sk_valuation_type, sk_time_id)中?

如果是,我应该将其设为群集还是非群集?

谢谢,

0 个答案:

没有答案