MySQL多表连接查询性能问题
我正在尝试编写一个搜索查询。
我有一个包含列的元表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是您完整的查询翻译,但您明白了。如果 MySQL 在处理如此多的子查询时遇到问题,请尝试这个。
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.