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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:37:05+00:00 2026-06-04T08:37:05+00:00

So, i have a table with roughly 1.5 million rows in it, looking somewhat

  • 0

So, i have a table with roughly 1.5 million rows in it, looking somewhat like this:

name   | time       | data1 | data2  
--------------------------------------
 93-15 | 1337348782 |   11  | 60.791 
 92-02 | 1337348783 |   11  | 62.584 
 92-02 | 1337348056 |   11  | 63.281
 93-15 | 1337348068 |    8  | 65.849
 92-02 | 1337348117 |   11  | 63.271 
 93-15 | 1337348129 |    8  | 65.849 
 92-02 | 1337348176 |   10  | 63.258 
 93-15 | 1337348188 |    8  | 65.849 
 92-02 | 1337348238 |   10  | 63.245 
 93-15 | 1337348248 |    8  | 65.849  

…these correspond to historical status updates from something that needs to be monitored. Now, what i would like to do is to find the current status if each unit.

It wasn’t hard finding similar questions here on stackoverflow, and extrapolating from the findingsat, i came up with this query:

SELECT * FROM vehicles v
  JOIN ( SELECT  MAX(time) as max, name
    FROM vehicles
    GROUP BY name)
  m_v
ON (v.time = m_v.max AND v.name = m_v.name);

but seeing as i have roughly 1.5 million rows (and counting), is there a different approach that allows for a faster query?

  • 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-06-04T08:37:06+00:00Added an answer on June 4, 2026 at 8:37 am
    WITH
      sequenced_data
    AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY name ORDER BY time DESC) AS sequence_id,
        *
      FROM
        vehicles
    )
    SELECT
      *
    FROM
      sequenced_data
    WHERE
      sequence_id = 1
    

    A covering index on (name, time) would be helpful too.

    EDIT: Notes on how it work, etc.

    PostgreSQL has what are know as windowing or analytical functions. These generally take the form some_function() OVER (PARTITION BY some_fields ORDER BY some_fields).

    In this case I used ROW_NUMBER() OVER (PARTITION BY name ORDER BY time DESC).

    ROW_NUMBER() creates a unique row number for a set of data. 1 to n for n records.

    PARTITION BY name means that this function is applied independently to different names. Each name is it’s own group/window/partition, and the results of ROW_NUMBER() start over from 1 again for each group/window/partition.

    ORDER BY time DESC takes all the records in one group/window/partition and orders them by the time field, with the highest value first, before the ROW_NUMBER() function is applied.

    For your example data, therefore, you get this…

     name  | time       | data1 | data2  | row_number
    --------------------------------------------------
    
     92-02 | 1337348783 |   11  | 62.584 | 1
     92-02 | 1337348238 |   10  | 63.245 | 2
     92-02 | 1337348176 |   10  | 63.258 | 3
     92-02 | 1337348117 |   11  | 63.271 | 4
     92-02 | 1337348056 |   11  | 63.281 | 5
    
     93-15 | 1337348782 |   11  | 60.791 | 1
     93-15 | 1337348248 |    8  | 65.849 | 2
     93-15 | 1337348188 |    8  | 65.849 | 3
     93-15 | 1337348129 |    8  | 65.849 | 4
     93-15 | 1337348068 |    8  | 65.849 | 5
    

    Because the ordering is time DESC, the highest valued time field, in each name group/window/partition, will always have a row_number of 1.

    Having an index on (name, time) makes it much easier for the optimiser by ensuring the data is in a friendly order. This means that ROW_NUMBER() isn’t actually applied to all the records; as soon as it finds the highest valued time record, and assigns ROW_NUMBER() = 1, it knows it can stop and move on to the next name.

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

Sidebar

Related Questions

I have a table with roughly 1 million rows. I'm doing a simple program
So, I have a mysql table that looks roughly like this, where the first
We have a table that looks roughly like this: CREATE TABLE Lockers { UserID
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I have an entity which is declared roughly like: @Entity @Table(name = myUserTable) public
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have a user table that has many columns, it looks roughly like this:
I have an as400 table containing roughly 1 million rows of full names /
I have some data in a table that looks roughly like the following: table
I have 2 columns in a table and I would like to roughly report

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.