MySQL query is very slow (15 minutes)


I was previously using SQLite for a personal project and due to a constraint of having it available online, I decided to make the switch to MySQL. I converted my database to the MySQL equivalent but I just noticed that performance is VERY poor. This is a 70 mb database with around 600k records total. The query I am running is an INNER JOIN that executes in less than 500 ms using SQLite but the same query using MySQL takes 15 minutes.

SELECT has.tag_id, has.image_id FROM has INNER JOIN image ON image.image_id = has.image_id INNER JOIN person ON person.person_id = image.person_id WHERE person.name="Random Person" 
  • has table has 80k records
  • image table has 290k records
  • person table has 500 records

Here is the structure of the three tables:

create table media.person (     person_id int auto_increment         primary key,     name         text not null )     collate = utf8_unicode_ci;  create table media.image (     id           int auto_increment,     image_id     int  not null,     person_id int  not null,     link         text not null,     checksum     text null,     constraint id         unique (id) )     collate = utf8_unicode_ci;  alter table media.image     add primary key (id);  create table media.has (     id       int auto_increment         primary key,     tag_id   int not null,     image_id int not null )     collate = utf8_unicode_ci; 

Note that I added a primary key to the has table because I suspected it might have been the source of the problem, but it isn’t and SQLite was doing fine without that primary key.

The database uses the InnoDB engine. Here is the output of the mysql --version command:

mysql Ver 14.14 Distrib 5.7.30 

Where could the problem come from? I can understand a small loss of performance because MySQL is heavier than SQLite but certainly not to the point of going from 500 ms to 15 minutes for such a simple query.