I have a table with columns of type SET
eg SET('abc','def','ghi')
wich store the data like "abc,ghi"
and I index these columns. So when I want to find "def"
or "ghi"
I have to use LIKE "%def%"
but I read about "%"
that if you use it as first character mysql doesn't use index for search. Waht should I do? should I change the type to enum and store each value in separate row with an ID like this:
+---------+
| column |
+---------+
| abc |
| abc,ghi |
| abc,def |
| ghi,def |
+---------+
change to:
+----+--------+
| ID | column |
+----+--------+
| 1 | abc |
| 2 | abc |
| 2 | ghi |
| 3 | abc |
| 3 | def |
| 4 | ghi |
| 4 | def |
+-------------+
or is there any thing to manipulate index to store each word separately?
The correct function to find an item in a set is FIND_IN_SET
. Sets are stored as bit maps, not as strings, and FIND_IN_SET
will not have to convert it to a string before matching like LIKE
would. But it still won't be able to use an index.
Your second schema is the proper way to normalize the data. You can put an index on the column
column, and queries that look for a value will be efficient. Whether to use an ENUM
or VARCHAR
for this column is a subject of intense debate within the database community.
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.