Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

MySQL: subquerys OR unions?

I do not explain the logic of the query, because it is not simple. If I explain in detail - no one will want to read and delve into the essence of the query.

There are two querys. They do their job well. Result: identical. Checked in manual mode, with a pencil on paper (and on a data base).

Which query will less load the server? Or will it only be found out some time after working on a real (production) server?


The operation UNION veryhard load the server? 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 whith unions

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you're looking for the sum of record counts for a series of different queries of your tables.

The first alternative ... count each query's results, then add them ... will be faster. Why? It has less work to do. Your second alternative has to wrangle a set of comid values, and then count them. That takes time.

Use COUNT(*) if you can. It's cheaper. Use UNION ALL instead of UNION when you can; UNION removes duplicates and UNION ALL doesn't. Removing duplicates takes time.

The performance of either alternative depends on good choices for indexes for each subquery.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...