以下是我的查询,我正在navicat我的sql执行包含超过91000条记录的表。
select l.*
from localcdrsmem l
LEFT JOIN (
SELECT LocalCDRID, ForeignCDRsID
from localcdrsmem l
INNER JOIN foreigncdrsmem f
ON l.CalledNumber=f.CalledNumber and
ABS(l.Duration-f.Duration)<= 2 and
ABS(TIMESTAMPDIFF(SECOND,f.StartTime,l.StartTime))<=100
group by l.CalledNumber)
as temp ON l.LocalCDRID = temp.LocalCDRID
where temp.LocalCDRID IS NULL;
表是:
localcdrsmem - >
+---------+------------+--------+----------
localcdrid | callednumber | duration | starttime
+---------+------------+--------+----------+
foreigncdrsmem- >
+------------+------------+--------+----------
foreigncdrid | callednumber | duration | starttime
+---------+------------+--------+----------+
优化此查询的任何想法??
答案 0 :(得分:-1)
select l.* from localcdrsmem l LEFT JOIN
(SELECT LocalCDRID, ForeignCDRsID from localcdrsmem l
INNER JOIN foreigncdrsmem f
ON l.CalledNumber=f.CalledNumber
WHERE ABS(l.Duration-f.Duration)<= 2
and ABS(TIMESTAMPDIFF(SECOND,f.StartTime,l.StartTime))<=100
group by l.CalledNumber) as temp
ON l.LocalCDRID = temp.LocalCDRID where temp.LocalCDRID IS NULL;
尝试以上查询。此查询执行得更快。只需在此语句and
之后使用where
子句更改ON l.CalledNumber=f.CalledNumber
语句。您可以看到我们的查询之间的区别。