Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the database performance improvement from storing as numbers rather than text?

Suppose I have text such as "Win", "Lose", "Incomplete", "Forfeit" etc. I can directly store the text in the database. Instead if use numbers such as 0 = Win, 1 = Lose etc would I get a material improvement in database performance? Specifically on queries where the field is part of my WHERE clause

like image 236
deltanovember Avatar asked Dec 29 '22 01:12

deltanovember


2 Answers

At the CPU level, comparing two fixed-size integers takes just one instruction, whereas comparing variable-length strings usually involves looping through each character. So for a very large dataset there should be a significant performance gain with using integers.

Moreover, a fixed-size integer will generally take less space and can allow the database engine to perform faster algorithms based on random seeking.

Most database systems however have an enum type which is meant for cases like yours - in the query you can compare the field value against a fixed set of literals while it is internally stored as an integer.

like image 103
Blagovest Buyukliev Avatar answered Dec 31 '22 10:12

Blagovest Buyukliev


There might be significant performance gains if the column is used in an index.

like image 41
drewrobb Avatar answered Dec 31 '22 10:12

drewrobb