Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to create a nonclustered index on a column of type varchar?

Tags:

sql-server

I have a table in which I have created a clustered index in one of the column of type INT. I wanted to create a nonclustered index to increase the performance of the query. However, I do not have any column of type int.

I therefore thought of creating a nonclustered index on a varchar type column.

Will it be good to create an index on a column of type Varchar? Will it help improving the performance of a query? I know it is not a good thing to create an index on a varchar type column, but just wanted to know if it will improve query performance.

like image 940
Running Rabbit Avatar asked Dec 26 '12 13:12

Running Rabbit


People also ask

Can we create index on varchar data type?

1) You can also create indexes on multiple fields combining int and varchar types, taking special care for filtering always on first field defined in the index. If you just filter using the second field index will not be used.

Can we create clustered index on varchar column?

Answers. Yes, you can create a clustered index on non-unique nullable varchar column.

Does nonclustered index improve performance?

It contains only a subset of the columns. It also contains a row locator looking back to the table's rows, or to the clustered index's key. Because of its smaller size (subset of columns), a non-clustered index can fit more rows in an index page, therefore resulting to an improved I/O performance.

When should we use non-clustered index?

A non-clustered index is also used to speed up search operations. Unlike a clustered index, a non-clustered index doesn't physically define the order in which records are inserted into a table. In fact, a non-clustered index is stored in a separate location from the data table.


1 Answers

There is nothing whatever wrong with creating an index on a VARCHAR column, or set of columns.

Regarding the performance of VARCHAR/INT, as with everything in a RDBMS, it depends on what you are doing. What you may be thinking of is the fact that clustering a table on a VARCHAR key is (in SQL Server) marginally less efficient than clustering on a monotonically increasing numerical key, and can introduce fragmentation.

Or you may be thinking of what you have heard about writing JOINs on VARCHAR columns - it is true, it is a little less efficient than a JOIN on numeric type, but it is only a little less efficient, nothing that would lead you to never join on varchar cols.

None of this does not mean that you should not create indexes on VARCHAR columns. A needed index on a VARCHAR column will boost query performance, often by orders of magnitude. If you need an index on a VARCHAR, create it. It makes no sense to try to find an integer column to create the index on - the engine will never use it.

like image 114
DeanGC Avatar answered Nov 02 '22 05:11

DeanGC