Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Meaning of: "If you're wanting to store a number and you store it as a string, it actually takes up more space on disk."

Hi all, I'm watching Andrew's DB video series, and he mentions: "If you're wanting to store a number and you store it as a string, it actually takes up more space on disk." Can anyone explain why this is in a simple/beginner level way? I'm also curious if mixing up different data types (like storing a VARCHAR as a DATETIME) also takes up more space on the disk, and how big of a problem is this memory misuse?

2 Answers

Steven Parker
Steven Parker
231,261 Points

Different data types take up different amounts of storage. For example, a 10-digit number less than about 4.3 billion can be stored in 32 bits, or 4 bytes of memory. This is because all 32 bits are used to represent the number in binary. But if this same name number were stored as a string, each digit would be represented by a code that uses an entire byte, plus there may be another byte of overhead, so that's 11 bytes.

The exact space taken up by each data type varies a bit depending on which type of database, and would be described in that database's technical specifications. These concerns are not significant except for very large amounts of data, and the optimization of storage and procedures is a very advanced topic area.

There's a bunch of web pages with info on this. One I've found interesting for MySQL is http://www.developwebsites.net/choose-optimal-mysql-data-type/.

This is for MySQL, and I believe the Workspaces are SQLite, but I doubt it's much different. If I read it correctly INTs require 4 bytes, and VARCHARs require 1 byte per character + 1 byte. So 123 would require 4 bytes as an INT, 4 bytes as a VARCHAR, but 1234567 would require 4 bytes as an INT, 8 bytes as a VARCHAR, or twice as much. So as the numbers get larger the difference gets worse. Of course, here we are talking about databases. I've looked at some sites that talk about the memory requirements of objects in java, and again, if reading correctly, String objects are quite a bit bigger that ints.