Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do database schemas often contain 32, 64, 128, etc

I understand that 2, 4, 8, 16, 32, 64, 128, 256... are the decimal equivalents of binary digits.

Is there a reason why these are used in databases? For example, VARCHAR fields are often 255 characters long. Since (I'm assuming) each character is one byte, why is there a difference between using 255 characters and using 257 characters?

like image 372
Leo Jiang Avatar asked Feb 23 '12 02:02

Leo Jiang


People also ask

Why do we need schema in database?

Access and security: Database schema design helps organize data into separate entities, making it easier to share a single schema within another database.

What is a schema in a database?

A database schema represents the logical configuration of all or part of a relational database. It can exist both as a visual representation and as a set of formulas known as integrity constraints that govern a database. These formulas are expressed in a data definition language, such as SQL.

What is a schema how many schemas can be used in one database?

How many schemas can be used in one database? In the SQL environment, a schema is a group of database objects such as tables and indexes that are related to each other. The schema belongs to a single user or application. A single database can hold multiple schemas that belong to different users or applications.

What is database page size?

DB pages can be between 512 bytes and 64K bytes in size. The size that you select must be a power of 2. You set your database's page size using DB->set_pagesize() . Note that a database's page size can only be selected at database creation time.


2 Answers

With varchar columns, the length is stored with the data using unsigned integers in the leading bytes of the data. The fewest number of bytes is used; one byte can store lengths from 0 to 255, two bytes from 0 to 65535, etc. By making the length 255, you get the "most value" out of the minimum one length byte.

In days gone by, single bytes of disk saved per row were worth saving. Although now disk is cheap, the thinking has remained, especially by grey-haired DBAs.

There is no advantage in choosing a length that is a power of 2, for example varchar(64) - it is merely a habit/convention (I even follow it - and I don't know why!).

like image 53
Bohemian Avatar answered Oct 11 '22 06:10

Bohemian


Not merely database schemas but pretty much any programming artifact will by found to contain many numbers of the form 2^N or 2^N-1. While some of these uses make sense (e.g. 2^32-1 being the largest number representable as a standard unsigned integer in many machine architectures), most uses of powers of 2 are less necessary. In practice, old hackers view powers of 2 as holy, and venerate them such.

like image 42
Dave Griffith Avatar answered Oct 11 '22 06:10

Dave Griffith