Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql varchar unique column varchar(255) vs. varchar(50)

Tags:

php

mysql

I will always be entering things of 20 characters into a particular column in my table.

I need this column to be unique.

Will there be any speed difference in SELECT queries if I set this column to be a varchar(255) instead of varchar(20)?

(data entered will always be 20 characters)

like image 512
David19801 Avatar asked Dec 22 '22 05:12

David19801


2 Answers

if data entered will always be 20 characters than why not consider using char(20). using varchar(20) will use 20 bytes for storing character and 1 byte for storing length. so if there are 1 million records, 1 million bytes will be wasted.

as far as speed is concerned between varchar(20) and varchar(255), then I dont think it might be very hard to pick one of them, both of them will be using 21 bytes, I dont see any significant performance benefit or loss of one over other.

like image 67
Zohaib Avatar answered Jan 06 '23 07:01

Zohaib


If it can only be 20, why would you want to specify 255? If it is always 20, even char(20) would be better.

like image 37
Cylindric Avatar answered Jan 06 '23 05:01

Cylindric