【发布时间】:2013-06-12 10:30:59
【问题描述】:
我有查询,它给出了正确的结果,但是,它有内部查询,重复相同的条件。我已经尽量缩短查询的长度。
谁能帮我降低查询的复杂性,因为它需要 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。查询变得复杂
【问题讨论】:
-
索引也是你的朋友,如果你发现自己在 where 条件中使用了很多列,创建一个索引,你会看到神奇的事情发生;)
-
同意你必须正确索引表格
-
谁能举个简单的例子吗?
-
如前所述,添加一个索引,涵盖 where 或 order by 语句中使用的所有字段。您的索引可能会有所不同,具体取决于您的数据库引擎,但通常这应该会提高性能。请注意,索引会增加您的数据库大小,特别是如果您添加许多索引,大型数据库可能会变得更大!
标签: mysql if-statement sum group-concat