Suppose we have following table
+-----+------------+
| id | categories |
+-----+------------+
| id1 | [20,25] |
| id2 | [25] |
| id3 | [20,25,28] |
| id4 | [28,25] |
| id5 | [20,25] |
+-----+------------+
Field categories
is JSON type. It contains only known and limited list of integers - like 20,25,28.
So, I need somehow count all inclusions of all these values in a manner like this:
+-------+--------+
| count | number |
+-------+--------+
| 20 | 3 |
| 25 | 5 |
| 28 | 2 |
+-------+--------+
The main issue is to make this using single request without looping through categories numbers in server code or in procedure call
Head-on decision is the following
SELECT
COUNT(id) AS 'count', '20' AS number
FROM
ml_categories
WHERE
JSON_CONTAINS(categories, '20')
UNION SELECT
COUNT(id) AS 'count', '25' AS number
FROM
ml_categories
WHERE
JSON_CONTAINS(categories, '25')
UNION SELECT
COUNT(id) AS 'count', '28' AS number
FROM
ml_categories
WHERE
JSON_CONTAINS(categories, '28')
But this solution has O(n) complexity and code itself isn't good enough. For example, looping through ~500K records takes about 1 sec for one category on my hardware, thus counting 10 categories takes about 10 seconds. Not good. Is there a way to optimize such query?
Thx in advance, guys