拥有vs MySQL中相关SubQuery的位置

时间:2015-11-19 07:53:59

标签: mysql where-clause correlated-subquery having-clause

在找到一些结果时,我发现了以下两种方法,但无法确定哪种查询具有更好的性能。希望有人会建议!

SELECT SQL_CALC_FOUND_ROWS *, MATCH (m.firstname, m.lastname) AGAINST ('*keyword1* *keyword2* *keyword3*' IN BOOLEAN MODE) AS relavance FROM members m WHERE m.status = 1 AND m.membership > 1 AND (SELECT count(media_id) FROM arts AS media WHERE media.active = 1 AND media.owner = m.mem_id) > 0 AND MATCH (m.firstname, m.lastname) AGAINST ('*keyword1* *keyword2* *keyword3*' IN BOOLEAN MODE) ORDER BY relavance DESC, m.firstname ASC, m.lastname ASC

SELECT SQL_CALC_FOUND_ROWS *, (SELECT count(media_id) FROM arts AS media WHERE media.active = 1 AND media.owner = m.mem_id) AS arts_count, MATCH (m.firstname, m.lastname) AGAINST ('*keyword1* *keyword2* *keyword3*' IN BOOLEAN MODE) AS relavance FROM members m WHERE m.status = 1 AND m.membership > 1 AND MATCH (m.firstname, m.lastname) AGAINST ('*keyword1* *keyword2* *keyword3*' IN BOOLEAN MODE) HAVING arts_count > 0 ORDER BY relavance DESC, m.firstname ASC, m.lastname ASC

修改

我也找到了另一种方法,现在比较是在三个查询之间。以下是我发现的新查询以及从性能角度来看更好的所有三个查询?

SELECT SQL_CALC_FOUND_ROWS *, MATCH (m.firstname, m.lastname) AGAINST ('*keyword1* *keyword2* *keyword3*' IN BOOLEAN MODE) AS relavance FROM members m LEFT JOIN (SELECT count(media_id) AS arts_count, owner AS ar_owner FROM arts GROUP BY media_id) AS ar ON ar.ar_ar_owner = m.owner WHERE m.status = 1 AND m.membership > 1 AND MATCH (m.firstname, m.lastname) AGAINST ('*keyword1* *keyword2* *keyword3*' IN BOOLEAN MODE) AND  ar.arts_count > 0 ORDER BY relavance DESC, m.firstname ASC, m.lastname ASC

查询目标:要查找匹配关键字的用户,并且至少有一个上传的图片/艺术品,还有一些检查最终按相关性进行排序。

0 个答案:

没有答案