我一直在使用Fetch Next(而不是linq查询skip.take())但是在查询下面,我怎么能处理这个慢查询。我想让它更快。
如何重写此sql查询和linq版本(如果它更快)
var ciftciDestekler = ctx.tblDestek.SqlQuery(
" select d.ParselId,d.DestekId,d.Urun,d.KullanimSekli,d.TarimSekli,d.EkiliAlan,d.DesteklenenAlan,d.Aciklama,d.TarifeId,d.VeriSurumu "+
"from tblCiftci c "+
"inner join tblParsel p on c.CiftciId = p.CiftciId "+
"inner join tblDestek d on p.ParselId = d.ParselId where c.UretimSezonu = 2016 "+
"order by d.DestekId "+
"OFFSET 0 ROWS -- skip 10 rows "+
"FETCH NEXT 10 ROWS ONLY; --").ToList();
答案 0 :(得分:3)
我想指出,您的查询不会只选择10行 - 选择所有行,因为FETCH NEXT部分是可选的,您有commented it out个“ - 跳过10行”。
您的查询是一个没有换行符的巨大单行,因此实际上并非如此:
select d.ParselId,d.DestekId,d.Urun,d.KullanimSekli,d.TarimSekli,d.EkiliAlan,d.DesteklenenAlan,d.Aciklama,d.TarifeId,d.VeriSurumu
from tblCiftci c
inner join tblParsel p on c.CiftciId = p.CiftciId
inner join tblDestek d on p.ParselId = d.ParselId where c.UretimSezonu = 2016
order by d.DestekId
OFFSET 0 ROWS -- skip 10 rows
FETCH NEXT 10 ROWS ONLY; --
而是a:
select d.ParselId,d.DestekId,d.Urun,d.KullanimSekli,d.TarimSekli,d.EkiliAlan,d.DesteklenenAlan,d.Aciklama,d.TarifeId,d.VeriSurumu from tblCiftci c inner join tblParsel p on c.CiftciId = p.CiftciId inner join tblDestek d on p.ParselId = d.ParselId where c.UretimSezonu = 2016 order by d.DestekId OFFSET 0 ROWS -- skip 10 rows FETCH NEXT 10 ROWS ONLY; --
您应该删除该评论或将其作为Slash-Star评论,不会影响以下代码。