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

2

18

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

Silurian answered 8/9, 2014 at 10:51 Comment(8)
How would you store a byte[] in a database without using a BLOB (Binary Large OBject)?Statvolt
BLOB (Binary Large OBject)`. It's SQL's way of storing byte arrays.Suspensory
#6662932 - Better use a BLOB - byte array . ByteArrayInputStrean - ImageIO.read. That should not pose any problem. (Though I prefer my files on the file system.)Feedback
I want to know whether there is difference between byte and binarySilurian
A byte is eight bits of binary. In this case they are effectively the same.Paraguay
Please share the code you have so far to store/load the image.Furcula
Have you tried to measure time of selecting/inserting images stored in different formats?Fevre
Storing images in a relational database is a bad idea. File storage is a much better solution and store the paths/pointers to the images in SQL.Obcordate
G
15

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.

Guimar answered 26/6, 2019 at 18:25 Comment(0)
O
0

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

hope this is simple and it answers your question.

Oleta answered 10/1, 2021 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.