I have two tables with large amounts of data.
ACTION_SUMMARY
and ACTION_DETAIL
Every user has one ACTION_SUMMARY
row per day in my db, and zero, one or more ACTION_DETAIL
rows per ACTION_SUMMARY
.
I'd like to have a query that does returns users that have at least one detail record with an action type (ACTYP_ID
) in some set of values.
Here is one example:
select
AS.USER_ID
from
ACTION_SUMMARY AS
JOIN ACTION_DETAIL AD on AS.AS_ID = AD.AS_ID
where
AS.DATE between '2015-01-01' and '2015-07-07'
and AD.ACTYP_ID in (45, 25, 11)
AS.DATE
has an index on it. However since there are over 2Million users in the DB, and each summary has on average 5-10 detail records, I'm running into a performance issue.
I was thinking of using EXISTS
in this fashion:
select
AS.USER_ID
from
ACTION_SUMMARY AS
where
AS.DATE between '2015-01-01' and '2015-07-07'
and EXISTS (select 1 from ACTION_DETAIL AD where AD.AS_id = AS.AS_ID and AD.ACTYP_ID in (45, 25, 11))
Now, I have two questions:
1) is my query with EXISTS
faster - as in will the sub query stop as soon as it finds something and move on?
2) How can I improve my query?
I have indexes on AS.DATE
, AS.AS_ID
, AD.AS_ID
and AD.ACTYP_ID
Thanks