MySQL多表连接查询性能问题

发布于 2024-12-12 08:01:48 字数 1657 浏览 0 评论 0原文

我正在尝试编写一个搜索查询。

我有一个包含列的元表:

ID | meta_key | meta_value

我想进行如下搜索:

SELECT ID FROM meta_table WHERE meta_key = 'title' AND meta_value = 'searchword'

但是,复杂的是我想从多组(多个 meta_keys 和多个 meta_values< /code>) 如果这有意义的话。
这是生成的查询之一:

SELECT name_table_dan.ID FROM 
    (SELECT user_id as ID FROM wp_usermeta WHERE (meta_key = 'first_name' or meta_key = 'last_name') AND meta_value LIKE '%dan%') as name_table_dan 
INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE (meta_key = 'first_name' or meta_key = 'last_name') AND meta_value LIKE '%newman%') as name_table_newman
    ON name_table_newman.ID=name_table_dan.ID 
    INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE meta_key = 'user_industry' AND meta_value LIKE '%games%') as industry_table_games 
    ON industry_table_games.ID=name_table_newman.ID
    INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE (meta_key = 'user_firm' or meta_key = 'company') AND meta_value LIKE '%incorp%') as company_table_incorp
    ON company_table_incorp.ID=industry_table_games.ID
    INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE meta_key = 'user_location' AND meta_value LIKE '%denver%') as location_table_denver
    ON location_table_denver.ID=company_table_incorp.ID GROUP BY ID;

很抱歉查询很长,但只是想表达我的观点。

查询工作得很好,问题在于,当其中一些搜索(即在查询开始时选择生成的表)返回大型结果集时,查询时间会急剧减慢,如果我得到一些大型结果集,那么该查询几乎使系统挂起,我必须手动结束该过程。

我认为问题可能出在MySQL(我相信postgre没有这个问题)。我环顾四周,发现mysql在嵌套查询和连接方面存在问题。

我问处理这个问题的最佳方法是什么?理想情况下,我希望避免进行多个数据库调用,因为我觉得这应该是不必要的,因此这不是我正在寻找的答案,除非别无选择。

I am trying to write a query for a search.

I have a meta table with columns:

ID | meta_key | meta_value

I want to pull a search like the following:

SELECT ID FROM meta_table WHERE meta_key = 'title' AND meta_value = 'searchword'

However, the complication is that I want to pull this from multiple sets of (multiple meta_keys and multiple meta_values) if that makes sense.
Here is one of the generated queries:

SELECT name_table_dan.ID FROM 
    (SELECT user_id as ID FROM wp_usermeta WHERE (meta_key = 'first_name' or meta_key = 'last_name') AND meta_value LIKE '%dan%') as name_table_dan 
INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE (meta_key = 'first_name' or meta_key = 'last_name') AND meta_value LIKE '%newman%') as name_table_newman
    ON name_table_newman.ID=name_table_dan.ID 
    INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE meta_key = 'user_industry' AND meta_value LIKE '%games%') as industry_table_games 
    ON industry_table_games.ID=name_table_newman.ID
    INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE (meta_key = 'user_firm' or meta_key = 'company') AND meta_value LIKE '%incorp%') as company_table_incorp
    ON company_table_incorp.ID=industry_table_games.ID
    INNER JOIN (SELECT user_id as ID FROM wp_usermeta WHERE meta_key = 'user_location' AND meta_value LIKE '%denver%') as location_table_denver
    ON location_table_denver.ID=company_table_incorp.ID GROUP BY ID;

Sorry for the long query, but just trying to make my point.

The query works just fine the problem being that when some of these searches - namely, the select generated tables towards the beginning of the query - return large result sets, the query time slows down dramatically, and if I get a few large result sets then the query pretty much hangs the system to where I have to end the process manually.

I think the problem may lie with MySQL (I believe postgre does not have this problem). I looked around and discovered that mysql has problems with nested queries and joins.

I am asking what the best way to handle this would be? I ideally would like to avoid have multiple database calls as I feel this should be unnecessary, so this is not an answer I am looking for unless there is no alternative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名 2024-12-19 08:01:48
 SELECT name_table_dan.ID FROM wp_usermeta name_table_dan
     INNER JOIN wp_usermeta name_table_newman ON name_table_dan.ID = name_table_newman.ID
      ...
 WHERE 
   (name_table_dan.meta_key = 'first_name' OR name_table_dan.meta_key = 'last_name' AND name_table_dan.meta_value LIKE '%dan%')
   AND
   (name_table_newman.meta_key = 'first_name' OR name_table_newman.meta_key = 'last_name' AND name_table_dan.meta_value LIKE '%newman%')
 ...

这不是您完整的查询翻译,但您明白了。如果 MySQL 在处理如此多的子查询时遇到问题,请尝试这个。

 SELECT name_table_dan.ID FROM wp_usermeta name_table_dan
     INNER JOIN wp_usermeta name_table_newman ON name_table_dan.ID = name_table_newman.ID
      ...
 WHERE 
   (name_table_dan.meta_key = 'first_name' OR name_table_dan.meta_key = 'last_name' AND name_table_dan.meta_value LIKE '%dan%')
   AND
   (name_table_newman.meta_key = 'first_name' OR name_table_newman.meta_key = 'last_name' AND name_table_dan.meta_value LIKE '%newman%')
 ...

It is not the complete query translation of yours, but you get the idea. If MySQL is having problems with so many subqueries, try this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文