Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me in understanding, when to create index on varchar,when do we use it?what is the difference between int and varchar index?Is it best practise or not

What I have tried:

Same as above. [edit]Afzaal Ahmad Zeeshan[/edit]
Posted
Updated 26-Nov-16 8:54am
v2

Hi,

You can create index on varchar, of course, especially on fields that are supposed are going to be filtered frequently in your application. But the most important think is to choose correcty the fields for creating the index. You should take the most selective possible fields. If not, indexes might not be used, it will depend on "statistics", but the probability will be low. For example, you can make use of indexes if your queries are similar to:

select * from Table where ColumnIndexedName = 'Value'
select * from Table where ColumnIndexedName like 'Value%'

But, index will not be used if you use:

select * from Table where ColumnIndexedName like '%Value'

I would say that the only difference between using int or varchar columns to build indexes is the fact that the latter will take up less space to store values, and perhaps it might make the query faster. The same to create primary keys...


To conclude:

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.

2) Try to avoid use of "*" operator. I mean, use only fields you need in your final result set. If all columns can be returned from the index the query will be "covered" and this will be great for the perfonmance. So, consider including new "aditional columns" to store in the index if possible according to your application requirements.


Regards!
 
Share this answer
 
Comments
Member 12424653 26-Nov-16 15:08pm    
but the varchar may have chances of duplicate values in the column if it has statements more then 2 strings like "is" or "are",and index should be unique values. how can we avoid them?
jamuro77 26-Nov-16 16:14pm    
Indexes can be "unique" or "not unique". If you want to have unique values in one field (or field combinations), you have to create a "unique" index on those fields. In addition to this, you can also create "unique" restrictions on columns.
Best practice is to create indexes on fields or field combinations you frequently use as conditions in your WHERE clause. The selectivity of these conditions should be high compared to the overall amount of rows in the table.

The decision to index or not isn't based on the data type but the type of the condition. As already mentioned the selectivity is one of the key thing, but other considerations are the use of the field in the condition. As pointed out in Solution one, certain wildcard searches cannot use indexes, or actually it doesn't make sense to use it. The similar situation is if you use functions or do calculations. These kinds of comparisons may not use indexes that are created directly on the fields.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900