求教一条sql语句执行不出来的问题,各位都帮忙出个主意
这条语句是查询出我想要的表的最近日期的3条最新数据。
因为原sql比较长,可能看起来麻烦了,我举个例子,
select * from (select 100 percent a.id,b.name,b.time from tba ta join tbb tb on tba.id=tbb.aid) a where (select count(*) from (select 100 percent a.id,b.name,b.time from tba ta join tbb tb on tba.id=tbb.aid) b where a.id=b.id and a.time>b.time)<3 order by time
那么现在问题就是,执行时间实在是太长了,我查询过,符合条件的数据大概有6W条,然后这个语句执行了30多分钟都没成功,各位是否能指点一下问题在哪里,或者有没有更好的办法去查询出我要的结果。 SQL
[解决办法]
把order by 去掉试试速度怎么样
[解决办法]
select * from (select 100 percent a.id,b.name,b.time from tba ta join tbb tb on tba.id=tbb.aid) a where (select count(1) from (select 100 percent a.id,b.name,b.time from tba ta join tbb tb on tba.id=tbb.aid) b where a.id=b.id and a.time>b.time)<3 order by time
这样呢
[解决办法]
放点数据出来吧
[解决办法]
你把a,b两个表做成视图,然后再看看
[解决办法]
select t.id, t.name, t.time
From (select a.id, b.name, b.time, row_number() over(partition by a.id order by b.time) as row from tba a inner join tbb b on a.id=b.id
) t
Where t.row <=3