Sample Table
CREATE TABLE
`foo` (
`id` INT NOT NULL AUTO_INCREMENT ,
`keyword_ids` VARCHAR(128) ,
PRIMARY KEY (`id`)
);
Sample Data
INSERT INTO
`foo`
SET
`keyword_ids` = '14,10,5,19,12'
Sample Query
SELECT
*
FROM
`foo`
WHERE
(`keyword_ids` LIKE '5,%%'
OR
`keyword_ids` LIKE '%%,5'
OR
`keyword_ids` = '5'
OR
`keyword_ids` LIKE '%%,5,%%')
As per my most recent question, this works just fine but is there a way I can improve it?
Note, however, that this expression is still not sargable, that means an index on
keyword_idscannot improve this query.You should normalize your model if you want this to be searchable fast.
Another option is to store comma-separated keywords (rather than
ids) in the table and create aFULLTEXTindex on them:This, however, would only work on a
MyISAMtable.