简体   繁体   中英

select longtext column too slow in mysql

I have a table like below,

CREATE TABLE `floor` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `floorName` varchar(20) NOT NULL,
  `buildingId` bigint(11) NOT NULL,
  `imageInString` longtext,
  `image` longtext,
  PRIMARY KEY (`id`),
  KEY `fk_floor_1_idx` (`buildingId`),
  CONSTRAINT `fk_floor_1` FOREIGN KEY (`buildingId`) REFERENCES `building` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;

table contains 20 rows. I am inserting images(SVG/JPEG) as base64 in the image column.

when I select different size of inserted column it takes,

4KB -> Loaded less than 1 sec

200KB -> 3 to 4 secs

500KB -> 8 secs

3MB   -> 30 to 50 secs

Here is my dealing, I want to load the 3MB file in 1 to 2 secs.

How can achieve this?

I'm not sure it will be a good idea to store images in MySQL - it's not built to store files, or large files for that matter. To avoid this pattern, I would recommend to store the images on a web server / in the cloud and just store the url / file name in the database, for the application's use.

If you would like to store the images in the database anyway, I would consider the following:

  1. Try different data types - what happens if you change the column type to BLOB and save binary information?
  2. Check for network connectivity - it's easier to say than do. Therefore, I would start with running your query locally on the MySQL server and not from a different server. That way, you can avoid the network lag and see which part takes longer, transferring the image over the network or uploading it to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM