Hi I have a simple example query below that is not what I am using in reality as it is far too complex to explain here. Basically this query is being used in a search in a web application.
Due to the amount of data that this query could pull out I am trying to avoid the use of HAVING or subqueries as it drastically increases the amount of time it takes to retrieve the data.
I want to use FIND_IN_SET in the where clause but obviously GROUP_CONCAT can't be used in the where clause so I am trying to something simple like the below but I don't think the variable is set in the where and no results are returned.
SELECT
v.message,
@cat_values:= GROUP_CONCAT(c.category_value) as cat_values
FROM
#Where the audit message is stored
AUDIT_ITEMS AS V
#Link table containing primary key of AUDIT_ITEMS and AUDIT_CATEGORIES
LEFT JOIN AUDIT_ITEM_CATEGORIES AS ic
ON V.UUID = ic.ITEM_UUID
#Gets category uuid from link table above
LEFT JOIN AUDIT_CATEGORIES AS c
ON ic.CATEGORY_UUID = c.UUID
#Gets the category label/type
LEFT JOIN AUDIT_CATEGORY_TYPES AS ct
ON c.CATEGORY_TYPE_UUID = ct.UUID
WHERE TRUE
#example filter used below
AND FIND_IN_SET("Errored", @cat_values) > 0
...etc...
I have tried:
AND FIND_IN_SET("Errored", (SELECT GROUP_CONCAT(c.category_value))) > 0
But it does seem slow.
Also tried:
HAVING FIND_IN_SET("Errored", GROUP_CONCAT(c.category_value)) > 0
But there must be a way of efficiently filtering results based on the values in the group_concat?
You are looking for a conditional in the having
clause:
having sum(c.category_value = 'Errored') > 0
You don't need group_concat()
for this part of the logic.
It is unclear why you think that having
would have a performance penalty. The performance hit is in the aggregation step -- which you need when you use group_concat()
in the select
.
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.