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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:43:40+00:00 2026-06-11T07:43:40+00:00

I feel very puzzled about the state, I found querying a table with a

  • 0

I feel very puzzled about the state, I found querying a table with
a where condition of a none index column with “limit 1” is very
fastT,though the column have no index。 The following is a example:

–1 create test table with 20000000 data

francs=> create table test_limit (id int4,name varchar(32));
CREATE TABLE

francs=> insert into test_limit select generate_series(1,20000000),generate_series(1,20000000) || 'a';
INSERT 0 20000000

francs=> \d test_limit;
         Table "francs.test_limit"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | 
 name   | character varying(32) | 

–2 query table

francs=> explain analyze select * from test_limit where id=1;
                                                 QUERY PLAN                                                  
-------------------------------------------------------------------------------------------------------------
 Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.028..3162.477 rows=1 loops=1)
   Filter: (id = 1)
 Total runtime: 3162.531 ms
(3 rows) 

Notice it takes about 3162 ms whihc is very slow as I expect。

–3 query table with “limit 1 ” cause

francs=> explain analyze select * from test_limit where id=1 limit 1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.019..0.019 rows=1 loops=1)
   ->  Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.017..0.017 rows=1 loops=1)
         Filter: (id = 1)
 Total runtime: 0.047 ms
(4 rows)

Notice it takes only about 0.047 ms ms,it is so fast, but the column id have no index。Any body can explain it ?
thanks a lot!

–4 addtion test

francs=> explain analyze select * from test_limit where id=2 limit 1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.023..0.023 rows=1 loops=1)
   ->  Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.022..0.022 rows=1 loops=1)
         Filter: (id = 2)
 Total runtime: 0.066 ms
(4 rows)

francs=> explain analyze select * from test_limit where id=3 limit 1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.022..0.022 rows=1 loops=1)
   ->  Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.021..0.021 rows=1 loops=1)
         Filter: (id = 3)
 Total runtime: 0.060 ms
(4 rows)

francs=> explain analyze select * from test_limit where id=101 limit 1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.035..0.036 rows=1 loops=1)
   ->  Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.033..0.033 rows=1 loops=1)
         Filter: (id = 101)
 Total runtime: 0.075 ms
(4 rows)

francs=> explain analyze select * from test_limit where id=1001 limit 1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.192..0.192 rows=1 loops=1)
   ->  Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=0.190..0.190 rows=1 loops=1)
         Filter: (id = 1001)
 Total runtime: 0.231 ms
(4 rows)

From the addtion test, we can see it’s also very fast.

–5 final test

francs=> explain analyze select * from test_limit where id=9999999 limit 1;
                                                      QUERY PLAN                                                      
----------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=1379.153..1379.154 rows=1 loops=1)
   ->  Seq Scan on test_limit  (cost=0.00..358111.05 rows=1 width=13) (actual time=1379.151..1379.151 rows=1 loops=1)
         Filter: (id = 9999999)
 Total runtime: 1379.206 ms
(4 rows)

From the above ,I use a later id which is 9999999 , it’s slow now; I understand now,thanks!

  • 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-11T07:43:42+00:00Added an answer on June 11, 2026 at 7:43 am

    Maybe “id=1” is very early in the table, so when it reads the table sequentially it will hit that row very quickly, and since you said “limit=1” it can just stop after the first result.

    Alternatively, there could be some caching involved too.

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

Sidebar

Related Questions

I feel very noobish asking this, but I can't seem to figure it out...
I feel very very very silly posting this, but I can't find the answer.
I feel I am very close, to my regex -9.99 through 0. I have
I am trying to create what I feel is a very simple form submission
I feel bad because it is very likely this has already been answered and
I feel this is not a very good question to post on SO, but
I feel as if this should be very simple, but it's behaving strangely. I
I feel like this is not a very efficient way of using linq. I
This is a very simple question and I feel stupid for asking it, but
The below MSSQL2005 query is very slow. I feel like their ought to be

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.