Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SELECT is very slow because LONGBLOB

why or how solve problem on mysql.

table xxx
-> id primary key
-> name varchar 255
-> data longblob

when I store to this table 100 files, each 100MB, the table would have 10GB

and then try to select any row... it takes to long

SELECT name FROM xxx WHERE id = 50 LIMIT 1;

takes about 8 second

my problem is probably in that, mysql reads whole row before it return name, which is only 255 characters ... so when I want to list names of 100 files, mysql reads 10 GB and return about 2 KB of result.

like image 581
user5332 Avatar asked Jan 16 '12 12:01

user5332


People also ask

Why is my MySQL query running slow?

Queries can become slow for various reasons ranging from improper index usage to bugs in the storage engine itself. However, in most cases, queries become slow because developers or MySQL database administrators neglect to monitor them and keep an eye on their performance.

What is Longblob in MySQL?

LONGBLOB: A binary large object column with a maximum length of 4294967295 (2^32 - 1) bytes, or 4GB in storage. Each LONGBLOB value is stored using a four-byte length prefix that indicates the number of bytes in the value.

What is the maximum size of BLOB in MySQL?

BLOB: Can handle up to 65,535 bytes of data. MEDIUMBLOB: The maximum length supported is 16,777,215 bytes. LONGBLOB: Stores up to 4,294,967,295 bytes of data.


1 Answers

Try splitting the blobs into a separate table.

For example, you could have a table xxx containing the columns id and name, and another table xxx_data containing the columns id and data. If you only want the name, you don't need to query the xxx_data table at all; if you need both the name and the data, you can join the tables together using the shared primary key id:

SELECT id, name, data
FROM xxx JOIN xxx_data USING (id)
WHERE id = ...

For more information and optimization ideas, see e.g. 10 tips for optimizing MySQL queries.

like image 60
Ilmari Karonen Avatar answered Oct 19 '22 07:10

Ilmari Karonen