简体   繁体   中英

Is there a way to index on multiple values in a column in MySQL

Currently I have a column in my table which has a set of comma separated values. I am currently using it to filter the results. I am wondering if it would be possible to index on it and query directly using it.

My table is as below:

userId        types
123           A, B, C
234           B, C

If I want to query a user which has types A and C, should get 123 If with B and C then 123, 234

EDIT: I am aware the problem can be solved by normalization. However my table is actually storing json and this field is a virtual column referencing a list. there are no relations used anywhere. We are facing an issue where querying by types was not considered and is now causing performance impact

First of all, you should normalize your table and remove the CSV data. Use something like this:

userId | types
123    | A
123    | B
123    | C
234    | B
234    | C

For the specific query you have in mind, you might choose:

SELECT userId
FROM yourTable
WHERE types IN ('A', 'C')
GROUP BY userId
HAVING MIN(types) <> MAX(types);

With this in mind, MySQL might be able to use the following composite index:

CREATE INDEX idx ON yourTable (userId, types);

This index should cover the entire query above actually.

The answer is basically no . . . but not really. The important point is that you should restructure the data and store it in a properly. And "properly" means that string columns are not used to store multiple values.

However, that is not your question. You can create an index to do what you want. Such an index would be a full-text index, allowing you to use match() . If you take this approach you need to be very careful:

  • You need to use boolean mode when querying.
  • You need to set the minimum word length so single characters are recognized as words.
  • You need to check the stop-words list, so words such as "A" and "I" are included.

So, what you want to do is possible. However, it is not recommended because the data in not in a proper relational format.

MySQL supports Multi-Value Indexes for JSON columns as of MySQL 8.0.17. It seems like exactly your case.

Details: https://dev.mysql.com/doc/refman/8.0/en/create-index.html#create-index-multi-valued

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM