Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.)

Let’s face it, building a database that works is not a challenge at all. The real challenge is to build a database that is fast, reliable, scalable, and well-engineered. In this article, let me show you some of the most important tips when building re…


This content originally appeared on DEV Community and was authored by Mohmed Ishak

mohmed ishak
Let's face it, building a database that works is not a challenge at all. The real challenge is to build a database that is fast, reliable, scalable, and well-engineered. In this article, let me show you some of the most important tips when building relational databases like MySQL, PostgreSQL, etc.

[1] Index Your Database

Consider a database table (customers) with the following header/columns:
| id | state | points |

// 10 million rows go here...

Here's how to query the database to get customers in 'NYC':

SELECT id
FROM customers
WHERE state = 'NYC';

The query will be slow because we've got 10 million rows and the DB engine needs to query all rows to get the relevant data. With indexing we can eliminate all irrelevant rows before querying the data so only rows with 'NYC' will be checked when 'NYC' is queried. Here's the same query with index:

CREATE INDEX idx_state ON customers (state);
-- convention is to prefix index with "idx" followed by underscore and then column name. 
SELECT id
FROM customers
WHERE state = 'NYC';

This query will be so much faster because only rows with 'NYC' will be queried, not all rows, thanks to indexing. Think of indexing as educating the database engine on which data is where and how it is stored so that when you query data next time, it gives the relevant data more efficiently without wasting resources/time.

I can talk so much about indexing but I don't want to bore you. If you want to learn more about indexing, Google these: "multiple-column indexing", "indexing and sort operation", "composite indexes", etc.

[2] Think in Present

You need to only store that data that you need or the business needs. Don't try to solve a problem which might or might not happen in future. If you need to store additional data, you can always create a new table and add relationship(s).

[3] Say No To Blobs

Blob stands for Binary Large Object and this basically means images, audios, and other large files converted to binary format. If I could impose a rule in the tech world, I'd forbid blob as a data in database tables, because it's incredibly bad to do so. Blobs increase the size of database like crazy and this impacts the performance.

[4] Find The Sweet Spot Between Normalizing / Denormalizing Data

If a table has way too much columns, it never means the table is well-engineered. If that's the case, consider splitting your table to two or more tables. In contrast, if there are a whole lot of tables with very few columns (say 1 to 2) and they're connected with relationships, consider combining them (and be okay with duplicating some data). A lot of beginners think that denormalizing data is something bad because it has the prefix "de" but I can assure you it's not true, lol.

[5] Select The Perfect Data Type

Just because a data type works well with your use case, it doesn't mean it's the best one. For example, to store age of users, use TINYINT data type (an unsigned TINYINT stores values from 0 to 255) instead of INT. INT data type stores up to 2 billion (or something like that). This takes more space and we don't need it at all. The age of users will never go beyond 255. If anyone does reach 254 years old, we still have 1 year to change the data type.

[6] Don't Use Relational Databases, They're Not Scalable

Just kidding, or am I? Just kidding... or am I?


This content originally appeared on DEV Community and was authored by Mohmed Ishak


Print Share Comment Cite Upload Translate Updates
APA

Mohmed Ishak | Sciencx (2021-06-16T21:03:47+00:00) Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.). Retrieved from https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/

MLA
" » Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.)." Mohmed Ishak | Sciencx - Wednesday June 16, 2021, https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/
HARVARD
Mohmed Ishak | Sciencx Wednesday June 16, 2021 » Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.)., viewed ,<https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/>
VANCOUVER
Mohmed Ishak | Sciencx - » Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/
CHICAGO
" » Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.)." Mohmed Ishak | Sciencx - Accessed . https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/
IEEE
" » Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.)." Mohmed Ishak | Sciencx [Online]. Available: https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/. [Accessed: ]
rf:citation
» Build Relational Databases Like A Pro (MySQL, PostgreSQL, etc.) | Mohmed Ishak | Sciencx | https://www.scien.cx/2021/06/16/build-relational-databases-like-a-pro-mysql-postgresql-etc/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.