Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index a varchar column

I asked Google but I am still confused.

1) Is there a problem in indexing a Varchar column. When I shouldn't, and When I should

2) Index a char column VS Varchar column.

Thanks

like image 538
Costa Avatar asked Nov 03 '10 15:11

Costa


2 Answers

1 - Index it if you are querying it and it is selective enough. If it is a column where 90% of the values are the same, there won't be much point.

2 - This is not a question, but I will guess you want to know if you should. Yes, if you query it and it meets the criteria above.

like image 165
JNK Avatar answered Oct 14 '22 08:10

JNK


  • ad 1) Yes, 900 bytes limit, huge keys, lots of index pages, lots of I/O involved, inefficient index operations. Conclusion: DON'T unless your varchar is about 50 chars max.
  • ad 2) Same as 1. The real difference between char vs. varchar are fixed size vs. variable size (ie. char(100)) always takes 100 bytes in data page, varchar(100) takes up to 100)
like image 39
Pavel Urbančík Avatar answered Oct 14 '22 07:10

Pavel Urbančík