views:

95

answers:

4

When performing a query where the attributes selected make up the components of an index does that result in a faster query? I would imagine that the query planner/optimizer could see that the requested columns could be satisfied completely by the index scan.

Trivial Example

CREATE TABLE "liked" (
  "id" BIGINT NOT NULL DEFAULT nextval('liked_id_seq'),
  "userid" BIGINT NOT NULL,
  "storyid" BIGINT NOT NULL,
  "notes" TEXT,
  PRIMARY KEY ("id")
);
CREATE INDEX "liked_user" ON "liked" (
  "userid",
  "storyid"
);
ALTER TABLE "liked" ADD FOREIGN KEY ("userid") REFERENCES "users" ("id") ON DELETE CASCADE;
ALTER TABLE "liked" ADD FOREIGN KEY ("storyid") REFERENCES "story" ("id") ON DELETE CASCADE;


SELECT storyid from liked where userid = 1;

With the query above there isn't any data external to what is already contained in the liked_user index so I would imagine there would be less actions if the query optimizer could infer that the resulting tuples could be satisfied by the index alone.

+1  A: 

In general, yes, but it depends on how you are accessing them. Using LIKE to key off a match in the middle out of a string field isn't going to be any faster with an index.

MarkusQ
It could be, if the optimizer supports full index scans, and if it estimates that it is more beneficial to fully scan the index and then probe the table.
David Aldridge
+1  A: 

Not so much, in my experience. You speed up queries by optimizing their conditions, and trying to use the best possible index in those conditions. There are many ways to slow down a query based on what you are selecting, such as using subqueries, perhaps some UDFs--and of course you can slow down queries using some less-than-optimal joins.

Peter
+3  A: 

It's called a "covering index", and it improves the efficiency somewhat, by varying amounts depending on which DBMS you are using (and if you are using MySQL, which flavor of storage).

Try giving an example of a specific situation, if you have one.

le dorfier
+1  A: 

It can do, but there are some caveats. These comments are based on Oracle, btw.

For example, SELECT COL1 FROM MY_TABLE might be able to use an index, but if all the columns of the index are nullable then there might be rows not included in a regular btree index, so the index might not be used.

It's also possible that an index might be larger (and therefore more costly to ful scan) than the underlying table (for example where the table only has a single column) because the index has to include a rowid for every entry as well as the column values. In that case, unless the query can leverage the index information in some special way (for example you are including an ORDER BY clause that the index can supply without the need for a sort) then the index might not be used.

You ought to also look into the various index access methods that the RDBMS can use in order to understand their strengths and weaknesses. In Oracle these would generally be INDEX RANGE SCAN, FULL INDEX SCAN, FAST FULL INDEX SCAN, and INDEX SKIP SCAN. This knowledge will help you understand whether an index could be used and in what way.

David Aldridge