Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between storing images in byte array and binary (BLOB) and which one is faster

I want to insert and select images from sql server in jdbc. I am confused whether BLOB and byte are the same thing or different. I have used Blob in my code and the application loads slow as it has to select the images stored in Blob and convert it pixel by pixel. I want to use byte array but I don't know whether they are same or different. My main aim is to load the image faster. Thank you

like image 392
viper Avatar asked Sep 08 '14 10:09

viper


People also ask

Is Blob same as byte array?

Blob simply means (Binary Large Object) and its the way database stores byte array.

Is it better to store images in database or filesystem?

Generally databases are best for data and the file system is best for files. It depends what you're planning to do with the image though. If you're storing images for a web page then it's best to store them as a file on the server. The web server will very quickly find an image file and send it to a visitor.

Is blob and binary same?

BLOB DefinitionBLOB stands for a “Binary Large Object,” a data type that stores binary data. Binary Large Objects (BLOBs) can be complex files like images or videos, unlike other data strings that only store letters and numbers.

Is Bytearray a binary?

Byte arrays mostly contain binary data such as an image. If the byte array that you are trying to convert to String contains binary data, then none of the text encodings (UTF_8 etc.) will work.


1 Answers

Before going further, we may need to remember about basic concepts about bit, byte and binary, BLOB.

Bit: Abbreviation of binary digit. It is the smallest storage unit. Bits can take values of 0 or 1.

Byte: Second smallest storage which is commonly (nibble is not mentioned since it is not very common term) used. It includes eight bits.

Binary: Actually, it is a numbering scheme that each digit of a number can take a value of 0 or 1.

BLOB: Set of binary data stored in a database. Also, type of a column which stores binary data inside.

To sum up definitions: Binary format is a scheme that which include bits.

To make it more concrete, we can observe results with the code below.

import java.nio.ByteBuffer;

public class TestByteAndBinary{

     public static void main(String []args){
        String s = "test"; //a string, series of chars
        System.out.println(s);

        System.out.println();

        byte[] bytes = s.getBytes(); //since each char has a size of 1 byte, we will have an array which has 4 elements
        for(byte b : bytes){
            System.out.println(b);
        } 

        System.out.println();

        for(byte b : bytes){
            String c = String.format("%8s", Integer.toBinaryString(b)).replace(' ', '0'); //each element is printed in its binary format
            System.out.println(c);
        }
     }
}

Output:

$javac TestByteAndBinary.java
$java -Xmx128M -Xms16M TestByteAndBinary
test

116
101
115
116

01110100
01100101
01110011
01110100

Let's go back to the question: If you really want to store an image inside a database, you have to use the BLOB type.

BUT! It is not the best practice.

  • Because databases are designed to store data and filesystems are designed to store the files.

  • Reading image from disk is a simple thing. But reading an image from the database need more time to accomplished (querying data, transforming to an array and vice versa).

  • While an image is being read, it will cause the database to suffer from lower performance since it is not simple textual or numerical read.

  • An image file doesn't benefit from characteristical features of a database (like indexing)

At this point, it is best practice to store that image on a server and store its path on the database.

As far as I can see on enterprise level projects, images are very very rarely stored inside the database. And it is the situation that those images were needed to store encrypted since they were including very sensual data. According to my humble opinion, even in that situation, those data had not to be stored in a database.

like image 79
mahmutoflaz Avatar answered Oct 08 '22 08:10

mahmutoflaz