Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data URI vs. Binary in Database

I'm posting an image to the server using the DATA URI (client side, using the canvas). I now have two choices: I can save the "image" as a string in a varchar(max) column or I can convert it to byte[] and save it as varbinary(max).

As far as implementation, the effort is the same. I am trying to determine what would be more efficient as far as 1: space in the DB and 2: displaying the image. Has anyone seen any analysis on this or have a good way to measure this?

AN FYI - a 3kb image is about 100K characters in the DB.

Using: ASP.NET 4.5, MVC, SQL Server 2008

To Clarify

I can either store the image in the database using byte[] in a varbinary(MAX) column like is typically the case or I can store the DATA URI from the HTML5 canvas that looks like data:image/png;base64,iVBORw0K... in a varchar(max) column.

Storing the byte[] is typical and needs no further explanation. Storing the DATA URI is just a string and displaying the image would be a matter of:

<img src="data:image/png;base64,iVBORw0K" /> or
<img src="@Model.Uri" />

My question is which one is more performant and space saving and if there's any documentation, white paper or analysis around this specific comparison.

like image 604
Tony Basallo Avatar asked Feb 21 '13 03:02

Tony Basallo


People also ask

What type of data is URI?

A Data URI (Uniform Resource Identifier) is a scheme that allows data to be encoded into a string, and then embedded directly into HTML or CSS. The more commonly known URL (Uniform Resource Locator) is a subset of Data URI that specifically identifies a resource's location, such as the IP address of a website.

What is the use of data URI?

What Is a Data URI? A data URI is a base64 encoded string that represents a file. Getting the contents of a file as a string means that you can directly embed the data within your HTML or CSS code. When the browser encounters a data URI in your code, it's able to decode the data and construct the original file.

How big can a data URI be?

1 Support is limited to images and linked resources like CSS files, not HTML or JS files. Max URI length is 32KB.

What is data URI of image?

A Data URL is a URI scheme that provides a way to inline data in a document, and it's commonly used to embed images in HTML and CSS.


1 Answers

With no real answer and little information found Binging with Google, I did a simple timed test inserting 20K records (less than that was pointless) and Selecting one record at a time in a loop. I used PetaPoco for DB access. If you find something out there or have some info, please share. I think this will be a more common scenario with DATA URI getting more attention.

URI was consistently faster inserting and selecting. Faster being relative because it's measured in milliseconds. This shouldn't be a factor - it's whatever is easier.

As far as rendering to the client. I used two methods, an ImageResult (custom ActionResult that returns an image) from an MVC action method (this is rendering an image in the http response) and returning the URI string and using it as the image SRC (i.e., src="@Model.Uri"). Again, barely a difference. Results using Chrome dev tools:

ImageResult: 2 requests, 200ms, 3.2KB
DATA URI: 2 requests, 200ms, 3.9KB

However, I did notice that the ImageResult (byte[]) version will automatically get cached by the browser because it's an image for all intents and purposes. The DATA URI version does not cache by the browser automatically.

From this rudimentary testing, byte[] is the way to go because of the automatic caching by the browser and all other results being equal.

My Set Up: i7, 8GB Ram, SSD, SQL Server 2012, IIS Express

Caching benefit was something I noticed with no setup. Yes, I'm sure one could manage the HTTP headers, etag, output caching, etc.

like image 78
Tony Basallo Avatar answered Sep 23 '22 17:09

Tony Basallo