I currently have a stored procedure that accepts TEXT as input, and then tries to find the matching ids within the set.
The input_ids value is an array of longs ‘1,2,3,…’.
It works but the problem is that find_in_set is slow, and I think there must be a better way.
CREATE PROCEDURE `sel_network_edges_by_id`(
IN input_ids TEXT
)
BEGIN
SELECT id, gradient, geoJson, ...
FROM network.network_edges
WHERE FIND_IN_SET(id, input_ids);
END
Table Sample
id, gradient, geoJson
1, -50, [34.34....]
2, 110, [35.3345..]
3, 110, [35.3345..]
2
Answers
You could use a temporary table and join it with the main table with this process:
Parsing Input String:
Convert the comma-separated input string into a temporary table that holds the IDs as separate rows. This can be done using a string splitting function:
Now you can use this function to split the input_ids and insert them into a temporary table:
Query Using Join:
Once you have the IDs in the temporary table, you can perform a more efficient query using a join:
Although this solution is more efficient than
FIND_IN_SET
, it does involve more steps. However, it should perform better, especially for larger datasets, as it avoids string manipulations and allows for optimized indexing. Temporary tables have session scope, so they will be automatically dropped when the session ends.Since
FIND_IN_SET
can’t use an index you should use a prepared statement withIN()
, where the values of the passed string can be used as an array insideIN()
by concatenation.: