一个SQL分页查询的优化问题
A表是博客信息表,里面字段有BlogId,UserId,Title,BlogText等信息
B表是注册用户表,里面字段有UserId,Password,LogonName,Icon等信息
两个表的数据都在100W左右
现在页面分页查询博客表的记录,也要取出来用户表中的LogonName和Icon。
LogonName有时候在某些时候会做为条件。
现在查询语句如下
SELECT BlogId,UserId,Title, BlogText, Icon
FROM
(SELECT
ROW_NUMBER() OVER (order by b.BlogId) as RowNumber,
b.*, u.Icon
FROM BlogInfo AS b
inner join UserInfo u On b.UserId=u.UserId
where
//某些情况下有这个条件
u.LogonName IN(参数)
) t
where rownumber>500000 and rownumber<500010
这样写的话,结果基本不能出来
如果不用Inner Join的话,时间一般就是0ms
这个地方为什么会这样?
怎么样去查询最好?