Why compress strings before insert to the database

Sercan KARA
1 min readMar 3, 2023

--

Photo by Wes Huynh on Unsplash

Hi everyone,
I had a new issue. I had to add long JSON data input to the database. But this in later times turns back into a big storage problem. Then I asked myself, why do I add the JSON without compressing it?

You can see the sample sizes.

gzcompress output:

xڋ�RP�b�p%+%��T%����7P(̃�����:��u���~��H:PuAmq Y㔚Z���*�����5��30@���!l�[~~
آ��6�@�'�hF�h0��� EkJ��5�u���%�� ��qJ-��B�l`� �ӣ���G��x}IDq�/=z%楖�#A��)}��Ӓ�����f>L�[���v~�~Z#�B�����

base64_encode(gzcompress()):

eNqL5lJQqAZiBQWlcA9/JSsFJa/8VCUdqIirqzdQKBrMg6kDy/iF+jq5BgHljHUQgq4RAa5+wa5IOlB1QW1xDAFZ45SaWqSkgyrn6Osf6geSNbTQMzBAkqvVIWygW35+Cm4DjahtoCXpBjon4vaxkQGaeXB2LBeaDdiiwYQ20UCGJ/EbaEbtaDAfBAbi97IJRRFrSpuINaF1/sL0JReMB7YMtcxxSi3JGPhCx2xgsiCd06PpwKRHqhfPeH1JRHGKLz16JealltAjQeKrEsgpffGHsgW105LFwOQYvKFmPkzKW7yepHZ+oX5aI6VC4IrlAgAOj9SF

Samples

FoodMenu Sample

<?php
$sampleData = file_get_contents('https://sercankara.xyz/samples/foodMenu.json');
$withBase64encode = base64_encode(gzcompress($sampleData, 9));
$withOutBase64encode = gzcompress($sampleData, 9);
echo 'pure data: ';
echo mb_strlen($sampleData, '8bit');
echo "\n";
echo 'with base64_encode: ';
echo mb_strlen($withBase64encode, '8bit');
echo "\n";
echo 'without base64_encode: ';
echo mb_strlen($withOutBase64encode, '8bit');

Output

pure data: 2696
with base64_encode: 328
without base64_encode: 246

Photos Sample

<?php
$sampleData = file_get_contents('https://sercankara.xyz/samples/photos.json');
$withBase64encode = base64_encode(gzcompress($sampleData, 9));
$withOutBase64encode = gzcompress($sampleData, 9);
echo 'pure data: ';
echo mb_strlen($sampleData, '8bit');
echo "\n";
echo 'with base64_encode: ';
echo mb_strlen($withBase64encode, '8bit');
echo "\n";
echo 'without base64_encode: ';
echo mb_strlen($withOutBase64encode, '8bit');

Output

pure data: 951473
with base64_encode: 170940
without base64_encode: 128205

If you don’t want to see ����� these characters. you have to use base64_encode. But you lose a few spaces.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response