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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:57:55+00:00 2026-05-27T02:57:55+00:00

Suppose you want to find the last record entered into the database (highest ID)

  • 0

Suppose you want to find the last record entered into the database (highest ID) matching a string: Model.where(:name => 'Joe'). There are 100,000+ records. There are many matches (say thousands).

What is the most efficient way to do this? Does PostgreSQL need to find all the records, or can it just find the last one? Is this a particularly slow query?

Working in Rails 3.0.7, Ruby 1.9.2 and PostgreSQL 8.3.

  • 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-27T02:57:56+00:00Added an answer on May 27, 2026 at 2:57 am

    The important part here is to have a matching index. You can try this small test setup:

    Create schema xfor testing:

    -- DROP SCHEMA x CASCADE;  -- to wipe it all for a retest or when done.
    CREATE SCHEMA x;
    CREATE TABLE x.tbl(id serial, name text);
    

    Insert 10000 random rows:

    INSERT INTO x.tbl(name) SELECT 'x' || generate_series(1,10000);
    

    Insert another 10000 rows with repeating names:

    INSERT INTO x.tbl(name) SELECT 'y' || generate_series(1,10000)%20;
    

    Delete random 10% to make it more real life:

    DELETE FROM x.tbl WHERE random() < 0.1;
    
    ANALYZE x.tbl;
    

    Query can look like this:

    SELECT *
    FROM   x.tbl
    WHERE  name = 'y17'
    ORDER  BY id DESC
    LIMIT  1;
    

    –> Total runtime: 5.535 ms

    CREATE INDEX tbl_name_idx on x.tbl(name);
    

    –> Total runtime: 1.228 ms

    DROP INDEX x.tbl_name_idx;
    CREATE INDEX tbl_name_id_idx on x.tbl(name, id);
    

    –> Total runtime: 0.053 ms

    DROP INDEX x.tbl_name_id_idx;
    CREATE INDEX tbl_name_id_idx on x.tbl(name, id DESC);
    

    –> Total runtime: 0.048 ms

    DROP INDEX x.tbl_name_id_idx;
    CREATE INDEX tbl_name_idx on x.tbl(name);
    CLUSTER x.tbl using tbl_name_idx;
    

    –> Total runtime: 1.144 ms

    DROP INDEX x.tbl_name_id_idx;
    CREATE INDEX tbl_name_id_idx on x.tbl(name, id DESC);
    CLUSTER x.tbl using tbl_name_id_idx;
    

    –> Total runtime: 0.047 ms

    Conclusion

    With a fitting index, the query performs more than 100x faster.
    Top performer is a multicolumn index with the filter column first and the sort column last.
    Matching sort order in the index helps a little in this case.

    Clustering helps with the simple index, because still many columns have to be read from the table, and these can be found in adjacent blocks after clustering. It doesn’t help with the multicolumn index in this case, because only one record has to be fetched from the table.
    Read more about multicolumn indexes in the manual.

    All of these effects grow with the size of the table. 10000 rows of two tiny columns is just a very small test case.

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

Sidebar

Related Questions

I just want to check my logic there. Suppose I want to find all
Suppose we want to find out what will be the color property of particular
Suppose I want to find 2nd bit in binary equivalent of 13 (binary :
Suppose I want to find the longest subsequence such that first half of subsequence
Suppose I have a sequence x1,x2,x3.....xn, and I want to find the longest continuous
Suppose I want to put objects that identify a server into a stl set
I want to find the first and the last occurrences of a specific character
I want to find the position (or index) of the last occurrence of a
Suppose the mongodb document(table) 'users' is { _id: 1, name: { first: 'John', last:
sample data is this,who can i find greater number,whenever first and last digit suppose

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.