1

Consider this test setup:

CREATE TABLE dept (deptid integer PRIMARY KEY, deptname TEXT);
CREATE INDEX dept_name_idx on dept(deptname);

The dept table contains 1000 rows and the deptname column contains 10 unique values that are evenly distributed.

Which of the following two sample queries would use the index dept_deptname_idx?

1) SELECT deptid from dept where deptname ='SAPA';

2) SELECT deptid from dept where deptname <>'SAPA';

1
  • So do you have your answer? Commented Mar 14, 2018 at 0:09

1 Answer 1

2

With only 10 distinct values, evenly distributed, chances are that neither query will use the index. A sequential scan of the table is typically faster than involving any indexes when retrieving more than roughly 5 % of all rows. Exact numbers depend on many details.

Also, 1000 small rows like in your example fit on a hand full of data pages. A sequential scan is hard to beat with such a small table.

With a much bigger table and/or substantially more distinct values in deptname, query 1 would be a candidate for using the index, but not query 2 (which retrieves most rows and will always use a sequential scan).

To optimize read performance for query 1 you could then use a multicolumn index on (deptname, deptid) - if preconditions for index-only scans are met.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.