MySQL脚本优化

MySQL脚本优化,mysql,Mysql,我有两个MySQL表,graphics,rasp: 我的查询工作正常,但速度太慢(限制1大约200秒以上,并且只有大约50000条记录) 我需要从graphics中选择1个记录中的2个(结果记录): 我的问题是: SELECT r.num, r.from, r.to, g.id, g.comment, g.stationid1, g2.id, g2.comment, g2.stationid1 FROM graphics g, graphics g2, rasp r WHERE abs(t

我有两个MySQL表,
graphics
rasp

我的查询工作正常,但速度太慢(限制1大约200秒以上,并且只有大约50000条记录)

我需要从
graphics
中选择1个记录中的2个(结果记录):

我的问题是:

SELECT r.num, r.from, r.to, g.id, g.comment, g.stationid1, g2.id, g2.comment, g2.stationid1 
FROM graphics g, graphics g2, rasp r 
WHERE abs(timestampdiff(minute,g.dt,CONCAT(g.date,' ',r.from)))<30 
    AND g2.id>g.id 
    AND g.stationid1=r.stationfrom 
    AND abs(timestampdiff(minute,g2.dt,CONCAT(g2.date,' ',r.to)))<30 
    AND g2.stationid2=r.stationto 
    AND g.date=g2.date 
    AND g.trainid=g2.trainid 
group by g.date, g.comment 
LIMIT 1
选择r.num、r.from、r.to、g.id、g.comment、g.stationid1、g2.id、g2.comment、g2.stationid1
从图形g、图形g2、锉刀r
其中abs(timestampdiff(分钟,g.dt,CONCAT(g.date,,,r.from))g.id
g.stationd1=r.stationfrom

和abs(timestampdiff(minute,g2.dt,CONCAT(g2.date,,,r.to)))表上有什么索引?EXPLAIN对查询有什么解释?EXPLAIN Barmar,索引解决了我的问题,谢谢
SELECT r.num, r.from, r.to, g.id, g.comment, g.stationid1, g2.id, g2.comment, g2.stationid1 
FROM graphics g, graphics g2, rasp r 
WHERE abs(timestampdiff(minute,g.dt,CONCAT(g.date,' ',r.from)))<30 
    AND g2.id>g.id 
    AND g.stationid1=r.stationfrom 
    AND abs(timestampdiff(minute,g2.dt,CONCAT(g2.date,' ',r.to)))<30 
    AND g2.stationid2=r.stationto 
    AND g.date=g2.date 
    AND g.trainid=g2.trainid 
group by g.date, g.comment 
LIMIT 1