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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:16:16+00:00 2026-06-13T00:16:16+00:00

I have a User table with 1m records: User (id, fname, lname, deleted_at, guest)

  • 0

I have a User table with 1m records:

User (id, fname, lname, deleted_at, guest)

I have the following query which is being run against a postgres 9.1 db:

SELECT "users".* 
FROM "users" 
WHERE (users.deleted_at IS NULL) AND (SUBSTRING(lower(fname), 1, 1) = 's') 
ORDER BY guest = false, fname ASC 
LIMIT 25 OFFSET 0

Using pgAdmin 3, this SQL is taking 7120ms to return 25 rows. If I remove the ‘ORDER BY guest = false, fname ASC’ the query takes just 31ms.

I have the following indexes:

add_index "users", ["fname"], :name => "index_users_on_fname"
add_index "users", ["guest", "fname"], :name => "index_users_on_guest_and_fname"
add_index "users", ["deleted_at"], :name => "index_users_on_deleted_at"
add_index "users", ["guest"], :name => "index_users_on_guest"

Any ideas? Thank you!

UPDATED with Explain

"Limit  (cost=43541.55..43541.62 rows=25 width=1612) (actual time=1276.777..1276.783 rows=25 loops=1)"
"  ->  Sort  (cost=43541.55..43558.82 rows=6905 width=1612) (actual time=1276.775..1276.777 rows=25 loops=1)"
"        Sort Key: ((NOT guest)), fname"
"        Sort Method: top-N heapsort  Memory: 37kB"
"        ->  Seq Scan on users  (cost=0.00..43346.70 rows=6905 width=1612) (actual time=5.143..1272.563 rows=475 loops=1)"
"              Filter: ((deleted_at IS NULL) AND pubic_profile_visible AND ((fname)::text ~~ 's%'::text))"
"Total runtime: 1276.967 ms"
  • 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-13T00:16:17+00:00Added an answer on June 13, 2026 at 12:16 am

    First, since PostgreSQL 9.1 you can use left() to simplify the expression:

    substring(lower(fname), 1, 1)
    lower(left(fname, 1)) -- equivalent, but simpler and faster
    

    Also slightly faster to take the first character before casting to lower case.
    Next, clean up the query:

    SELECT * 
    FROM   users 
    WHERE  deleted_at IS NULL
    AND    lower(left(fname, 1)) = 's'
    ORDER  BY guest DESC NULLS LAST, fname
    LIMIT  25 OFFSET 0;
    

    guest DESC NULLS LAST results in the same as guest = FALSE, just without calculating a new value for every row.
    Next, create this multi-column partial index:

    CREATE INDEX users_multi_idx
    ON users (lower(left(fname, 1)), guest DESC NULLS LAST, fname)
    WHERE deleted_at IS NULL;
    

    Run

    ANALYZE users;
    

    Or, even better, CLUSTER (if you don’t have more important queries requiring a different order) – and then ANALYZE:

    CLUSTER users using users_multi_idx;
    

    And it will be way faster than anything you tried before. Because now, the query reads rows from the index sequentially and the table has been physically rewritten in the same order, resulting in only few page hits …

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

Sidebar

Related Questions

I have a User table which has a PrivilegeId foreign key points to a
In SQL Server 2008 I have a user table like the following: Userid Username
I have the following Join statement : SELECT DISTINCT dn.fname, dn.lname,w.websit,q.qual,q.year,q.postqual,p.pnumber,a.accred,n.nspecial FROM cur_doctor_names dn
I have a user table that contain 8 records. I want to arrange the
I have a User entity which represents a User table in my database. This
I have a table(user_admin_password). I am using this query to get the records. SELECT
I have a table that holds the card, which is described by the following
We have a user table which contains all the users from the company. As
I have a User table that has a many records to 1 user relationship.
I have a Car table and User table which store car and user particular.

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.