I’m learning about the usage of datatypes for databases.
For example:
- Which is better for email? varchar[100], char[100], or tinyint (joking)
- Which is better for username? should I use int, bigint, or varchar?
Explain. Some of my friends say that if we use int, bigint, or another numeric datatype it will be better (facebook does it). Like u=123400023 refers to user 123400023, rather then user=thenameoftheuser. Since numbers take less time to fetch. - Which is better for phone numbers? Posts (like in blogs or announcments)? Or maybe dates (I use datetime for that)? maybe some have make research that would like to share.
- Product price (I use decimal(11,2), don’t know about you guys)?
- Or anything else that you have in mind, like, "I use serial datatype for blablabla".
Why do I mention innodb specifically?
Unless you are using the InnoDB table
types (see Chapter 11, "Advanced
MySQL," for more information), CHAR
columns are faster to access than
VARCHAR.
Inno db has some diffrence that I don’t know.
I read that from here.
Brief Summary:
(just my opinions)
VARCHAR(255)VARCHAR(100)orVARCHAR(255)INT(unless you plan on over 2 billion users in you system)INTorVARCHARor maybeCHAR(depends on if you want to store formatting)TEXTDATEorDATETIME(definitely include times for things like posts or emails)DECIMAL(11,2)As far as using InnoDB because
VARCHARis supposed to be faster, I wouldn’t worry about that, or speed in general. Use InnoDB because you need to do transactions and/or you want to use foreign key constraints (FK) for data integrity. Also, InnoDB uses row level locking whereas MyISAM only uses table level locking. Therefore, InnoDB can handle higher levels of concurrency better than MyISAM. Use MyISAM to use full-text indexes and for somewhat less overhead.More importantly for speed than the engine type: put indexes on the columns that you need to search on quickly. Always put indexes on your ID/PK columns, such as the id_username that I mentioned.
More details:
Here’s a bunch of questions about MySQL datatypes and database design (warning, more than you asked for):
What DataType should I pick?
Table design question
Enum datatype versus table of data in MySQL?
mysql datatype for telephne number and address
Best mysql datatype for grams, milligrams, micrograms and kilojoule
MySQL 5-star rating datatype?
And a couple questions on when to use the InnoDB engine:
MyISAM versus InnoDB
When should you choose to use InnoDB in MySQL?
I just use
tinyintfor almost everything (seriously).Edit – How to store “posts:”
Below are some links with more details, but here’s the short version. For storing “posts,” you need room for a long text string.
CHARmax length is 255, so that’s not an option, and of courseCHARwould waste unused characters versusVARCHAR, which is variable lengthCHAR.Prior to MySQL 5.0.3,
VARCHARmax length was 255, so you’d be left withTEXT. However, in newer versions of MySQL, you can useVARCHARorTEXT. The choice comes down to preference, but there are a couple differences.VARCHARandTEXTmax length is now both 65,535, but you can set you own max onVARCHAR. Let’s say you think your posts will only need to be 2000 max, you can setVARCHAR(2000). If you every run into the limit, you canALTERyou table later and bump it toVARCHAR(3000). On the other hand,TEXTactually stores its data in aBLOB(1). I’ve heard that there may be performance differences betweenVARCHARandTEXT, but I haven’t seen any proof, so you may want to look into that more, but you can always change that minor detail in the future.More importantly, searching this “post” column using a Full-Text Index instead of
LIKEwould be much faster (2). However, you have to use the MyISAM engine to use full-text index because InnoDB doesn’t support it. In a MySQL database, you can have a heterogeneous mix of engines for each table, so you would just need to make your “posts” table use MyISAM. However, if you absolutely need “posts” to use InnoDB (for transactions), then set up a trigger to update the MyISAM copy of your “posts” table and use the MyISAM copy for all your full-text searches.See bottom for some useful quotes.
MySQL Data Type Chart (outdated)
MySQL Datatypes (outdated)
Chapter 10. Data Types (better details)
The BLOB and TEXT Types (1)
11.9. Full-Text Search Functions (2)
10.4.1. The CHAR and VARCHAR Types (3)
Lastly, here’s a great post about the pros and cons of VARCHAR versus TEXT. It also speaks to the performance issue: