SQL table partitioning

Using table partitioning, developers can split up large tables into smaller, manageable pieces. A database’s performance and scalability can be improved when users only have access to the data they need, not the whole table.

Whenever a table gets too big for efficient management, horizontal partitioning divides it into smaller tables. Each table has a subset of the original table’s rows.

For example, a customer order table might be populated with millions of rows, each representing one order, on an e-commerce website. If a table isn’t partitioned, it can be difficult and time consuming to perform queries on the data, especially if it needs to be scanned in full.

In order to solve this problem, a horizontal partition by date can be used. By doing so, it will not have to scan through the entire table to find the data it needs, but will only be able to search for relevant partitions. A partition would contain orders from a particular period, like a month or year.

A database can improve query performance while reducing the amount of data it has to search by searching only relevant partitions for the data it needs. By partitioning, you can access only the relevant partition instead of scanning a large table and doing a lot of unnecessary IO. As a result, we don’t have as many read operations, the disk isn’t overloaded, and the database has more time to handle other requests.

There’s more to partitioning than just reading operations — writing operations also benefit from partitions, especially if there’s an index on a table. The bigger the table, the bigger the indexes. Each write operation to the table requires updating the indexes on the table. Because indexes are updated in real time and not as a background process, inserting data into a big table without partitions will take a lot longer.

It’s also important to think ahead, about the future, since we’ve already talked about writing. If you have a database that’s not static but dynamic, and it gets a lot of write operations every day, as time passes, it’ll get bigger and bigger until the central table becomes hard to manage. By letting the database handle more data, the partition makes it simpler and easier to expand. If you have a date-partitioned database, you can add new partitions as needed to store data from future time periods.

There is also a significance to the size of your partitions in this case. Make sure that the partitions are not too large, otherwise the solution will not help and the problem will persist. For example, if you were to divide by date, you could divide by year, month, day, hour, etc… So how do you do it? There are two key parameters to consider: the first is — what is the nature of your application, what is the style of your queries? Are they daily? Monthly? Annuals? And the second parameter is the amount of data that the partition will contain so that it is not too large and difficult to manage.

Several organizations I found used the Hebridean solution for partition size, too. For the “hot” information, like future dates and dates from the near past, they implement small partitions, like daily partitions. For “cold” information, they move it to bigger partitions, like monthly or annual partitions.

What are the advantages of partitioning over indexing?

I used to interview database managers by asking them:

There is a very large table, and the queries addressed to it are focused on a certain date. For example, an event table, which contains a date (and time) column, the application is querying for data by date. (where date_time = ‘XXXXX’)

To improve query performance, which solution should be implemented? Dividing the table into partitions or building an index on the date column?

The correct answer is both, let me explain why.

The partition-only solution won’t help us get good query performance because what size partitions will we make? A partition for each second or every minute doesn’t make sense. The partition of an hour will have a lot of data, so every query will have to scan the whole partition, and you’ll have to do a lot of unnecessary I/O operations.

Index-only might find the record faster and more accurately, but a large index can sometimes result in more I/O per read compared to using table partitioning. This is because a large index can have many levels, which means that the database may need to perform more I/O in order to navigate through the index and locate the desired data.

The best way to achieve our goal is with a hybrid solution. Partitions and an index.

It is possible to discuss partitions in more detail, such as their advantages from a maintenance perspective. Which commands allow us to manage our information more effectively and efficiently.

Also, another interesting point about partitions is actually related to what I’m working on today, query caching. Correctly constructing a schema with partitions will allow you to make use of query caching more efficiently.

Next time, we’ll talk about that.

Contact us at: info@rapydo.io
Visit our site at: rapydo.io
Follow us on LinkedIn

More from the blog

Block queries from running on your database

As an engineer, you want to make sure that your database is running smoothly, with no unexpected outages or lags in response-time. One of the best ways to do this is to make sure that only the queries you expect to run are being executed.

Keep reading

Uncover the power of database log analysis

Logs.They’re not exactly the most exciting things to deal with, and it’s easy to just ignore them and hope for the best. But here’s the thing: logs are actually super useful and can save you a ton of headaches in the long run.

Keep reading