请帮我调整一下这个sql server查询。此处vcompanyquicksearch
是company
表格上的视图。
我应该创建哪些索引?执行计划中正在进行排序操作 费用为24%,即
[CCEP_DEV].[dbo].[Company].vchContactFirstName Descending,
[CCEP_DEV].[dbo].[Company].iCompanyId Ascending
我不知道为什么第二次排序正在进行......任何人都可以建议为什么?
select top *
from vCompanyquicksearch
LEFT OUTER JOIN ReferenceParameters r1
on vCompanyquicksearch.iCompanyTypeCode = r1.iParameterId AND
r1.iSiteId =1
LEFT OUTER JOIN ReferenceParameters r2
on vCompanyquicksearch.iCompanySubTypeCode = r2.iParameterId AND
r2.iSiteId =1
LEFT OUTER JOIN ReferenceParameters r3
on vCompanyquicksearch.iPhoneTypeId = r3.iParameterId AND
r3.iSiteId =1
LEFT OUTER JOIN Country c ON
vCompanyquicksearch.chCountryCode = c.chCountryCode AND
ISNULL(vCompanyquicksearch.chCountryCode,'''') <> '''' and
c.iSiteId = 1
LEFT OUTER JOIN Region r ON
vCompanyquicksearch.chCountryCode = r.chCountryCode AND
ISNULL(vCompanyquicksearch.chCountryCode,'''') <> '''' AND
vCompanyquicksearch.chRegionCode = r.chRegionCode AND
ISNULL(vCompanyquicksearch.chRegionCode,'''') <> '''' and
r.iSiteId = 1
where vCompanyquicksearch.isiteid = 1 and vCompanyquicksearch.tirecordstatus = 1 and vCompanyquicksearch.iCompanyId in (
select distinct * from (
select customer_id from csu_customer_policy
inner join csu_policy on csu_policy.policy_number = csu_customer_policy.policy_number
and csu_policy.data_source = csu_customer_policy.data_source
where csu_policy.vehicle_no in(
'sgv3976r')
and csu_customer_policy.delete_status = 0 and csu_customer_policy.site_id = ' 1 '
) yTable
) Order by vchName desc
答案 0 :(得分:0)
试试这个 -
;WITH cte AS
(
SELECT *
FROM ReferenceParameters r
WHERE r.iSiteId = 1
)
SELECT /*TOP*/ *
FROM (
SELECT *
FROM dbo.vCompanyquicksearch c1 --<-- I think the main problem in this view
WHERE c1.isiteid = 1
AND c1.tirecordstatus = 1
) c1
LEFT JOIN cte r1 ON c1.iCompanyTypeCode = r1.iParameterId
LEFT JOIN cte r2 ON c1.iCompanySubTypeCode = r2.iParameterId
LEFT JOIN cte r3 ON c1.iPhoneTypeId = r3.iParameterId
LEFT JOIN (
SELECT *
FROM dbo.Country c
WHERE c.iSiteId = 1
) c ON c1.chCountryCode = c.chCountryCode
AND ISNULL(c1.chCountryCode, '') != ''
LEFT JOIN (
SELECT *
FROM dbo.Region r
WHERE r.iSiteId = 1
) r ON c1.chCountryCode = r.chCountryCode
AND c1.chRegionCode = r.chRegionCode
AND ISNULL(c1.chCountryCode, '') != ''
AND ISNULL(c1.chRegionCode,'') != ''
WHERE c1.iCompanyId IN
(
SELECT DISTINCT customer_id
FROM dbo.csu_customer_policy p
JOIN dbo.csu_policy pc ON pc.policy_number = p.policy_number
AND pc.data_source = p.data_source
WHERE pc.vehicle_no = 'sgv3976r'
AND p.delete_status = 0
AND p.site_id = ' 1 '
)
ORDER BY vchName DESC
<强>更新强>
...
SELECT *
FROM (
SELECT TOP ... *
FROM dbo.vCompanyquicksearch c1
WHERE c1.isiteid = 1
AND c1.tirecordstatus = 1
ORDER BY vchName DESC
) c1
...
WHERE c1.iCompanyId IN
(
SELECT DISTINCT customer_id
FROM dbo.csu_customer_policy p
JOIN dbo.csu_policy pc ON pc.policy_number = p.policy_number
AND pc.data_source = p.data_source
WHERE pc.vehicle_no = 'sgv3976r'
AND p.delete_status = 0
AND p.site_id = ' 1 '
)