Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql medium int vs. int performance

I have a simple users table, I guess the maximum users I am going to have is 300,000.

Currently I am using:

 CREATE TABLE users
 (
         id INT UNSIGNED AUTOINCREMENT PRIMARY KEY,
         ....

Of course I have many other tables for which users(id) is a FOREIGN KEY.

I read that since the id is not going to use the full maximum of INT it is better to use: MEDIUMINT and it will give better performance.

Is it true?

(I am using mysql on Windows Server 2008)

like image 961
aviv Avatar asked May 22 '10 09:05

aviv


People also ask

What is medium INT in MySQL?

MEDIUMINT is a medium-sized integer. The minimum and maximum values for MEDIUMINT SIGNED are -8388608 and 8388607 respectively. The range for minimum and maximum UNSIGNED values equals 0 and 16777215 respectively. The size is 3 bytes per row.

Why use TINYINT in MySQL?

TINYINT can be used to store value of 8 bits. The maximum value we can store is 127. We cannot store, for example 987 with 8 bit value. If we try to insert 987 with TINYINT data type, MySQL raises an error.

What means INT 11 in MySQL?

In MySQL integer int(11) has size is 4 bytes which equals 32 bit. Signed value is : - 2^(32-1) to 0 to 2^(32-1)-1 = -2147483648 to 0 to 2147483647. Unsigned values is : 0 to 2^32-1 = 0 to 4294967295.


2 Answers

I would consider using MEDIUMINT sometimes.. on 300K rows.. MEDIUMINT gives u enough room up to 16M rows (unsigned).

It is not only about "smaller table size" when u use indexes.. the difference can be huge on a 27M rows tables.. changing 2 columns from INT to MEDIUMINT saved me 1GB (indexes + table data) so it went from 2.5GB to 1.5 GB.

like image 177
shion Avatar answered Sep 23 '22 15:09

shion


That is called micro-optimization and not an issue.
Try to ask (yourself in the first place) performance questions based only on the real experience, not imagination. And profiling is always for help to distinguish one from another.

As for the "300k max" - in the real life numbers tend to grow unexpectedly. Why to dig a pitfall for yourself?

like image 45
Your Common Sense Avatar answered Sep 23 '22 15:09

Your Common Sense