Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force a query to not use a index on a given table?

I'm currently doing some testing to determine the performance implications of including an index on a given column in SQL Server 2005.

The test data set I'm using has approximately ~72 million rows (about 6 GB of data). In order to actually test the performance of the index I need to be able to compare the performance with and without the index there.

That's all well and fine, but creating an index in the first place is not a cheap operation. If I want to test the table without the index, I need to, at the very least, disable the index. To test with the index I need to re-enable it which takes quite a long time.

Is there any way that I can force SQL Server 2005 to ignore a given index when it's executing a query? I don't want to have to disable the index just to test a query since it takes such a long time to disable the index.

like image 919
Mike Bailey Avatar asked Jun 13 '12 14:06

Mike Bailey


People also ask

Can you force the database to use an index on a query?

In case the query optimizer ignores the index, you can use the FORCE INDEX hint to instruct it to use the index instead. In this syntax, you put the FORCE INDEX clause after the FROM clause followed by a list of named indexes that the query optimizer must use.

How do I know if a given query uses an index or not?

Write "explain " in front of your query. The result will tell you which indexes might be used.

How do I turn off index?

As a job seeker, you can close your account by signing in to your Account page. Click the Close My Account link. You will see a confirmation window. If you are certain that you would like to proceed, click on Close my account.


2 Answers

SELECT * FROM MyTable WITH (INDEX(0)) WHERE MyIndexedColumn = 0 

Query would normally use the index on MyIndexedColumn, but due to the table hint, it will instead tablescan.


SELECT * FROM MyTable WITH (INDEX(IndexName)) WHERE MyIndexedColumn = 0 

Query would normally use the index on MyIndexedColumn, but due to the table hint, it will instead use the index named IndexName.

like image 148
Amy B Avatar answered Sep 22 '22 06:09

Amy B


I'm working with all different kinds of DBs and can never remember the specific hint when I need it. Therefore I'm using a pure SQL approach that (currently) works with all the DBs that cross my way.

The idea is just to make it impossible for the DB to use the specific index by obfuscating the respective expression in the SQL. E.g. when a where clause makes the database believe it's best resolved using an index, but it isn't.

SELECT * FROM MyTable WHERE MyIndexedColumn + 0 = 0 

Similarly, you can add an empty string to a string value. Current optimizers do no resolve such expressions can cannot use an index on (MyIndexedColumn).

This is actually an anti-pattern I described in my book. Here are some on page about math in SQL

It's definitively good enough for ad-hoc testing. In production code, hints are more expressive of course!

like image 40
Markus Winand Avatar answered Sep 23 '22 06:09

Markus Winand