I’ve got an older table where some IDs are stored in a varchar as comma separated list.
name street some_ids
Pete Teststr 3,4,7,2
Tom Teststr 1,4,7,3
Inga Teststr 3,7,2,5
I.e. need to find the entries containing 2 in column "some_ids" I tried:
WHERE 2 IN (some_ids) = 0 results
WHERE 3 IN (some_ids) = only rows where some_ids starts with 3 = 2 results
Another possibility is to use find_in_set() but there are lots of rows in the table and I want to use the index which isn’t by find_in_set().
Any ideas?
2
Answers
I think you could try to search with
LIKE
, and to simplify the searching you can add first,
at the begining and end of thesome_ids
column.Here’s a solution to make a multi-valued array on the comma-separated string, after forming it into JSON array syntax. The EXPLAIN shows that the query will use the index, if we search the exact same expression.
The multi-valued index feature requires MySQL 8.0.17 or later. If you’re still using MySQL 5.x, and you want to optimize a search with an index, then you must normalize your table, and not store comma-separated lists.