0

我有一个视图,其中五个字段(不是其中的 ID 字段)与 XML PATH 函数一起使用。该视图从 ID 是聚集主键的表中填充值。该视图正在花费大量执行时间并造成混乱。任何人都可以帮助。

这是我正在操作的视图

SELECT t1.ID,
hcp = REPLACE((SELECT t2.hcp AS [data()]--,t2.imstime AS [data()],t2.medtime AS [data()],t2.slntime AS [data()]
                        FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID
                        ORDER BY t2.hcp--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),
       convert(Datetime,REPLACE((SELECT t2.hcptime AS [data()]--,t2.imstime AS [data()],t2.medtime AS [data()],t2.slntime AS [data()]
                        FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID and datalength(t2.hcptime)>0
                        ORDER BY t2.hcptime --,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),127) as hcptime,
      ims = REPLACE((SELECT t2.ims AS [data()]--,t2.imstime AS [data()],t2.medtime AS [data()],t2.slntime AS [data()]
                        FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID
                        ORDER BY t2.ims--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),
        convert(Datetime,REPLACE((SELECT t2.imstime AS [data()]
                         FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID and datalength(t2.imstime)>0
                        ORDER BY t2.imstime--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),127) as imstime,
         med = REPLACE((SELECT t2.med AS [data()]--,t2.imstime AS [data()],t2.medtime AS [data()],t2.slntime AS [data()]
                        FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID
                        ORDER BY t2.med--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),

        convert(Datetime,REPLACE((SELECT t2.medtime AS [data()]
                         FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID and datalength(t2.medtime)>0
                        ORDER BY t2.medtime--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),127) as medtime,

         sln = REPLACE((SELECT t2.sln AS [data()]--,t2.imstime AS [data()],t2.medtime AS [data()],t2.slntime AS [data()]
                        FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID
                        ORDER BY t2.sln--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),

        convert(Datetime,REPLACE((SELECT t2.slntime AS [data()]
                         FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t2
                        WHERE t2.ID = t1.ID and datalength(t2.slntime)>0
                        ORDER BY t2.slntime--,t2.imstime,t2.medtime,t2.slntime
                        FOR XML PATH('')),
                       ' ', ','),127) as slntime

       FROM dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS t1
       GROUP BY ID,med,sln,ims,hcp

处理 12K 记录并将其减少到 10K 需要 1 小时的时间。在原始表中的 ID 列上有索引,从其中操作“dbo.STG_HCP_ATTESTTATION_ADDRESS_ATLAS”视图。

4

0 回答 0