Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a blob in an sqlite table is not null, without reading the blob entirely?

Tags:

sql

sqlite

blob

I am trying to query a table that has a blob column, and need to filter the results to only provide rows that have content (any content) in the blob column.

However, doing SELECT column_name FROM table_name WHERE blob_column IS NOT NULL takes a long time, which I assume is due to the fact that some of the blobs are quite heavy. It seems that the WHERE clause is reading the entire content of the blobs and comparing them to null.

Is there a way to test whether a blob column is null or not, without having sqlite read the entire contents of the blob?

like image 826
waldyrious Avatar asked Oct 16 '25 07:10

waldyrious


1 Answers

Since version 3.7.12, using the length() or typeof() functions avoids reading the binary data (the query is much faster). So to avoid the unnecessary overhead, one must change

SELECT column_name FROM table_name WHERE blob_column IS NOT NULL

to

SELECT column_name FROM table_name WHERE LENGTH(blob_column) IS NOT NULL

or

SELECT column_name FROM table_name WHERE TYPEOF(blob_column) != 'null'
like image 111
waldyrious Avatar answered Oct 17 '25 20:10

waldyrious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!