Int id vs varchar id

What type of identifier is better to request and use as a foreign key in mysql:

1) Varchar ID with size 20

2) Int id

+5
source share
3 answers

Usually int is better because it gives better performance.

If your element has a natural key , which is varchar, you can use it and it will work. But you will get a performance boost (and some other benefits) by adding a surrogate key , which is an integer. You can add a column AUTO_INCREMENTfor this purpose.

+8
source

INT identifiers are always preferred.

It is faster and uses less storage in the database.

, VARCHAR, INT

0

Int IDSs are more efficient because the database will only compare once.

So let's say you have lines n. If you use int, you look at O(n)how the worst case is. If you use varchar(20), you lookO(n*20)

0
source

All Articles