在下面的示例之外,我实际上运行了一个在9秒内拉出39000行的查询。我想更改查询以将与BusinessRuleCriteriaID相关的行合并为一行(在提供的示例中,所有9行应该是一行)。我可以牺牲相当多的时间来实现这一目标。在最大我想要查询需要2分钟。 我最快的时间是在ItemTagDescriptions和SegSubTags列表中进行连接30分钟。我尝试的任何事情都已接近2分钟。
使用的SQL版本是SQL Server 2008。
SELECT [BusinessRuleCriteriaID], [ItemType], [tag], [TagValue], Operator,
[ESSubTag], [ES01], [ES02], [ES03], [ES04], [ES05], [ES06], [ES07], [ES08], [ES09], [ES10], [ES11], [ES12],
[ITMSubTag], [ITM01],
[RR4SubTag], [RR401], [RR402]
FROM
(
SELECT
DISTINCT crit.BusinessRuleCriteriaID,
crit.ItemType,
crit.tag,
crit.TagValue,
crit.Operator,
case when seg.SegmentType = 'ES' then SegSubTags.list end AS [ESSubTag],
case when seg.SegmentType = 'ITM' then SegSubTags.list end AS [ItemSubTag],
case when seg.SegmentType = 'RR4' then SegSubTags.list end AS [RR4SubTag],
c.CellName as [SegmentId],
case when c.CellValue is not null and len(c.CellValue) > 0 Then c.CellValue
Else ItemTagDescriptions.list
End as [Tags]
FROM
BMT_BusinessRuleCriteria crit
LEFT OUTER JOIN
BMT_MappingRuleServiceType it
ON it.BusinessRuleCriteriaID = crit.BusinessRuleCriteriaID
LEFT OUTER JOIN
BMT_BusinessRuleSegment seg
ON seg.BusinessRuleCriteriaID = crit.BusinessRuleCriteriaID
LEFT OUTER JOIN
BMT_BusinessRuleCell c
ON c.BusinessRuleSegmentID = seg.BusinessRuleSegmentID
LEFT OUTER JOIN
BMT_BusinessRuleCellOption MT
ON c.BusinessRuleCellId = MT.BusinessRuleCellId
OUTER APPLY
(
SELECT
IsNull(MT2.Tag, '{Unknown}') + ISNULL('=' + MT.TagValue, '') + ':' + IsNull(MT.CellValue, '') + ';' AS [text()]
FROM
BMT_BusinessRuleCellOption MT
WHERE
MT.BusinessRuleCellId = MT.BusinessRuleCellId
ORDER BY
MT.BusinessRuleCellId
FOR XML PATH('')) ItemTagDescriptions (list)
OUTER APPLY
(
SELECT IsNull(SegSubTag.Tag, '{Unknown}') + ' ' + ISNULL(SegSubTag.Operator, '') + ' ' + IsNull(SegSubTag.TagValue, '') + ';' as [text()]
FROM
BMT_BusinessRuleSegmentTag SegSubTag
WHERE
SegSubTag.BusinessRuleSegmentID = seg.BusinessRuleSegmentID
ORDER BY
SegSubTag.BusinessRuleSegmentID
FOR XML PATH('')
) SegSubTags (list)
WHERE
crit.BusinessRuleCriteriaID = 489302
) AS ESTable
PIVOT
(
max([Tags])
FOR [SegmentId] IN ([ES01],[ES02],[ES03],[ES04],[ES05], [ES06], [ES07], [ES08], [ES09], [ES10], [ES11], [ES12], [ITM01], [RR401], [RR402])
) AS ESPivotTable
<table border='1'><body>
<tr><td>BusinessRuleCriteriaID</td><td>ItemType</td><td>tag</td><td>TagValue</td><td>Operator</td><td>ESSubTag</td><td>ES01</td><td>ES02</td><td>ES03</td><td>ES04</td><td>ES05</td><td>ES06</td><td>ES07</td><td>ES08</td><td>ES09</td><td>ES10</td><td>ES11</td><td>ES12</td><td>ItemSubTag</td><td>ITM01</td><td>RR4SubTag</td><td>RR401</td><td>RR402</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>{INFOSTOR}</td><td>NULL</td><td>88</td><td>45</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARARND present ;</td><td>PARARND:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARACONT present ;</td><td>PARACONT:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARAJUK present ;</td><td>PARAJUK:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARAFUL present ;</td><td>PARAFUL:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARANEK present ;</td><td>PARANEK:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARANOID present ;</td><td>PARANOID:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>PARATRAN present ;</td><td>PARATRAN:{INFOSTOR};</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
<tr><td>489302</td><td>OP</td><td>DHOTIAP</td><td>NULL</td><td>present</td><td>TIKAPE not present ;TIKELP not present ;TIKRAP not present ;</td><td>GH</td><td>TEK</td><td>12</td><td>NULL</td><td>NULL</td><td>SSLYWA:3;REGFA:43;</td><td>SOLFAR:{Data};TENDE:{Data};</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>W</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
</body></table>
<BR><BR>
<table border='1'>
<body>
<tr>
<td>BusinessRuleCriteriaID</td>
<td>ItemType</td>
<td>tag</td>
<td>TagValue</td>
<td>Operator</td>
<td>ESSubTag</td>
<td>ES01</td>
<td>ES02</td>
<td>ES03</td>
<td>ES04</td>
<td>ES05</td>
<td>ES06</td>
<td>ES07</td>
<td>ES08</td>
<td>ES09</td>
<td>ES10</td>
<td>ES11</td>
<td>ES12</td>
<td>ItemSubTag</td>
<td>ITM01</td>
<td>RR4SubTag</td>
<td>RR401</td>
<td>RR402</td>
</tr>
<tr>
<td>489302</td>
<td>OP</td>
<td>DHOTIAP</td>
<td>NULL</td>
<td>present</td>
<td>TIKAPE not present ;TIKELP not present ;TIKRAP not present ;</td>
<td>GH</td>
<td>TEK</td>
<td>12</td>
<td>NULL</td>
<td>NULL</td>
<td>SSLYWA:3;REGFA:43;</td>
<td>SOLFAR:{Data};TENDE:{Data};</td>
<td>NULL</td>
<td>NULL</td>
<td>NULL</td>
<td>NULL</td>
<td>W</td>
<td>PARARND present ;PARACONT present ;PARAJUK present ;PARAFUL present ;PARANEK present ;PARANOID present ;PARATRAN present ;</td>
<td>{INFOSTOR};PARARND:{INFOSTOR};PARACONT:{INFOSTOR};PARAJUK:{INFOSTOR};PARAFUL:{INFOSTOR};PARANEK:{INFOSTOR};PARANOID:{INFOSTOR};PARATRAN:{INFOSTOR};</td>
<td>NULL</td>
<td>88</td>
<td>45</td>
</tr>
</body>
</table>
<BR><BR>
<table border='1'><body>
<tr><td id='1'>BusinessRuleCriteriaID</td><td id='2'>ItemType</td><td id='3'>tag</td><td id='4'>TagValue</td><td id='5'>Operator</td><td id='6'>ESSubTag</td><td id='19'>ItemSubTag</td><td id='21'>RR4SubTag</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>NULL</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARARND present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARACONT present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARAJUK present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARAFUL present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARANEK present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARANOID present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>NULL</td><td id='19'>PARATRAN present ;</td><td id='21'>NULL</td></tr>
<tr><td id='1'>489302</td><td id='2'>OP</td><td id='3'>DHOTIAP</td><td id='4'>NULL</td><td id='5'>present</td><td id='6'>TIKAPE not present ;TIKELP not present ;TIKRAP not present ;</td><td id='19'>NULL</td><td id='21'>NULL</td></tr>
</body></table>
&#13;
答案 0 :(得分:0)
我认为我建议的第一件事就是在没有&#34; DISTINCT&#34;的情况下重新编写您的查询。在里面。请改用GROUP BY子句,因为根据我的经验,这往往会表现得更好。您也可以在没有PIVOT语句的情况下从查询中发布样本数据集。它将帮助我识别您在BusinessRuleCriteriaID中遇到的分组问题。