My boss sent us a query and asked us to replace it with a more efficient version and to achieve following objective:
Get all records from
PACKAGE
table which has at least 1 record inPASS_PACKAGE_DETAILS
table.
Given SQL:
SELECT DISTINCT
pckg.*
FROM
PASS_PACKAGE pckg
JOIN
PASS_PACKAGE_DETAILS pckg_dtl ON (pckg.PACKAGE_ID = pckg_dtl.PACKAGE_ID)
WHERE
IS_ACTIVE = 1
AND '2015/04/22' BETWEEN DATE_START AND DATE_END
ORDER BY
PACKAGE_NAME
Correct me if I'm wrong, but I believe query above will slow down the performance due to the JOIN
method. After reading this, I'm wondering which of my query does really achieved my boss requirement and why.
My SQL :
Attempt #1 - using IN
:
SELECT
pckg.*
FROM
PASS_PACKAGE pckg
WHERE
IS_ACTIVE = 1
AND '2015/04/22' BETWEEN DATE_START AND DATE_END
AND pckg.PACKAGE_ID IN (SELECT DISTINCT pckg_dtl.PACKAGE_ID
FROM PASS_PACKAGE_DETAILS pckg_dtl)
ORDER BY
PACKAGE_NAME
Attempt #2 - using EXISTS
:
SELECT
pckg.*
FROM
PASS_PACKAGE pckg
WHERE
IS_ACTIVE = 1
AND '2015/04/22' BETWEEN DATE_START AND DATE_END
AND EXISTS (SELECT pckg_dtl.PACKAGE_ID
FROM PASS_PACKAGE_DETAILS pckg_dtl
WHERE pckg_dtl.package_id = pckg.package_id)
ORDER BY
PACKAGE_NAME
Hope to get some valuable information from experts here!
EDIT: I'm using SQL Server Management Studio. My execution time is kinda weird. For IN
method, 1st execution will take around 90+ ms, 2nd execution like 200 ms, 3rd execution back to 90+ ms. So I'm not sure which execution time I should refer to. Same goes for the EXISTS
method.