Query #1 returns thousands of rows for each skid. I like to know if there is a faster way select only top 10 for each skid. query #2 is slow. Is there is any way I can optimize in query #1 to make it faster?
query #1
select skid, date_run, price, ROW_NUMBER()over(PARTITION BY
skid ORDER BY date_run ASC) as rowid
from stocktrack where rsi is null
query #2
with ctevalue
(select skid, date_run, price, ROW_NUMBER()over(PARTITION BY
skid ORDER BY date_run ASC) as rowid
from stocktrack where rsi is null
)
select skid, date_run rowid
from ctevalue
where rowid < 11
Did you try
select
*
from
(select
skid, date_run, price, ROW_NUMBER() over (PARTITION BY skid ORDER BY date_run ASC) as rowid
from
stocktrack where rsi is null) data
where
data.rowid < 11
Hope this helps
Try using cross apply
:
select st.skid, st.date_run, st.price
from sktable sk cross apply
(select top 10 st.*
from stocktrack st2
where st2.skid = sk.skid and
order by date_run asc
) st
along with an index on stocktrack(skid, date_run, price)
.
The first table is wherever the skid
is referring to. If necessary, you could try (select distinct skid from stocktrack st)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.