Why is PostgreSQL not using my indexes on a small table?

后端 未结 2 1731
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 03:22

I have the following table in PostgreSQL:

CREATE TABLE index_test
(
    id int PRIMARY KEY NOT NULL,
    text varchar(2048) NOT NULL,
    last_modified times         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 03:52

    Maybe, the reason is that I have too few rows in my table?

    Yes. For a total of 20 rows in a table a seq scan is always going to be faster than an index scan. Chances are that those rows are located in a single database block anyway, so the seq scan would only need a single I/O operation.

    If you use

    explain (analyze true, verbose true, buffers true) select ....
    

    you can see a bit more details about what is really going on.

    Btw: you shouldn't use text as a column name, as that is also a datatype in Postgres (and thus a reserved word).

提交回复
热议问题