Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 518759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:58:12+00:00 2026-05-13T07:58:12+00:00

Firstly, this DB question could be a bit DB agnostic, but I am using

  • 0

Firstly, this DB question could be a bit DB agnostic, but I am using Sql Server 2008 if that has a specialised solution for this problem, but please keep reading this if you’re not an MS Sql Server person .. please 🙂


Ok, I read in a log file that contains data for a game. Eg. when a player connects, disconnects, does stuff, etc. Nothing too hard. Works great.

Now, two of the log file entry types are

  • NewConnection
  • LostConnection

What I’m trying to keep track of are the currently connected players, to the game.
So what I originally thought of was to create a second table where it contains the most recent new connection, per player. When a player disconnects/loses connection i then remove this entry from this second table.

Eg.

Table 1: LogEntries
LogEntryId INT PK NOT NULL
EntryTypeId TINYINT NOT NULL
PlayerId INT NOT NULL
....

Table 2: ConnectedPlayers
LogEntryId INT FK (back to LogEntries table) NOT NULL

Then, I thought I could use a trigger to insert this into the cache data into the ConnectedPlayers table. Don’t forget, if it’s a trigger, it needs to handle multiple records, updates and deletes.

But I’m not sure if this is the best way. Like, could I have an Indexed View?

I would love to know people’s thoughts on this.

Oh, one more thing: for simplicity, lets just assume that when a player drops connection/lags out/modem dies/etc, the application is smart enough to know this and DOES record this as a LostConnection entry. There will be no phantom users reported as connected, when they have really got disconnected accidently, etc.


UPDATE:

I was thinking that maybe I could use a view instead? (and i can index this view if i want to, also 🙂 ) By Partitioning my results, I could get the most recent event type, per player .. where the event is a NewConnection or a LostConnection. Then only grab those most recent NewConnection .. which means they are connected. No second table/triggers/extra insert .NET code/whatever needed …

eg..

SELECT LogEntryId, EntryTypeId, PlayerId
FROM
    (SELECT LogEntryId, EntryTypeId, PlayerId
         RANK() OVER (PARTITION BY PlayerId ORDER BY LogEntryId DESC) AS MostRecentRank
     FROM LogEntries
     WHERE (EntryTypeId = 2 -- NewConnection
            OR EntryTypeId = 4 -- LostConnection)
     ) SubQuery
WHERE MostRecentRank = 1

How does that sound/look?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:58:13+00:00Added an answer on May 13, 2026 at 7:58 am

    You don’t need a second table, but you do need a date column, which I assume is part of your log data. I would normalize the data and avoid the temptation to optimize prematurely. Make sure you index the key columns, mainly the LogEntryDate and PlayerId columns in the case of your query.

    Then, use a standard aggregate query to determine the newest log entry for each user, and then filter out the ones that are not connected. You could further optimize this by only selecting from log entries from the last 24 hours (or last week or whatever makes sense for your app).

    select l.* 
    from ( 
        select PlayerId, max(LogEntryDate) as MaxLogEntryDate
        from LogEntries
        where EntryTypeId in (2,4) 
            and LogEntryDate > GetDate() - 7 --only look at the last week, as connections older than that have timed out
        group by PlayerId
    ) lm
    inner join LogEntries l on lm.PlayerId = l.PlayerId and lm.MaxLogEntryDate = l.LogEntryDate
    where l.EntryTypeId = 2 --new connections only
    

    If you find that you are still not getting the speed you want out of the query, then look at strategies for optimizing. You seem reluctant to cache in the application layer, so your proposal of indexed views would work. You could use the query above as a basis for this to create a Player view that includes a boolean IsConnected column.

    Note: if you do not receive a date with each log entry but the LogEntryId is generated by the game, that should work as a substitute for the date. If you are generating the LogEntryId on insert though, I would caution against relying on that as it would only take one out of order import to throw off all of your data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 295k
  • Answers 295k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can bypass excel automation and use OleDbConnection like this… May 13, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer A: At least on NTFS, you can attach alternate data… May 13, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer This problem is called picking. Search for mouse picking and… May 13, 2026 at 6:55 pm

Related Questions

I'm storing banner ads in a mysql table or I should say just the
Image i have a view that is cached with the OutputCache attribute but i
Firstly I tried fiding some similar posts but could not quite find anything. Mainly
So I am pulling data from a SQL Server 2000 DB then converting it
I decided to make a system for a client using Castle ActiveRecord , everything

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.