CanNeverThinkOfaName
Programmer
Hi
i need to count the number of people who are logged in over and including 3 minutes
eg user a logs in at 9:00 and logs out at 9:05
user b logs in at 9:00 and logs out at 9:01
user c user a logs in at 9:00 and logs out at 9:03
from example above that would be users a and c!
the row data exists in my database as
accountid,timestamp,event
user a, 9:00, logged in
user a, 9:01, logged in
user a, 9:02, logged in
user a, 9:03, logged in
user a, 9:04, logged in
user a, 9:05, logged out
etc
the sql below works great for what I want but it takes about an hour to run when I look for data in between 2 dates
Can anyone think of any way to speed up the SQL here at all- admittedly table Y is huge and the database if underspec'd and very slow but an hour is still too long for it to take!
Thanks in advance for any help!
i need to count the number of people who are logged in over and including 3 minutes
eg user a logs in at 9:00 and logs out at 9:05
user b logs in at 9:00 and logs out at 9:01
user c user a logs in at 9:00 and logs out at 9:03
from example above that would be users a and c!
the row data exists in my database as
accountid,timestamp,event
user a, 9:00, logged in
user a, 9:01, logged in
user a, 9:02, logged in
user a, 9:03, logged in
user a, 9:04, logged in
user a, 9:05, logged out
etc
the sql below works great for what I want but it takes about an hour to run when I look for data in between 2 dates
Code:
SELECT X.ACCOUNTID, B.TITLE, X.NO_OF_MINS_WATCHED, X.START_WATCHING, trim(to_char(extract(month from trunc(add_months(sysdate, -1), 'MM')),'09')||extract(year from trunc(add_months(sysdate, -1), 'MM')))
FROM
( SELECT accountid,channelid,
MIN (usagetimestamp )KEEP (DENSE_RANK FIRST ORDER BY usagetimestamp) start_watching,
COUNT (*) NO_OF_mins_watched
FROM
(SELECT B.*, SUM (FLAG) OVER (PARTITION BY accountid, channelid ORDER BY usagetimestamp) GRP
FROM (SELECT accountid,showid,usagetimestamp,channelid,
DECODE
( (usagetimestamp - LAG (usagetimestamp) OVER (PARTITION BY accountid , channelid ORDER BY usagetimestamp)) *24*60 ,0, 0, 1, 0, 2, 0, 1) FLAG
FROM Y i)
B
)
GROUP BY accountid,channelid, GRP ) X, B WHERE ( B.EXTERNALNO(+)=X.CHANNELID ) AND ( ( X.START_WATCHING ) between trunc(add_months(sysdate, -1), 'MM') and last_day(trunc(add_months(sysdate, -1),'MM')) )
Can anyone think of any way to speed up the SQL here at all- admittedly table Y is huge and the database if underspec'd and very slow but an hour is still too long for it to take!
Thanks in advance for any help!