简体   繁体   English

优化 MySQL 中 FROM 子句的子查询

[英]Optimising a subquery on a FROM clause in MySQL

I am developing a web application that has a listbox with thousands of records from a MySQL database ('Table1').我正在开发一个 web 应用程序,该应用程序有一个列表框,其中包含来自 MySQL 数据库('Table1')的数千条记录。

  • I have a quick search field that searches all fields with the OR operator (value: aaaa).我有一个快速搜索字段,可以使用 OR 运算符(值:aaaa)搜索所有字段。
  • I have a dialog box that allows filtering by all fields with the AND operator (values: xxx, yyy, zzz).我有一个对话框,允许使用 AND 运算符(值: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:我在 FROM 子句上创建了一个子查询,如下所示:

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).在我看来,使用公用表表达式 (CTE) 会更正确。 I have used this CTE expression:我使用了这个 CTE 表达式:

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?.你怎么看?。

Found a similar issue:发现了一个类似的问题:

Mysql Improve Search Performance with wildcards (%%) Mysql 使用通配符提高搜索性能 (%%)

No, because MySQL will not be able to utilize the index when you have a leading wildcard.不,因为当您有一个前导通配符时,MySQL 将无法使用该索引。 If you changed your LIKE to 'aaaa%', then it would be able to use the index.如果您将 LIKE 更改为 'aaaa%',那么它将能够使用索引。

If you want to check if indices are being utilized, check the execution plan with EXPLAIN:如果要检查是否正在使用索引,请使用 EXPLAIN 检查执行计划:

https://dev.mysql.com/doc/refman/8.0/en/using-explain.html https://dev.mysql.com/doc/refman/8.0/en/using-explain.html

Or try using the MYSQL Full-Text Index MATCH()/AGAINST().或者尝试使用 MYSQL 全文索引 MATCH()/AGAINST()。 Here are some articles about:这里有一些关于:

https://severalnines.com/database-blog/full-text-searches-mysql-good-bad-and-ugly https://www.w3resource.com/mysql/mysql-full-text-search-functions.php https://severalnines.com/database-blog/full-text-searches-mysql-good-bad-and-ugly https://www.w3resource.com/mysql/mysql-full-text-search-functions.php

Edit : After some investigation, I came to the conclusion that there is no way a leading wildcard can utilize the Table index编辑:经过一番调查,我得出结论,前导通配符无法利用表索引

Wildcard search in MySQL full-text search https://blog.monyog.com/problem-queries-are-killing-your-database-performance/ MySQL 全文搜索中的通配符搜索https://blog.monyog.com/problem-queries-are-killing-your-database-performance/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM