1

我有查询,它给出了正确的结果,但是,它有内部查询,重复相同的条件。我试图从我的结尾尽可能缩短查询的长度。

任何人都可以帮助我降低查询的复杂性,因为在服务器中获取结果需要 12 分钟,这可能会在未来引起很多问题。

问题是,重复相同的条件,并且查询在其中使用内部查询。

使用这些 mysql 参数查询:-

sum,if,GROUP_CONCAT在其中。

询问:-

select 
sum( 
IF(priority="P1",1,0)) P1,
sum( 
IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P1") ,1,0))P1_exeeded,
 (select 
 GROUP_CONCAT( DISTINCT bug_id) 
 from bugs 
 where  
 ( 
   IF((timediff(delta_ts,creation_ts) > "00:02:00") && (priority="P1") 
   && (product_id=237)
   &&(bugs.resolution='FIXED')
   &&(bug_status="RESOLVED")
   &&(bugs.creation_ts >='2013-06-14 09:00:00' 
   && bugs.creation_ts <= '2013-06-16 08:59:59') ,1,0)
 )
) as bug_ids,

SUM(
IF(priority="P2",1,0)) P2,
sum( 
IF((timediff(delta_ts,creation_ts) > "00:01:00") 
&& (priority="P2") ,1,0))P2_exeeded,
(select GROUP_CONCAT( DISTINCT bug_id) 
 from bugs 
 where
 ( 
 IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P2") 
 && (product_id=237)&&(bugs.resolution='FIXED')
 &&(bug_status="RESOLVED")&&(bugs.creation_ts >='2013-06-14 09:00:00' 
 && bugs.creation_ts <= '2013-06-16 08:59:59') ,1,0)
 )
 ) as bug_ids,
 SUM(
 IF(priority="P3",1,0)) P3count,
 SUM(
 IF(priority="P4",1,0)) P4count 
 from bugs 
 where bugs.product_id=237 
 and bugs.resolution='FIXED' 
 and bugs.creation_ts >='2013-06-14 09:00:00' 
 and bugs.creation_ts <= '2013-06-16 08:59:59' 
 and bug_status="RESOLVED";

结果:-

+--------+------------+---------+------+------------ +---------+---------+---------+
| P1 | P1_exeeded | bug_ids | P2 | P2_exeeded | bug_ids | P3count | P4count |
+--------+------------+---------+------+------------ +---------+---------+---------+
| 7 | 1 | 3743304 | 6 | 1 | 3743305 | 5 | 1 |
+--------+------------+---------+------+------------ +---------+---------+---------+
  • 为了获取错误 ID,我使用了 group concat。查询变得复杂
4

1 回答 1

0

得到了答案:-

解决它像: -

select 
sum(
IF(
priority="P1",1,0)) P1, 
sum(
IF(
(timediff(delta_ts,creation_ts) > "00:02:00") && (priority="P1") ,1,0)) P1_exeeded,
REPLACE(GROUP_CONCAT(
IF(
(timediff(delta_ts,creation_ts) > "00:02:00") 
  && (priority="P1") ,bug_id,'')),',,','' ) as bugids,  
SUM(
IF(priority="P2",1,0)) P2, 
sum( IF((timediff(delta_ts,creation_ts) > "00:01:00") 
 && (priority="P2") ,1,0))P2_exeeded,
REPLACE(GROUP_CONCAT(IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P2") ,bug_id,'')),',,','' ) as bugids ,
SUM(
IF(priority="P3",1,0)) P3count, 
sum( 
IF(
(timediff(delta_ts,creation_ts) > "00:02:00") 
&& (priority="P3"),1,0))P3_exeeded,
REPLACE(GROUP_CONCAT(
IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P3") ,bug_id,'')),',,','' ) asbugids, 
SUM(
 IF(priority="P4",1,0)) P4count,
sum(
 IF(
(timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P4") ,1,0)) P4_exeeded,
 REPLACE(
 GROUP_CONCAT(
  IF(
  (timediff(delta_ts,creation_ts) > "00:02:00") 
  && (priority="P4") ,bug_id,'')),',,','' ) as bugids 
  from bugs 
  where bugs.product_id=237 
  and bugs.resolution  ='FIXED' 
  and bugs.creation_ts >='2013-06-14 09:00:00' 
  and bugs.creation_ts <= '2013-06-16 08:59:59' 
  and bug_status="RESOLVED";

我在 0.01 秒内得到了结果

+--------+------------+------------+------+---------- --+---------+---------+------------+--------+----- ----+------------+--------+
| P1 | P1_exeeded | 臭虫 | P2 | P2_exeeded | 臭虫 | P3count | P3_exeeded | 臭虫 | P4count | P4_exeeded | 臭虫 |
+--------+------------+------------+------+---------- --+---------+---------+------------+--------+----- ----+------------+--------+
| 7 | 1 | ,3743304, | 6 | 1 | 3743305 | 5 | 0 | | 1 | 0 | |
+--------+------------+------------+------+---------- --+---------+---------+------------+--------+----- ----+------------+--------+

谢谢你们,我使用了替换,而不是内部查询,并使用了 group con cat,w/o 内部查询。

于 2013-06-16T15:30:25.053 回答