0

我有 2 个 SQL,

    SELECT  @AlreadyPerformedActionsPerDay = COUNT(*)
    FROM    UserRewardActions URA
            INNER JOIN UserRewardActionTypes URAT ON URAT.ID = URA.UserRewardActionTypeID
    WHERE   URA.UserID = @UserID
            AND (
                    @UserRewardActionTypeID = 1 -- Because the register action is not per-date-wise(only allows once in lifetime)
                    OR CreationDateInUtc BETWEEN CAST(GETUTCDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETUTCDATE() AS DATE)) -- Inside Current Date
                );

    SELECT  @AlreadyRedeemedTheCurrentAction = 1
    FROM    UserRewardActions URA
            INNER JOIN UserRewardActionTypes URAT ON URAT.ID = URA.UserRewardActionTypeID
    WHERE   URA.UserID = @UserID
            AND CreationDateInUtc BETWEEN CAST(GETUTCDATE() AS DATE) AND DATEADD(DAY, 1, CAST(GETUTCDATE() AS DATE)) -- Inside Current Date
            AND URL IS NOT NULL 
            AND URL = @Url;

第一个 SQL 是今天抓取的用户操作总数。其次,检查当前 url 是否已被同一用户使用。这可以。但我希望合并此查询以解决性能问题。

4

1 回答 1

1

好吧,在重新格式化您的查询后,如下所示。我可以看到他们有不同的标准。

DECLARE @from DateTime;
DECLARE @to DateTime;

SET @from  = cast(getutcdate() as Date);
SET @to = dateadd(DAY, 1, @from));

SELECT
                @AlreadyPerformedActionsPerDay = COUNT(*)
    FROM
                UserRewardActions URA
        JOIN
                UserRewardActionTypes URAT
                    ON URAT.ID = URA.UserRewardActionTypeID
        WHERE
                URA.UserID = @UserID
            AND
                (
                    @UserRewardActionTypeID = 1
                 OR 
                    CreationDateInUtc BETWEEN @from AND @to
                );

SELECT
                @AlreadyRedeemedTheCurrentAction = 1
    FROM
                UserRewardActions URA
         JOIN
                UserRewardActionTypes URAT
                    ON URAT.ID = URA.UserRewardActionTypeID
    WHERE
                URA.UserID = @UserID
        AND
                CreationDateInUtc BETWEEN @from AND @to
        AND
                URL IS NOT NULL 
        AND
                URL = @Url;
于 2013-09-24T10:00:37.783 回答