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 169639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:40:00+00:00 2026-05-11T12:40:00+00:00

How do I best gather messages that have not been read by a given

  • 0

How do I best gather messages that have not been read by a given user?

Existing tables

Message table ---------------------------------- id    title    body    sentAt  User table ---------------------------------- id    username  Read Messages table ---------------------------------- user_id    message_id 

I’m thinking something like

select    m.id, m.title, m.sentAt, u.username from    message m,   [user] u where    u.id = 1 and -- @userId parameter   m.id not in      (select r.message_id from read_messages r where r.user_id = u.id) 

Unfortunately for me I dont understand much looking at the execution plan. /Adam

  • 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. 2026-05-11T12:40:00+00:00Added an answer on May 11, 2026 at 12:40 pm

    Suggesting an alternative approach:

    I was faced with the exact same problem at work earlier. I wasted a good week in trying to figure the best way to do this. I ended up with creating a join table, as you have done, but the table contains only unread messages, instead of keeping track of read messages.

    Because

    1. The status quo is ‘everyone has read all their messages’.
    2. Getting the unread messages (or their count) should be as fast as possible.
    3. The status quo should be the least straining status on the system.

    Now, if I would’ve kept track of all the messages everyone has read, the clutter in the database grows pretty rapidly (users*messages rows), easily leading to thousands of rows of ‘dead weight’ in even smaller applications. This problem is exaggerated if the lifetime of messages are indefinite – you could be keeping track of message statuses that are many years old.

    If keeping track of the inverse, your ‘unread messages’ table contains only a handful of rows, and they diminish for each message that a user reads. Also, getting the amount of unread messages is as simple as ‘SELECT COUNT(*) FROM unread WHERE user = foo‘.

    But

    As everything, this is a trade-off. While reading is pretty much as fast as computationally possible, writing is a chore. For each written message, you need to insert an entry to this join table. Additionally, if multiple people can read the same message, you need to insert one row for each recipient. If the recipients are implicit (e.g. only a user group’s name is given, or even with the criteria such as ‘anyone who has access to this thing’), creating new messages becomes even more complicated.

    But I feel this is a fair compromise.

    YMMV, HTH.

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

Sidebar

Related Questions

I'm not sure how best to approach my problem. I have a service with
I have a site that has user submissions that users can vote on. I
Afternoon, Lets say I have gather a random selection of users: User.find(:all, :limit =>
Best method to add/remove webcontrols or user controls in asp.net for dynamic forms without
Best to describe this in code... I have this public class A<T> { public
Best to use an example to describe the problem. Lets say I have a
Best practices or tools for installing a SQL Server database I have a SQL
I have a very large text file and I need to gather data from
I'm trying to find the best method to gather URLs, I could create my
Does anyone have any suggestions for the best / simplest way to view all

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.