Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
seq scan instead of id index?
Author Summary
Jonathan_Frias is in Idaho
Post Body

I have a very large table in postgreql 9.6 that has a composite primary key. Recently, I added an id column, back-filled the ids and added a unique index on it. But when I do basic lookups it does a sequential scan instead of using the index.

explain select * from table where id = 1000;

output:

Seq Scan on table  (cost=0.00..17897134.60 rows=1 width=1074)
  Filter: (id = 1000)

Here's the schema

-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS table_id_seq;

-- Table Definition
CREATE TABLE "public"."table" (
    "co_no" int4 NOT NULL,
    "parcel_id" varchar(26) NOT NULL,
    "asmnt_yr" int4 NOT NULL,
    -- rest of columns are omitted
    "id" int4 NOT NULL DEFAULT nextval('table_id_seq'::regclass),
    PRIMARY KEY ("co_no","asmnt_yr","parcel_id")
);

CREATE UNIQUE INDEX "table_index_id" ON "public"."table" USING BTREE ("id");

CREATE UNIQUE INDEX "table_index_primary" ON "public"."table" USING BTREE 
("co_no","asmnt_yr","parcel_id");

Any idea as to why it won't use the existing index? Does it have to do with using such an old PG version?

EDIT Adding the constraint fixes this issue. Thanks /u/pehrs

Author
Account Strength
90%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,138
Link Karma
107
Comment Karma
4,031
Profile updated: 2 days ago
Posts updated: 7 months ago

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago