S
S
SHA10242020-08-07 11:09:39
Transact SQL
SHA1024, 2020-08-07 11:09:39

How to optimize a TSQL query of a similar design?

Help optimize the query.

SELECT
    id = (
        SELECT
            TOP 1 id
        FROM
            learnings
        WHERE
            person_id = l.person_id
            AND course_id = l.course_id
        ORDER BY
            (
                CASE
                    WHEN state_id = 4 THEN 5
                    WHEN state_id = 2 THEN 4
                    WHEN state_id = 5 THEN 3
                    WHEN state_id = 3 THEN 2
                    ELSE state_id
                END
            ) DESC,
            score DESC,
            creation_date DESC
    ),
    l.person_id,
    l.course_id
FROM
    @LEARNINGS as l

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Skorzhinsky, 2020-08-10
@AndyKorg

Read about execution plans .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question