A
A
Anatoly2020-06-18 14:14:36
MySQL
Anatoly, 2020-06-18 14:14:36

How to execute mysql json_contains to find out if an array value is in the range of another?

Hello,

Given an array of data type json in MySQL, and there is a column with arrays to compare them through json_contains.

Everything would be fine and it would be possible to manage with one command, if not for one nuance:
In the first array, the value of one of the keys is an integer. And in the second group of arrays, we are dealing with a range formatted through {min_value: n ; max_value: n }.
For all other keys, everything is unambiguous. But what to do with it?

Of course, there are alternatives like putting the boundaries of the range into separate columns and first checking for compliance with them through operators, and then doing json_contains already without this problematic key in the array. But, it seems to me, 2 commands will always be executed longer.

How can you still compare arrays for occurrence with one command? Or am I bothering in vain, and taking the range out of the array and matching through 2 commands will not significantly affect the speed of the request?

If you look at the question from the other side, is it really better for one team? If there are multiple indexed columns, and the search performed on them filters out a good part of the column's data, then the subsequent json command may run faster.

However, this is a very small table. Initially, let's say it has 500 lines. With an "intermediate" search, 150-300 lines will remain. But you can also use multi-valued indexes per group of keys at once (MySQL 8).

Be that as it may, it is possible to choose which option will be faster in the event that the existence of all options is possible at all.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question