我一直在玩这个查询好几天,
select slno,Power_Factor_L32_Avg
from power_logger pl
where (pl.slno between 1851219 and 2042099)
and meter_id="logger1"
and pl.Power_Factor_L32_Avg is not null
and case when (((pl.slno=1851219)
and ((pl.Power_Factor_L32_Avg) not between 0.400 and 1.000))
or ((pl.slno=2042099) and
((pl.Power_Factor_L32_Avg) not between 0.400 and 1.000))
or ((pl.Power_Factor_L32_Avg not between 0.400 and 1.000) and
(((select p2.Power_Factor_L32_Avg
from power_logger p2
where p2.slno=(select slno
from power_logger
where slno>pl.slno
and Power_Factor_L32_Avg is not null
order by slno asc limit 1))
between 0.400 and 1.000)
or ((select p4.Power_Factor_L32_Avg
from power_logger p4
where p4.slno=(select slno
from power_logger
where slno<pl.slno
and Power_Factor_L32_Avg is not null
order by slno desc limit 1 ))
between 0.400 and 1.000))))
then (case when (((pl.slno=1851219)
and ((select p10.Power_Factor_L32_Avg
from power_logger p10
where p10.slno=pl.slno+1) is null
or (select p10.Power_Factor_L32_Avg
from power_logger p10
where p10.slno=pl.slno+1) between 0.400 and 1.000))
or (((select p5.Power_Factor_L32_Avg
from power_logger p5
where p5.slno=(select slno
from power_logger
where slno<pl.slno
and Power_Factor_L32_Avg is not null
order by slno desc limit 1 ))
between 0.400 and 1.000)
and ((select p6.Power_Factor_L32_Avg
from power_logger p6
where p6.slno=(select slno
from power_logger
where slno>pl.slno
and Power_Factor_L32_Avg is not null
order by slno asc limit 1 ))
between 0.400 and 1.000)))
then 0=1
else 1
end)
else 0=1
end
order by slno asc
乍一看很难理解查询,但它正在查询具有数百万条记录的表。这里唯一的问题是嵌套查询“ select slno from power_logger where
slno<pl.slno and Power_Factor_L32_Avg is not null order by slno desc limit 1
”和“ select slno from power_logger where
slno>pl.slno and Power_Factor_L32_Avg is not null order by slno desc limit 1
”,它们用于查找当前记录的前一个和下一个记录的序列号(slno),分别具有非空值。当这些查询自行运行时,它会在几微秒内给出序列号,但是当包含在上面的查询中时,它会搞砸整个事情,需要明确终止该进程。如果不使用嵌套查询,则上面的查询将在不到 1.5 秒内执行,而是被下一个/上一个序列号替换,无论它是否为空。
这不会去任何地方。感谢是否有人可以帮助我。
使用临时表的新查询,
create temporary table temp(
id int auto_increment
,slno int
,pf decimal(10,4)
,primary key(id)
);
insert into temp(slno,pf)
(select slno,Power_Factor_L32_Avg
from power_logger
where slno between 1851219 and 2042099
and meter_id='logger1'
and Power_Factor_L32_Avg is not null);
select pl.slno,pl.Power_Factor_L32_Avg,t1.slno,t1.pf
from power_logger pl
left join temp t1 on t1.slno>pl.slno
where pl.slno between 1851219 and 2042099 and pl.meter_id='logger1'
and pl.Power_Factor_L32_Avg is not null;
这是为了测试是否与从 power_logger 表slno
一起获取功率因数为空的下一个。slno
这也不起作用。我不确定是否有任何其他工作可以在slno
不使用大于号的情况下获得下一个。