日期:2014-05-18  浏览次数:20632 次

sql查询速度的问题!
高手们,真的很着急,我是新手,请求大家帮帮我。在sql数据库里大概有200万以上的数据,查询速度太慢,索引建的不太对,请大侠们替我出出主意看看索引怎么建,还有就是这个sql语句还需要优化没?多谢多谢
SELECT   DeviceNo,   Road,No,   PlateColor,
                    (SELECT   TypeName
                  FROM   tabType
                  WHERE   TypeCode   =Type)   AS   Type,Logo,   Speed,   WatchTime,   Pic1
FROM   tabVehicle
WHERE   (DeviceNo   =   '地点1 '   or   DeviceNo   =   '地点二 '   OR   DeviceNo   =   '地点三 '   OR   DeviceNo   =   '地点4 '     )   AND  
            (WatchTime   >   '2007-01-17   08:54:31 ')   AND   (WatchTime   <   '2007-01-18   20:54:31 ')   AND  
            (Type   =
                    (SELECT   TypeCode
                  FROM   tabType
                  WHERE   TypeName   =   '小))

------解决方案--------------------
SELECT DeviceNo, Road,No, PlateColor,
(SELECT TypeName
FROM tabType
WHERE TypeCode =Type) AS Type,Logo, Speed, WatchTime, Pic1
FROM tabVehicle
WHERE (DeviceNo in( '地点1 ', '地点二 ', '地点三 ', '地点4 ') AND
(WatchTime between '2007-01-17 08:54:31 ' AND '2007-01-18 20:54:31 ') AND
(Type =
(SELECT TypeCode
FROM tabType
WHERE TypeName = '小))

------解决方案--------------------
DeviceNo,WatchTime,Type 上建索引
SELECT DeviceNo, Road,No, PlateColor,
(SELECT TypeName
FROM tabType
WHERE TypeCode =Type) AS Type,Logo, Speed, WatchTime, Pic1
FROM tabVehicle
WHERE (DeviceNo = '地点1 ' AND
(WatchTime between '2007-01-17 08:54:31 ' AND '2007-01-18 20:54:31 ') AND
(Type =
(SELECT TypeCode
FROM tabType
WHERE TypeName = '小))
union all
SELECT DeviceNo, Road,No, PlateColor,
(SELECT TypeName
FROM tabType
WHERE TypeCode =Type) AS Type,Logo, Speed, WatchTime, Pic1
FROM tabVehicle
WHERE (DeviceNo = '地点2 ' AND
(WatchTime between '2007-01-17 08:54:31 ' AND '2007-01-18 20:54:31 ') AND
(Type =
(SELECT TypeCode
FROM tabType
WHERE TypeName = '小))
union all
SELECT DeviceNo, Road,No, PlateColor,
(SELECT TypeName
FROM tabType
WHERE TypeCode =Type) AS Type,Logo, Speed, WatchTime, Pic1
FROM tabVehicle
WHERE (DeviceNo = '地点3 ' AND
(WatchTime between '2007-01-17 08:54:31 ' AND '2007-01-18 20:54:31 ') AND
(Type =
(SELECT TypeCode
FROM tabType
WHERE TypeName = '小))
union all
SELECT DeviceNo, Road,No, PlateColor,
(SELECT TypeName
FROM tabType
WHERE TypeCode =Type) AS Type,Logo, Speed, WatchTime, Pic1
FROM tabVehicle
WHERE (DeviceNo = '地点4 ' AND
(