0

I have 2 tables in my DB. One tracks when users log in: id, name, IP, & time-stamp. The other is a series of events: id, event name, IP, & time-stamp.

Is there an easy way to see which user is logged on given a specific event?.

For example, if I have

id  l_ip          l_user  l_time
0   172.16.1.10   ces77   2010-12-14 09:02:12
1   172.16.1.64   ces34   2010-12-14 10:06:43
2   172.16.1.10   ces47   2010-12-14 11:10:52

and

id  e_name   e_ip         e_time
0   remove   172.16.1.10  2010-12-14 09:10:33
1   run      172.16.1.64  2010-12-14 10:15:10
2   change   172.16.1.64  2010-12-14 10:20:32
3   run      172.16.1.10  2010-12-14 12:55:59

could I run a query to give me the user for each one of the events so it would produce?

id  e_name   e_ip         e_time               l_user
0   remove   172.16.1.10  2010-12-14 09:10:33  ces77
1   run      172.16.1.64  2010-12-14 10:15:10  ces34
2   change   172.16.1.64  2010-12-14 10:20:32  ces34
3   run      172.16.1.10  2010-12-14 12:55:59  ces47

2 Answers 2

1

The best way to handle this is from code that accesses the database. It won't likely be terribly fast, because the necessary data to efficiently answer the question is not available; you'll have to look at records, and then for each record answer the question "which user was logged in at this time?".

A suggestion might be to perform a bit of de-normalization on your schema, and permit the user information to appear in two tables. If you use usernames as the key into your user table, then you can just make the l_user column be a FK pointing to that table. Of course, you'll still need a program written to update the tables to include the data that you're looking for in the first place, and there is no way to make it 100% correct for all time-stamps, unless you can guarantee certain things like "the user did not log out between timestamps A and B" and so forth.

I'd write a program first to get the answers to the question that you're looking to answer right now, and then I would strongly consider crafting a few enhancements to the data structure so that you can always answer this question with nothing more than a single SQL query. Essentially, iterate over every event, find who was logged in when that event occurred, and write that out somewhere (unless you can afford to keep that information in memory to work with it later, of course).

5
  • At first I thought the other solution worked, but later realized it didn't. It will return any user that logged in before. I think Michael is right... but that means I have to redesign some parts of the program : (
    – Nathan
    Dec 14, 2010 at 9:50
  • Assuming an user is always logged on when an event occurs, see my answer that correspond to your dataset.
    – Danosaure
    Dec 14, 2010 at 11:36
  • @Danosaure: An interesting way of doing it, but it'd be better to pay the cost of aggregating and validating the data up-front, once, and move to a model where all the appropriate information is recorded. Unless there is a really good reason to do otherwise, it makes the most sense to me to keep the cost of reading data low: there are already enough data processing tasks that take time without unnecessary extra processing to answer questions like this... Dec 14, 2010 at 15:29
  • I'm not saying the only solution is mine, but I do tend to put the load for tasks like this on the server to get the power of joins, instead of reinventing the wheel application-wise. Also DB servers tends to have more RAM/CPU than web hosts. My answer is an alternative for the OP, not to contradict your answer.
    – Danosaure
    Dec 14, 2010 at 18:34
  • Oh, no, I understand that. I was just saying that the load can be put on the DB server and can be more efficient. Short-term, kludge in the application (or report generation tool, or database); long-term, fix the underlying problem so that none of those kludges is necessary and the users do not have to pay for the cost of a design in need of improvement. That's why I said that it's better to pay for it up-front, because that is an easily hidden (from the user) cost and request time can stay fast. I know that in the apps I'm managing currently, paying per-request would kill the application. Dec 14, 2010 at 20:22
1

UPD>

SELECT e.id, e_name, e_ip, e_time, l_user
FROM events e
  INNER JOIN loggedin l ON e.e_ip = l.l_ip AND e.e_time >= l.l_time

Is this what you need?

3
  • Not exactly. The problem is that the events don't correspond to the exact time that someone logs in. I need to see the user that logged in before the event time-stamp. Thanks for the reply.
    – Nathan
    Dec 14, 2010 at 8:00
  • See update, here selects only the events happened after log in time.
    – rMX
    Dec 14, 2010 at 8:11
  • That is almost perfect; I just tweaked it a bit. Thanks a lot--you saved me hours of work!
    – Nathan
    Dec 14, 2010 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.