简体   繁体   English

mysql联合订单性能

[英]mysql union order performance

I have a MySQL union of 2 queries, with ORDER BY done on indexed columns. 我有2个查询的MySQL联合,在索引列上完成了ORDER BY。 What is better performance wise, order by each of the union queries separately, or order by at the end of the union as I currently have it? 在性能上更好的方法是:按每个联合查询分别进行排序,或者按我目前的意愿在联合末尾进行排序?

(SELECT pep.P_Surname, pep.P_FirstName, pep.P_ID, adm.Loc_ID AS lid 
FROM lup_people pep
JOIN tr_adm adm ON pep.P_ID=adm.P_ID                    
WHERE (P_FirstName LIKE 'g%' 
OR P_Surname LIKE 'g%')
AND pep.active = 'Y')

 UNION

(SELECT pep.P_Surname, pep.P_FirstName, pep.P_ID, vil.Loc_ID AS lid 
FROM lup_people pep
JOIN tr_village vil ON (pep.P_ID=vil.P1_ID OR pep.P_ID=vil.P2_ID)                    
WHERE (P_FirstName LIKE 'g%' 
OR P_Surname LIKE 'g%') 
AND pep.active = 'Y') 

ORDER BY P_Surname,P_FirstName LIMIT 50

VS VS

(SELECT pep.P_Surname, pep.P_FirstName, pep.P_ID, adm.Loc_ID AS lid 
FROM lup_people pep
JOIN tr_adm adm ON pep.P_ID=adm.P_ID                    
WHERE (P_FirstName LIKE 'g%' 
OR P_Surname LIKE 'g%')
AND pep.active = 'Y' ORDER BY P_Surname,P_FirstName LIMIT 50)

UNION

(SELECT pep.P_Surname, pep.P_FirstName, pep.P_ID, vil.Loc_ID AS lid 
FROM lup_people pep
JOIN tr_village vil ON (pep.P_ID=vil.P1_ID OR pep.P_ID=vil.P2_ID)                    
WHERE (P_FirstName LIKE 'g%' 
OR P_Surname LIKE 'g%') 
AND pep.active = 'Y' ORDER BY P_Surname,P_FirstName LIMIT 50) 

(1) You should use union all , unless you intend for duplicates to be removed by the union . (1)除非您打算由union删除重复项,否则应使用union all union

(2) The queries are different. (2)查询不同。 The first returns 100 rows. 第一个返回100行。 The second returns 50 rows. 第二个返回50行。

(3) You should not depend on the ordering of results from a union query. (3)您不应该依赖union查询的结果顺序。 If you want results in a particular order, then use an order by after the union . 如果要按特定顺序生成结果,则在union之后使用一个order by

(4) Quite possibly, the fastest approach is to use left outer join . (4)很有可能,最快的方法是使用left outer join It would look something like: 它看起来像:

SELECT pep.P_Surname, pep.P_FirstName, pep.P_ID,
       coalesce(adm.Loc_ID, vil.loc_id, vil2.loc_id) AS lid 
FROM lup_people pep LEFT JOIN
     tr_adm adm
     ON pep.P_ID = adm.P_ID LEFT JOIN
     tr_village vil
     ON pep.P_ID = vil.P1_ID LEFT JOIN
     tr_village vil2
     ON pep.P_ID = vil2.P2_ID                 
WHERE (P_FirstName LIKE 'g%' OR P_Surname LIKE 'g%') AND pep.active = 'Y'
ORDER BY P_Surname, P_FirstName
LIMIT 50;

Based on the volume of data, it could go either way. 根据数据量,它可以任意选择。 The issue you are faced with is this. 您面临的问题是这个。 In your first query you are doing a select-from getting ALL POSSIBLE answers that qualify, after which, THEN the order by clause is applied and finally the limit. 在第一个查询中,您要进行选择,以获取所有符合条件的可能答案,然后,然后应用order by子句,最后应用限制。 So, if you have 1000's of qualified records, you are still plowing through them, ordering, then get the top 50. 因此,如果您有1000条合格记录,那么您仍在对其进行耕种,排序,然后获得前50名。

The second one will AT LEAST, cut their respective result sets down to 50 from each leaving a max of 100 records. 第二个将至少将其各自的结果集从每个减少到50个,最多保留100条记录。

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

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