473,387 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Max Simultaenous requests based on Date/Time

Hi,

I have a table in which i have the information about actions along with the date/time when the action happened. I want to know maximum simultaneous requests for a given action at any given time.

Example: Users signing into web site. Here is some sample data (using integers at timestamps to keep the example simple) :

1. User A logs in at time 0.
2. User B logs in at time 0.
3. User C logs in at time 1.
4. User D logs in at time 3.
5. User A logs out at time 4.
6. User E, F and G logs in at time 5.
7. User C logs out at time 6.
8. All Users log out at time 7.

Now, I want to run a query on start time = 0, end time = 7 and I want to know what was the maximum simultaneous count for the number of users logged in?

Based on the data, the user/time distribution is:
time 0: 2 users
time 1: 3 users
time 2: 3 users
time 3: 4 users
time 4: 3 users
time 5: 6 users
time 6: 5 users
time 7: 0 users

and so, max. simultaneous users between 0 and 7 is 6 (at time = 5). How can I find this using SQL query?

Table structure:
1. Each row has log-in and log-out time.
2. Record is written to the table once user logs-out. Till that time, its kept in memory. (Changing this behavior is not an option right now).

You can assume that this table only stores log-in and log-out actions.
Aug 4 '07 #1
3 1857
pbmods
5,821 Expert 4TB
Heya, ataulhaq. Welcome to TSDN!

The problem with trying to do this with a query is that you always have to start from time 0 or else your count might not be valid.

For example, if there are two Users logged in at time 0, but you start counting with time 2, your result set will always be off by two Users.

And what happens after your site has been around for awhile, and you want to get the max. simultaneous logins up to time 1,500,000?

The easiest way to accomplish this would be to create a counter that keeps track of the number of Users that are logged in and then saving it with each entry in the table.

E.g.,
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         MAX(`UserCount`)
  3.     FROM
  4.         `LoginActivity`
  5.     WHERE
  6.         `Time` BETWEEN 0 AND 7
  7.     GROUP BY
  8.         `Time`
  9.  
Now, the *real* fun kicks in when your Users don't log themselves out when they're done using your site....
Aug 5 '07 #2
Heya, ataulhaq. Welcome to TSDN!

The problem with trying to do this with a query is that you always have to start from time 0 or else your count might not be valid.

For example, if there are two Users logged in at time 0, but you start counting with time 2, your result set will always be off by two Users.

And what happens after your site has been around for awhile, and you want to get the max. simultaneous logins up to time 1,500,000?

The easiest way to accomplish this would be to create a counter that keeps track of the number of Users that are logged in and then saving it with each entry in the table.

E.g.,
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         MAX(`UserCount`)
  3.     FROM
  4.         `LoginActivity`
  5.     WHERE
  6.         `Time` BETWEEN 0 AND 7
  7.     GROUP BY
  8.         `Time`
  9.  
Now, the *real* fun kicks in when your Users don't log themselves out when they're done using your site....
Hi,

Well, the counts may have created the situation more confusiong. The real data in database has date/time stamps. I just didn't want to write the whole timestamp and so used counts.

So, the comment regarding starting from count 0 may not be applicable since the query would be between two timestamps always. Like I want to know maximum number of users between 2007-06-01 10:00:00 and 2007-06-03 10:00:00.

I agree with the counter approach and that is what we are planning for but I was just wondering if there is a way we can do it using SQL. We do have a solution to it but it is not pure SQL. We get two results sets. One for log-in time and one for log out time and we merge both sets and sort it. So for every log in, we increment the count and for every log out we decrement it. The maximum count is the max simultaneous users within given time period.
Aug 6 '07 #3
pbmods
5,821 Expert 4TB
Heya, ataulhaq.

The issue with doing this using a query is that you have to take into account the number of Users who were logged in before you start calculating.

For example, assume 2 Users logged in at 2007-05-31 13:45:00 and 2007-05-31 13:45:05, respectively.

Here's where the problem occurs:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     .
  3.     .
  4.     .
  5.     WHERE
  6.         `datetime` BETWEEN '2007-06-01 00:00:00' AND '2007-06-30 11:59:59'
  7.  
Notice that the number of active Users on June 1st will be at least two off because it won't take into account the two Users who logged in on May 31st without logging off.

And if the Users that were logged in on May 31st then log out on June 1st, your User count might even be negative!

I think your best bet is to either update a counter on login/logout, or you can do what phpBB does, but you'll have to combine it with a periodic script (such as cron, or by implementing a cron script and hope your traffic is frequent).

The way phpBB does it is the database stores the timestamp of the last action taken by the User (i.e., every time the User sends a request, the timestamp updates).

Then, to find out the number of *active* Users, simply count the number of timestamps greater than 15 minutes ago.

To get a history of this value, you'd need to run a script once per day and count (and index) the number of timestamps greater than 1 day ago.

The counter idea is probably easier, though the 'active' counter is probably a better benchmark.
Aug 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Fritz Bosch | last post by:
We are in the process of refactoring our GUI-based test application for radio equipment and are rewriting a significant part in Python. The new architecture will substantially be based on the...
4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
3
by: Bond | last post by:
how do I know when the browser is making a request to the server? I am not having an onclick event for EVERY hyperlink, submit, etc. There must be some javascript function that I can overwrite...
1
by: Dominic | last post by:
Hi all, We've just migrated to IIS 6.0 / Windows Server 2003. We are now experiencing some stability problem what we did not experience in IIS 5.0 / Windows 2000 Server. Our ASP.NET application...
2
by: Peter McEvoy | last post by:
Folks, I've been building a Webservice API for a contract that will be exposed to the internet at large. There are two endpoints, and each endpoint contains a number of webmethods. Every...
5
by: linda.chen | last post by:
Hi all, We have a webservice (service1), which calls another webservice(service2) from another orginization. Our end users make requests throught service1. When service2 receives a quest, it...
6
by: trytobreak | last post by:
Hi All, I am a network administrator in a fairly large software company and I would like to write myself a small utility, which would connect (one by one) to all machines on the network and get...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I want to limit the user only login the system one time at the same time. I don't want him login the system two with the same user at the same time. How to do this? If i have a table to record...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.