Existing data was indexed, but as advised by Pyrce, a query with LIKE %[token]%
is unable to leverage a fulltext index.
Rewrite your condition like this, this strictly equivalent to your inital query (ignoring stopwords and the likes):
WHERE MATCH(tags) AGAINST ('tag1 tag2 tag3 tag4' IN BOOLEAN MODE)
However you should rather focus on normalizing your structure. Storing non-scalar values (such as coma-separated values) in a field violates the very first normal form.
Create a new image_tag
table and establish a 1-N relationship with images
. A regular index will allow instant querying.
CREATE TABLE image_tags (
image_id INT,
tag VARCHAR(50),
PRIMARY KEY (image_id, tag),
FOREIGN KEY (image_id) REFERENCES images(id), -- replace with "images" table's actual primary key
KEY(tag, image_id) -- this index may be superfluous, check EXPLAIN
);
SELECT images.*
FROM images
JOIN image_tags ON image_id = images.id
WHERE tag IN ('tag1', 'tag2', 'tag3', 'tag4');
Repeat with images.category
.