我正在尝试计算站点行驶时间内会员的行驶时间百分位。
我有两个表 1 一个临时表 #OrganizationNidSiteMetadataBase,有 24800521 条记录:
与组织和站点相关的地理区域以及到周边地理区域的车程时间。
然后我还有另一个表 FactSportMemberMetadata:
会员组织和站点以及地理区域以及会员到达主站点的车程。
是网站和地理区域内成员的行驶时间百分位数。
SELECT r.OrganisationId,
NidSiteId,
NidSiteGeographicIdentifier,
CatchmentLevel0GeographicIdentifier,
CatchmentLevel1GeographicIdentifier,
SiteDriveTime,
ROUND(CAST(COUNT(CASE WHEN curr.DriveTimeInMinutes < r.SiteDriveTime THEN 1 END) AS FLOAT) / CAST(COUNT(*) AS FLOAT) * 100, 0) AS DriveTimePercentileCurrent
INTO #RecencyCurrent
FROM #OrganisationNidSiteMetadataBase r
JOIN FactSportMemberMetadata curr
ON r.OrganisationId = curr.OrganisationId
AND r.NidSiteId = curr.HomeSiteNidSiteId
AND curr.MonthsSinceLastTransactionDate = 0
GROUP BY r.OrganisationId, NidSiteId, NidSiteGeographicIdentifier, CatchmentLevel0GeographicIdentifier,CatchmentLevel1GeographicIdentifier,SiteDriveTime
我也尝试过:
SELECT r.OrganisationId,
NidSiteId,
NidSiteGeographicIdentifier,
CatchmentLevel0GeographicIdentifier,
CatchmentLevel1GeographicIdentifier,
SiteDriveTime,
AVG(CASE WHEN curr.DriveTimeInMinutes < r.SiteDriveTime THEN 1.0 ELSE 0.0 END) * 100 AS DriveTimePercentileCurrent
INTO #RecencyCurrent2
FROM #OrganisationNidSiteMetadataBase r
JOIN FactSportMemberMetadata curr
ON r.OrganisationId = curr.OrganisationId
AND r.NidSiteId = curr.HomeSiteNidSiteId
GROUP BY r.OrganisationId, NidSiteId, NidSiteGeographicIdentifier, CatchmentLevel0GeographicIdentifier,CatchmentLevel1GeographicIdentifier,SiteDriveTime
但也需要同样长的时间。
有没有一种方法可以使计算字段更加高效,因为这是永远需要的?
我尝试使用 AVG() 函数,但这对运行查询所需的时间没有影响。
我还尝试将计算分解为更小的查询,但这使其运行时间更长。
除了上面评论中写的内容之外。在这种情况下创建索引将使事情变得混乱且困难。
在 #OrganizationNidSiteMetadataBase 中创建可为空的列 DriveTime,然后更新
Update r
set DriveTime=1
FROM #OrganisationNidSiteMetadataBase r
JOIN FactSportMemberMetadata curr
ON r.OrganisationId = curr.OrganisationId
AND r.NidSiteId = curr.HomeSiteNidSiteId
where curr.MonthsSinceLastTransactionDate = 0
and (curr.DriveTimeInMinutes < r.SiteDriveTime )
那
curr.DriveTimeInMinutes > r.SiteDriveTime
呢?
那么计算就会变成,
ROUND(COUNT(DriveTime) / CAST(COUNT(*) AS FLOAT) * 100, 0) AS DriveTimePercentileCurrent
我认为由于这个问题,您的查询中涉及了额外的行数。过滤掉这些额外的行并应用索引