Is there anyway to optimize the following query:
SELECT
t1.id,
(SELECT SUM(col1) FROM table_name_two t2 WHERE t2.name LIKE CONCAT('%',t1.name)) AS col1_count
FROM
table_name_one t1
ORDER BY
col1_count DESC
Using ORDER BY col1_count DESC takes a long time.
Thanks.
Just make a normal join with your comparison in the join’s
onclause:It should be way faster this way – it’s basically one query instead of “n” queries, although it won’t get any help from indexes using the
LIKEoperator with a leading%