我想知道是否有更有效的方法来进行以下查询。
const store = createStore(rootReducer, {}, applyMiddleware(thunk));
我担心构建I_POSN_DIM列的查询。查询的执行速度很慢,似乎必须反复扫描该查询以提取结果。这是在postgresql中,我对这个平台不那么熟悉。我尝试使用使用MAX函数的CTE,但COUNT始终与此查询不同。任何帮助都会得到补偿。
答案 0 :(得分:0)
我认为是这样的:
SELECT
t.iCertification_UserCertificationTranscriptID AS I_CERT_TRANSCRIPT_DIM,
t.iHR_UserID AS I_EMP_DIM,
t.iCertification_CertificationID AS I_RUL_CERT_DIM,
uPG.iHR_PositionID AS I_POSN_DIM,
(t.dEffectiveStart.Year * 10000 + t.dEffectiveStart.Month * 100 + t.dEffectiveStart.Day) AS I_DAY_STRT_DIM,
(t.dEffectiveEnd.Year * 10000 + t.dEffectiveEnd.Month * 100 + t.dEffectiveEnd.Day) AS I_DAY_END_DIM,
CASE WHEN t.iCertification_TranscriptSourceID = '1' THEN 'Y' ELSE 'N' END AS L_AdminOverride,
t.mComment AS X_Comment
FROM dbo.tCertification_UCT AS t
INNER JOIN UserProfileNoGroups uPG ON t.iHR_UserID = uPG.iHR_UserID
WHERE Profile_IsPrimary = '1'
AND UserPosition_IsPrimary = '1'
AND (t.dEffectiveStart BETWEEN UserPosition_Effective AND COALESCE(UserPosition_End, LOCALTIMESTAMP));
我将内部查询移到外部,并在等效的内部联接中将其转换。在其他字段中,您确实进行过强制转换,但这不是必需的,因为该操作的结果是一个整数。
但是我认为表UserProfileNoGroups在tCertification_UCT的每个记录都有1条记录。如果不是这样,查询将更改一个小标题:
SELECT
t.iCertification_UserCertificationTranscriptID AS I_CERT_TRANSCRIPT_DIM,
t.iHR_UserID AS I_EMP_DIM,
t.iCertification_CertificationID AS I_RUL_CERT_DIM,
uPG.iHR_PositionID OVER (ORDER BY UserPosition_Effective DESC) AS I_POSN_DIM,
(t.dEffectiveStart.Year * 10000 + t.dEffectiveStart.Month * 100 + t.dEffectiveStart.Day) AS I_DAY_STRT_DIM,
(t.dEffectiveEnd.Year * 10000 + t.dEffectiveEnd.Month * 100 + t.dEffectiveEnd.Day) AS I_DAY_END_DIM,
CASE WHEN t.iCertification_TranscriptSourceID = '1' THEN 'Y' ELSE 'N' END AS L_AdminOverride,
t.mComment AS X_Comment
FROM dbo.tCertification_UCT AS t
INNER JOIN UserProfileNoGroups uPG ON t.iHR_UserID = uPG.iHR_UserID
WHERE Profile_IsPrimary = '1'
AND UserPosition_IsPrimary = '1'
AND (t.dEffectiveStart BETWEEN UserPosition_Effective AND COALESCE(UserPosition_End, LOCALTIMESTAMP));
您可以使用OVER子句: