1

この SQL サーバー クエリの調整を手伝ってください。これがテーブルvcompanyquicksearch上のビューです。company

どのインデックスを作成する必要がありますか? 24% のコストがかかる実行計画でソート操作が行われています。つまり、

 [CCEP_DEV].[dbo].[Company].vchContactFirstName Descending,
 [CCEP_DEV].[dbo].[Company].iCompanyId Ascending

2回目の選別が行われている理由がわかりません...誰かが理由を提案できますか?

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
4

1 に答える 1

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 '
)
于 2013-06-13T13:26:41.070 に答える