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

  • Home
  • SEARCH
  • 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 9137257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:04:52+00:00 2026-06-17T09:04:52+00:00

What if I want to search for a single row in a table with

  • 0

What if I want to search for a single row in a table with a decrementing precision, e.g. like this:

SELECT * FROM image WHERE name LIKE 'text' AND group_id = 10 LIMIT 1

When this gives me no result, try this one:

SELECT * FROM image WHERE name LIKE 'text' LIMIT 1

And when this gives me no result, try this one:

SELECT * FROM image WHERE group_id = 10 LIMIT 1

Is it possible to do that with just one expression?

Also there arises a problem when I have not two but e.g. three or more search parameters. Is there a generic solution for that? Of course it would come in handy when the search result is sorted by its relevance.

  • 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-17T09:04:53+00:00Added an answer on June 17, 2026 at 9:04 am

    Test setup

    CREATE TABLE image (
      image_id serial PRIMARY KEY
    , group_id int NOT NULL
    , name     text NOT NULL
    );
    

    Indexes are the key ingredient for performance. Ideally, you create these two in addition to the primary key:

    CREATE INDEX image_name_grp_idx ON image (name, group_id);
    CREATE INDEX image_grp_idx ON image (group_id);
    

    The second may not be necessary, depending on data distribution and other details. See:

    • Is a composite index also good for queries on the first field?

    Query

    Update: this becomes unreliable in Postgres 11 or later when Parallel Append is used for big sets! Consider this question and answers (incl. a reliable alternative in my answer):

    • Are results from UNION ALL clauses always appended in order?

    This should be the fastest possible query for your case:

    SELECT * FROM image WHERE name = 'name105' AND group_id = 10
    UNION ALL
    SELECT * FROM image WHERE name = 'name105'
    UNION ALL
    SELECT * FROM image WHERE group_id = 10
    LIMIT  1;
    

    fiddle
    Old sqlfiddle

    LIKE without wildcard character is equivalent to =

    The LIMIT clause applies to the whole query. Postgres is smart enough not to execute later legs of the UNION ALL as soon as it has found enough rows to satisfy the LIMIT. Consequently, for a match in the first SELECT of the query, the output of EXPLAIN ANALYZE looks like this (scroll to the right!):

    Limit  (cost=0.00..0.86 rows=1 width=40) (actual time=0.045..0.046 rows=1 loops=1)
      Buffers: local hit=4
      ->  Result  (cost=0.00..866.59 rows=1002 width=40) (actual time=0.042..0.042 rows=1 loops=1)
            Buffers: local hit=4
            ->  Append  (cost=0.00..866.59 rows=1002 width=40) (actual time=0.039..0.039 rows=1 loops=1)
                  Buffers: local hit=4
                  ->  Index Scan using image_name_grp_idx on image  (cost=0.00..3.76 rows=2 width=40) (actual time=0.035..0.035 rows=1 loops=1)
                        Index Cond: ((name = 'name105'::text) AND (group_id = 10))
                        Buffers: local hit=4
                  ->  Index Scan using image_name_grp_idx on image  (cost=0.00..406.36 rows=500 width=40) (never executed)
                        Index Cond: (name = 'name105'::text)
                  ->  Index Scan using image_grp_idx on image  (cost=0.00..406.36 rows=500 width=40) (never executed)
                        Index Cond: (group_id = 10)
    Total runtime: 0.087 ms

    Bold emphasis mine.

    Do not add an outer ORDER BY clause, this would void the effect. Then Postgres would have to consider all rows before returning the top row.

    Final questions

    Is there a generic solution for that?

    This is the generic solution. Add as many SELECT statements as you want.

    Of course it would come in handy when the search result is sorted by its relevance.

    There is only one row in the result with LIMIT 1. Kind of voids sorting.

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

Sidebar

Related Questions

I want to search two item (name=string and location=json). this search is (one input
I want to search a text in a table without knowing its attributes. Example
I want to search with grep for a string that looks like this: something
I want to implement something like this using JSF.(part of search screen) More and
I have a timestamp and I want to search for a single date but
I want to search a series of files based on the files name. Here
I want to search a user from LDAP and after getting the user I
I want to let the user select one row to use to submit the
I want to retrieve all the address locations (latitude,longitude,format address) for a single 'search
I have written a code that searches specific string from database table.what i want

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.