如何优化此查询?

时间:2013-01-08 10:06:06

标签: sql sql-server-2005 xquery

我是XQuery的新手。 请建议一种方法来优化以下查询。

我试过下面的查询它花费的时间比预期的要长。

Select 
A.a_name, 
I.p_type, 
SUM(P.c_n_amount) AS c_n_amount,
CASE  WHEN SUM(ABS(M_value)) = 0 THEN 0
ELSE SUM(ABS(Price) * ABS(M_value)) / SUM(ABS(M_value)) END AS 
[price],
SUM(M_value) AS m_value,SUM(N50) [-50],SUM(P50) [50]
from  position P 
INNER JOIN COUNT AS A ON P.a_id = A.a_id 
INNER JOIN I_VIEW AS I ON P.i_id = I.i_id 
inner join (
select R_C.p_id,M_value,Price,N50,P50
from(
select j.p_id,
C.value('(Result[@resultName = "m_value"]/resultValue)[1]', 'float') as M_value,
C.value('(Result[@resultName = "price"]/resultValue)[1]', 'float') as Price
from  jyothi_xml j CROSS APPLY resultkeys.nodes('/Results')  T(C) 
where r_set='POLY' and r_category ='sk')  R_Category,
(select j1.p_id,
C.value('(Result[@resultName = "rate_shock_n50"]/resultValue)[1]', 'float') as N50,
C.value('(Result[@resultName = "rate_shock_p50"]/resultValue)[1]', 'float') as P50
from jyothi_xml j1 CROSS APPLY resultkeys.nodes('/Results')  T(C)
where result_set='POLY' and result_category ='rate_shocks')  R_S_Category,position p
where p.p_id=R_Category.p_id and p.p_id=R_S_Category.p_id) XMlData  on p.p_id=XMlData.p_id
WHERE P.as_of_date = '2012-10-10' 
GROUP BY A.a_name, I.p_type

1 个答案:

答案 0 :(得分:0)

如果要解析大量XML,首先使用所需的XML限制哪些行,然后解析它们是非常有益的。因此,如果可以,请从Position,Count和I_View表中找出所需内容,然后在jyothi_xml表中搜索这些ID。我发现这对我的查询有很大帮助。