我可以以任何方式优化此SQL查询吗?

时间:2012-06-27 06:50:02

标签: tsql

我有三个名为edum_application,edua_courseEvent和edua_coursePackage的表。

我正在编写一个问题来获取edum_application中的所有应用程序,其中所有courseEvent的coursePackage_id都是x并且都具有相同的applicationWrapper_id

此查询有效,但效率如何?

SELECT app.application_id, app.applicationWrapper_id
FROM edum_application AS app
    INNER JOIN edua_courseEvent AS ce ON app.courseEvent_id = ce.courseEvent_id
WHERE app.applicationWrapper_id = 662
    AND app.courseEvent_id IN
    (
        -- Is this really efficient?
        SELECT ce.courseEvent_id
        FROM edua_courseEvent AS ce
        WHERE ce.coursePackage_id =
        (
            SELECT coursePackage_id
            FROM edua_courseEvent AS ce
            WHERE ce.courseEvent_id = 13377
        )
    )

输出:

application_id  applicationWrapper_id
72643           662
72645           662
72646           662
72647           662

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT     app.application_id, app.applicationWrapper_id
FROM       edum_application AS app
INNER JOIN edua_courseEvent AS ce
ON         app.courseEvent_id = ce.courseEvent_id
INNER JOIN edua_courseEvent AS ce2
ON         ce.coursePackage_id = ce2.coursePackage_id
WHERE      app.applicationWrapper_id = 662
AND        ce2.courseEvent_id = 13377

您可能需要DISTINCT,具体取决于不同表之间/关系中的行数。