Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it faster to search by column integer or column string in mysql?

Tags:

mysql

I have a table "transactions" with million records

id        trx   secret_string (varchar(50))        secret_id (int(2.))
1         80    52987624f7cb03c61d403b7c68502fb0   1
2         28    52987624f7cb03c61d403b7c68502fb0   1
3         55    8502fb052987624f61d403b7c67cb03c   2
4         61    52987624f7cb03c61d403b7c68502fb0   1
5         39    8502fb052987624f61d403b7c67cb03c   2
..
999997    27    8502fb052987624f61d403b7c67cb03c   2
999998    94    8502fb052987624f61d403b7c67cb03c   2
999999    40    52987624f7cb03c61d403b7c68502fb0   1
1000000   35    8502fb052987624f61d403b7c67cb03c   2

As you can notice, secret_string and secret_id will always match.

Let's say, I need to select records where secret_string = "52987624f7cb03c61d403b7c68502fb0".

Is it faster to do:

SELECT id FROM transactions WHERE secret_id = 1

Than:

SELECT id FROM transactions WHERE secret_string = "52987624f7cb03c61d403b7c68502fb0"

Or it does not matter? What about for other operations such as SUM(trx), COUNT(trx), AVG(trx), etc?

Column secret_id currently does not exist, but if it is faster to search records by it, I am planning to create it upon row insertions.

Thank you

I hope I make sense.

like image 271
karlosuccess Avatar asked Nov 08 '22 23:11

karlosuccess


1 Answers

Int comparisons are faster than varchar comparisons, for the simple fact that ints take up much less space than varchars.

This holds true both for unindexed and indexed access. The fastest way to go is an indexed int column.

There is another reason to use an int, and that is to normalise the database. Instead of having the text '52987624f7cb03c61d403b7c68502fb0' stored thousands of times in the table,you should store it's id and have the secret string stored once in a separate table. It's the same deal for other operations such as SUM COUNT AVG.

like image 170
Leo Avatar answered Nov 14 '22 21:11

Leo