2017-07-17 28 views
-1

I do not explain the logic of the query,因爲它不簡單。如果我詳細解釋 - 沒有人會想要閱讀並深入瞭解查詢的本質。MySQL:subquerys或聯合?

有兩個查詢。他們做得很好。結果:完全相同。在紙上(和數據庫上)用鉛筆檢查手動模式。

哪個查詢會減少服務器的負載?或者只能在真正的(生產)服務器上工作一段時間才能找到它?


操作UNION很難裝載服務器? What should I look for in the explanations of the querys?


1 Q蒙山+ + +

select (select count(comid) from coms join posts on pid=pid_coms where uid_posts=8888 and uid_coms=8888) 

+ 
(select count(comid) from frends join posts on sl_frend=uid_posts join coms on pid=pid_coms 
where uid_coms=8888 and m_frend=8888 and ((postacc=1 and postcomacc=2) or (postacc=2 and postcomacc=2) or (postacc=2 and postcomacc=1))) 

+ 
(select count(comid) from frends join posts on m_frend=uid_posts join coms on pid=pid_coms 
where uid_coms=8888 and sl_frend=8888 and ((postacc=1 and postcomacc=2) or (postacc=2 and postcomacc=2) or (postacc=2 and postcomacc=1))) 

+ 
(select count(comid) from coms join posts on pid_coms=pid where uid_posts != 8888 and uid_coms=8888 and postacc=1 and postcomacc=1) CountMyComms; 

EXPLAIN 
+----+-------------+--------+--------+-----------------------+----------+---------+---------------------+------+----------------+ 
| id | select_type | table | type | possible_keys   | key  | key_len | ref     | rows | Extra   | 
+----+-------------+--------+--------+-----------------------+----------+---------+---------------------+------+----------------+ 
| 1 | PRIMARY  | NULL | NULL | NULL     | NULL  | NULL | NULL    | NULL | No tables used | 
| 5 | SUBQUERY | coms | ref | uid_coms,pid_coms  | uid_coms | 4  | const    | 7 |    | 
| 5 | SUBQUERY | posts | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where | 
| 4 | SUBQUERY | frends | ref | m_frend,sl_frend  | sl_frend | 4  | const    | 1 |    | 
| 4 | SUBQUERY | coms | ref | uid_coms,pid_coms  | uid_coms | 4  | const    | 7 |    | 
| 4 | SUBQUERY | posts | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where | 
| 3 | SUBQUERY | coms | ref | uid_coms,pid_coms  | uid_coms | 4  | const    | 7 |    | 
| 3 | SUBQUERY | posts | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where | 
| 3 | SUBQUERY | frends | ref | m_frend,sl_frend  | sl_frend | 4  | mbs.posts.uid_posts | 1 | Using where | 
| 2 | SUBQUERY | coms | ref | uid_coms,pid_coms  | uid_coms | 4  | const    | 7 |    | 
| 2 | SUBQUERY | posts | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where | 
+----+-------------+--------+--------+-----------------------+----------+---------+---------------------+------+----------------+ 
11 rows in set (0.00 sec) 

2 Q蒙山工會

select count(comid) from (select comid from coms join posts on pid=pid_coms where uid_posts=8888 and uid_coms=8888 

union 
select comid from frends join posts on sl_frend=uid_posts join coms on pid=pid_coms 
where uid_coms=8888 and m_frend=8888 and ((postacc=1 and postcomacc=2) or (postacc=2 and postcomacc=2) or (postacc=2 and postcomacc=1)) 

union 
select comid from frends join posts on m_frend=uid_posts join coms on pid=pid_coms 
where uid_coms=8888 and sl_frend=8888 and ((postacc=1 and postcomacc=2) or (postacc=2 and postcomacc=2) or (postacc=2 and postcomacc=1)) 

union 
select comid from coms join posts on pid_coms=pid 
where uid_posts != 8888 and uid_coms=8888 and postacc=1 and postcomacc=1) a; 

EXPLAIN 
+----+--------------+----------------+--------+-----------------------+----------+---------+---------------------+------+------------------------------+ 
| id | select_type | table   | type | possible_keys   | key  | key_len | ref     | rows | Extra      | 
+----+--------------+----------------+--------+-----------------------+----------+---------+---------------------+------+------------------------------+ 
| 1 | PRIMARY  | NULL   | NULL | NULL     | NULL  | NULL | NULL    | NULL | Select tables optimized away | 
| 2 | DERIVED  | coms   | ref | uid_coms,pid_coms  | uid_coms | 4  |      | 7 |        | 
| 2 | DERIVED  | posts   | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where     | 
| 3 | UNION  | coms   | ref | uid_coms,pid_coms  | uid_coms | 4  |      | 7 |        | 
| 3 | UNION  | posts   | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where     | 
| 3 | UNION  | frends   | ref | m_frend,sl_frend  | sl_frend | 4  | mbs.posts.uid_posts | 1 | Using where     | 
| 4 | UNION  | frends   | ref | m_frend,sl_frend  | sl_frend | 4  |      | 1 |        | 
| 4 | UNION  | coms   | ref | uid_coms,pid_coms  | uid_coms | 4  |      | 7 |        | 
| 4 | UNION  | posts   | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where     | 
| 5 | UNION  | coms   | ref | uid_coms,pid_coms  | uid_coms | 4  |      | 7 |        | 
| 5 | UNION  | posts   | eq_ref | PRIMARY,pid,uid_posts | PRIMARY | 4  | mbs.coms.pid_coms | 1 | Using where     | 
| NULL | UNION RESULT | <union2,3,4,5> | ALL | NULL     | NULL  | NULL | NULL    | NULL |        | 
+----+--------------+----------------+--------+-----------------------+----------+---------+---------------------+------+------------------------------+ 
12 rows in set (0.00 sec) 
+0

您是否必須使用'COUNT(comid)'?或者你可以使用'COUNT(*)'? (也就是說,有沒有空'comid'值?'COUNT(*)'稍快。在你的第二個查詢,是由'UNION'不相交的各部分退還?如果是的話'comid'值的集合,使用'UNION ALL'。同樣,使用'COUNT(*)'如果你能。 –

+0

沒有'null',好吧,我使用COUNT(*) –

回答

1

它看起來像你要找的記錄數的總和了一系列不同查詢你的表格。

第一個選擇...計算每個查詢的結果,然後添加它們...會更快。爲什麼?它有更少的工作要做。你的第二個選擇必須糾纏一組comid值,然後對它們進行計數。這需要時間。

如果可以,請使用COUNT(*)。這個更便宜。如果可以,請使用UNION ALL而不是UNION; UNION刪除重複項,並且UNION ALL不刪除重複項。刪除重複項需要時間。

這兩種替代方法的性能取決於每個子查詢的索引的良好選擇。

+0

'duolicates'不可能的,目前指數。運行時的這種(蒙山任何變種:union,+ + +,count(comid),count(*))queryS小於0.08秒,測試大約70 000 - 100 000條記錄,一切正常,將來我需要監控生產服務器。 –