SQL查询优化
-- 统计某一段时间登录过系统,且同时拥有2种物品的用户总数
select count(distinct(i.user_no))
from inventory i
where i.user_no in (select i.user_no
from inventory i, timelog l
where i.user_no = l.user_no
and l.logindate between
to_date('20070101000000', 'yyyymmddhh24miss') and
to_date('20070107235959', 'yyyymmddhh24miss')
and i.itemid = 10002)
and i.itemid = 10001
说明:
timelog表中包括user_no和logindate字段,用户每次登录系统都会产生一条记录
inventory表中包括user_no和itemid字段,当用户拥有某种物品时,其中会有一条对应的记录
我的问题是:感觉这样查询的效率不高,但是又不知道怎样优化语句,请达人指点一二,谢谢!