表film 其中id为主建
id filmname paici
1 测试影片a 2222222
2 测试影片b 2222222
3 测试影片c 2222222
4 测试影片d 2222222
5 测试影片e 2222222
表data
id playdate filmid piaofang
1 '2012-09-01' 1 2222222
2 '2012-09-02' 1 2222222
3 '2012-09-03' 1 2222222
4 '2012-09-04' 1 2222222
5 '2012-09-01' 1 2222222
以下sql语句执行速度一样吗?data表数据大概有500W行数据
1.
select A.* from (
select * from data where playdate between '2012-09-01' and '2012-09-20'
)A left join film on a.filmid=film.id
2.
select * from data d
left join film f on d.filmid=f.id
where d.playdate between '2012-09-01' and '2012-09-20'
哪个速度更快一点,,,
如果在playdate上加入聚集索引,,,速度又会怎么样....
------解决方案--------------------
- SQL code
CREATE TABLE film (id INT PRIMARY KEY , filmname VARCHAR(20), paici VARCHAR(20)) INSERT INTO film SELECT 1 ,'测试影片a' ,'2222222' UNION ALL SELECT 2 ,'测试影片b' ,'2222222' UNION ALL SELECT 3 ,'测试影片c' ,'2222222' UNION ALL SELECT 4 ,'测试影片d' ,'2222222' UNION ALL SELECT 5 ,'测试影片e' ,'2222222' CREATE TABLE DATA(id INT, playdate datetime,filmid CHAR(1), piaofang VARCHAR(20) ) INSERT INTO DATA SELECT 1, '2012-09-01', 1 ,'2222222' UNION ALL SELECT 2 ,'2012-09-02', 1 ,'2222222' UNION ALL SELECT 3 ,'2012-09-03', 1 ,'2222222' UNION ALL SELECT 4 ,'2012-09-04' ,1 ,'2222222' UNION ALL SELECT 5 ,'2012-09-01', 1 ,'2222222' --1. select A.* from ( select * from data where playdate between '2012-09-01' and '2012-09-20' )A left join film on a.filmid=film.id --2. select * from data d left join film f on d.filmid=f.id where d.playdate between '2012-09-01' and '2012-09-20'
------解决方案--------------------
如果输出的列一样,那就会生成一样的执行计划,速度是一样的,在Playdate列键建索引是十分必要的,当然不是针对这两三条数的时候,当你的数据变多,这个索引就会起作用,做性能测试的时候数据量一定要和时间数据相当,不能是这样的小模型,这是不准确的