I am developing a web application that has a listbox with thousands of records from a MySQL database ('Table1').
- I have a quick search field that searches all fields with the OR
operator (value: aaaa).
- I have a dialog box that allows filtering by all fields with the AND
operator (values: xxx, yyy, zzz).
The idea is to create a query that contains both, the filtered values and the search value.
I have created a subquery on a FROM clause like this:
SELECT b.*
FROM
( SELECT *
FROM Table1
WHERE Field1 LIKE '%xxx%'
AND Field2 LIKE '%yyy%'
AND Field3 LIKE '%zzz%'
) b
WHERE b.Field1 LIKE '%aaaa%'
OR b.Field2 LIKE '%aaaa%'
OR b.Field3 LIKE '%aaaa%'
After running the subquery it seems to me that the performance is not optimal.
Would it be possible to improve the query in some way to optimize the performance (and lower the response time)?
Thank you very much.
Wardiam
Update:
It seems to me that it would be more correct to use a Common Table Expression (CTE). I have used this CTE expression:
WITH CTE_Expression AS
(SELECT *
FROM Table1
WHERE Field1 LIKE '%xxx%'
AND Field2 LIKE '%yyy%'
AND Field3 LIKE '%zzz%'
)
SELECT b.*
FROM CTE_Expression b
WHERE b.Field1 LIKE '%aaaa%'
OR b.Field2 LIKE '%aaaa%'
OR b.Field3 LIKE '%aaaa%'
What do you think?.
question from:
https://stackoverflow.com/questions/65865513/optimising-a-subquery-on-a-from-clause-in-mysql 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…