

Increase Database Performance By Using Multiple Columns
source link: https://downing.tech/blog/increase-database-performance-by-using-multiple-columns
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Increase Database Performance By Using Multiple Columns
Last updated 07/11/2021By thinking outside of the box, you can often get much faster database performance by splitting values across multiple database columns
My Friend Sam Rowden and I were recently pairing on a project of his for a client, and came across an interesting requirement: serial numbers. The client wanted serial numbers to have the following format: [type]-[subtype]-[5 digit incrementing integer]. So, for example, "C-VB-00001", "C-VB-00002". The incrementing integers were to be unique the type, so there may well be "C-VB-00001" and "A-BC-00001" in the database at the same time.
Evidently, these serial numbers need to be generated programatically before inserting into the database. The most obvious solution here is a unique string column called serial_number
on the table. On further reflection, however, there is a key problem with this approach: How do you retrieve the latest serial number on a per-type basis?
We could use some advanced SQL functions to achieve this, but as the table grows, this will have a detremental impact on performance. The fact of the matter is that we're going to need to get the latest entry every time we generate new serial numbers to work out the next number in the sequence, so this needs to be a fast query.
There is perhaps a much simpler solution that gives us all of the necessary power and speed.
Using multiple database columns for a single value
There are really two parts to these serial numbers: a type/subtype string and an incrementing integer. So, why don't we split the serial number into two parts in our database too?
We can have a string column serial_number_type
and an int column serial_number_value
. These two columns then form a unique index to prevent duplicate entries. Here's what that schema looks like:
1$table->string('serial_number_type');
2$table->unsignedInteger('serial_number_value');
3$table->unique(['serial_number_type', 'serial_number_value']);
With that done, we need to combine these two columns programatically in our Eloquent Model. The best way to do this is using a custom Eloquent cast. Let's generate one using Artisan:
1php artisan make:cast AsSerialNumber
The implementation of that cast might look something like this:
1class AsSerialNumber implements CastsAttributes
4 public function get($model, string $key, $value, array $attributes): string
6 return Str::of($attributes['serial_number_value'])
7 ->padLeft(5, '0')
8 ->start('-')
9 ->prepend($attributes['serial_number_type'])
10 ->__toString();
14 public function set($model, string $key, $value, array $attributes): array
16 return [
17 'serial_number_type' => Str::beforeLast($value, '-'),
18 'serial_number_value' => (int) Str::afterLast($value, '-'),
Now, we can apply this cast to our model in the $casts
array to a generated column called serial_number
, like so:
1class OurModel extends Model
4 protected $casts = [
5 'serial_number' => AsSerialNumber::class,
8 // The rest of your model...
Anytime our model is retrieved from the database, it will now cast our serial_number_type
and serial_number_value
columns to a single attribute, serial_number
, which is a complete representation of our serial number. Let's take a quick look at how our string manipulation works here:
1Str::of($attributes['serial_number_value']) // 1, 2, 3 etc.
2 ->padLeft(5, '0') // If the number is less than 5 digits, fill to the left with '0'
3 ->prepend("{$attributes['serial_number_type']}-") // Add the type to the start
4 ->__toString(); // Convert to a native PHP string
Casting back to the database is just a matter of splitting the serial number by the last '-' character. With this out of the way, we can now retrieve the latest serial number for a type using a very simple Eloquent query:
1$latestSerialNumber = OurModel::select(['serial_number_type', 'serial_number_value'])
2 ->where('serial_number_type', $type)
3 ->latest('serial_number_value')
4 ->first();
Incrementing the serial number is as simple as incrementing the returned serial_number_value
by one.
Conclusion
So, the next time you're dealing with a situation like this in your applications, keep in mind that it may be easier to just split that value in your database and combine programatically!
Hopefully, you picked up some useful tips here. Be sure to follow me on Twitter for more!
Kind Regards, Luke
Recommend
-
58
In this article we will discuss different ways to select rows in DataFrame based on condition on single or multiple columns. First let’s create a DataFrame, # List of Tuples students = [ ('jack', 'Appl...
-
13
You can use the SQL UPDATE statement to update multiple columns. The trick is to specify the columns you wish to update in the SET clause. All the examples f...
-
10
How InnoDB Handles TEXT/BLOB Columns Back to the Blog
-
8
Home > Python > Python error – Length of passed values is 6, index implies 2 (while doing PIVOT with MultiIndex or multiple...
-
4
In SQL, how and when would you do a group by with multiple columns? Also provide an example. In SQL, the group by statement is used along with aggregate functions like SUM, AVG, MAX, etc. Using the group by statement with mu...
-
14
Photo by Alex Block This is a follow up to my previous post about sorting large files by column us...
-
6
Learn SQL (11 Part Series) Sometimes we need to join the data from multiple columns or string values to get the desired result. Without...
-
3
Convert multiple list items to separate the data.frame columns advertisements I am trying to convert a list to data.frame f...
-
8
Vishnu Sindhe 1 hour ago if we increase the card size then no of bar chart columns also in increasing in OVP 13...
-
4
Performance Implications Of Using Calculated Columns In Composite Models On Power BI Semantic Models I don’t have anything against the use of calculated columns in Power BI semantic models in general b...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK