Activity Feed: List of last activity by each user - most efficient way?

Options
I have a list of Activities performed by multiple users. A user can perform multiple activities in a row.

I want to return a list of that contains only the latest activity by each user in a list.

Last Activity by User 1
Last Activity by User 2
Last Activity by User 3


Given there could be many thousands of entries, what would be the most efficient way to approach this?

Would I use the new Stream functionality with a loop?

Or can this be done with Aggregate return somehow?

Comments

  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
    Hey, Ryan. It depends a little bit on your database structure here; are you hanging on to only a single activity in your database, or keeping a list of recent activities and only want the most recent? 
  • Ryan Anderson
    Options
    So it’s coming from one big table of ALL activity (because this data is used for a lot more) that looks something like this:

    created_at
    created_by
    company_id

    And then a bunch more fields describing the activity.

    So I want to take a list of data like this (which will always be filtered by company_id):

    User 1 | 26 September
    User 2 | 20 September
    User 1 | 8 September
    User 2 | 4 September
    User 1 | 3 September

    And return a list like this:

    User 1 | 26 September
    User 2 | 26 September

    By the time I filter by company_id and a created_date of 7 days, the largest number of total activity records returned would be about 50,000 but on average it will be more like 1,000 - 2,000 records.
  • Ryan Anderson
    Options
    Still haven't figured how to make this happen @ - any ideas?