【问题标题】:MySQL: subquerys OR unions?MySQL:子查询或联合?
【发布时间】:2017-07-17 21:16:11
【问题描述】:

I do not explain the logic of the query,因为不简单。如果我详细解释 - 没有人愿意阅读和深入研究查询的本质。

有两个查询。他们做得很好。结果:相同。在手动模式下,用铅笔在纸上(和数据库)检查。

哪个查询会减少服务器的负载?还是只有在实际(生产)服务器上工作一段时间后才能发现?


操作UNION很难加载服务器? What should I look for in the explanations of the querys?


1 q whith + + +

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)

【问题讨论】:

  • 一定要使用COUNT(comid)吗?或者你可以使用COUNT(*)? (也就是说,是否有任何空的comid 值?COUNT(*) 稍微快一些。在您的第二个查询中,UNION 的每个部分返回的comid 值集是否不相交?如果是,请使用@987654332 @. 再次,如果可以,请使用COUNT(*)
  • 没有 null,好的,我使用 COUNT(*)

标签: mysql subquery union


【解决方案1】:

您似乎正在查找对表的一系列不同查询的记录总数。

第一种选择...计算每个查询的结果,然后添加它们...会更快。为什么?它要做的工作更少。您的第二种选择必须整理一组comid 值,然后计算它们。这需要时间。

如果可以,请使用COUNT(*)。这个更便宜。尽可能使用UNION ALL 而不是UNIONUNION 删除重复项,UNION ALL 不删除。删除重复项需要时间。

任一替代方案的性能取决于为每个子查询选择好的索引。

【讨论】:

  • duolicates 不可能,存在索引。这个(带有任何变体:union、++、count(comid)、count(*))queryS 的运行时间少于 0.08 秒。测试了大约 70 000 - 100 000 条记录,一切正常。将来我需要监控生产服务器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 2013-02-27
  • 2017-04-22
  • 1970-01-01
  • 2017-11-02
  • 2015-05-04
  • 1970-01-01
相关资源
最近更新 更多