ms sql 2000高手给优化一下sql查询

binghaizjw 2009-01-14 06:02:07

这里只设计到一个表 bloginfo
字段有 id,主键; blog_url,blog网络地址;username,作者;webname,网站名称。typeinfo,区分blog还是bbs

分页显示10-20条信息。

select top 10 * from
(select counts, c.url,c.blog_name,c.UserName,c.WebName,c.TypeInfo from(
select count(*) as counts,url from(
select blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from bloginfo where webname='百度') as a group by a.url
) as b,

(select distinct blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from bloginfo where webname='百度') as c where b.url=c.url
)
as d

where d.url not in(
select top 10 c.url from(
select count(*) as counts,url from(
select blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from bloginfo where webname='百度') as a group by a.url
) as b,

(select distinct blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from bloginfo where webname='百度'
) as c
where b.url=c.url order by counts desc
) order by counts desc
上边一段如何优化啊
是不是要创建一个视图啊
请高手给创建一个视图
另外如何在java中应用视图啊
和表的一样吗? 是不是 select * from 视图名称 where webName=“百度”就可以在视图中查询到百度的
记录。
...全文
128 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2009-01-18
  • 打赏
  • 举报
回复
没有你的关系说明及你要实现的, 看不懂关系,无法给出建议。

blog_url按道理应该是唯一的,但从你的语句看不出这一点
oec2003 2009-01-18
  • 打赏
  • 举报
回复
建视图吧
那样写感觉有点乱
Q315054403 2009-01-17
  • 打赏
  • 举报
回复
这个该改进一下结构,以改善效率

firecc05 2009-01-16
  • 打赏
  • 举报
回复
row_number()在sql 2000里没有。
临时表效率低,尤其是表大了以后。
cyz9977 2009-01-16
  • 打赏
  • 举报
回复
使用臨時表,

select identITy(int,1,1)as id , blog_name, blog_url as url ,UserName,WebName ,TypeInfo
into #temp
from bloginfo
where webname='百度'

select blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from #temp id>10 and id<=20
where
cyz9977 2009-01-16
  • 打赏
  • 举报
回复

瘋掉了。。。。


select blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from
(select row_number() over(order by blog_name) as num,blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from bloginfo
where webname='百度')
where num>10 and num<=20


cyz9977 2009-01-16
  • 打赏
  • 举报
回复

瘋掉了。。。。


select blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from
(select row_number() over(order by blog_name) as count,blog_name, blog_url as url ,UserName,WebName ,TypeInfo
from bloginfo
where webname='百度')
where num>10 and num<=20


水族杰纶 2009-01-15
  • 打赏
  • 举报
回复
--TRY
NOT IN -->EXISTS
nalnait 2009-01-14
  • 打赏
  • 举报
回复
not in !!
firecc05 2009-01-14
  • 打赏
  • 举报
回复
你这句子逻辑就有问题了。分页算法也不优化。建议改成这样的:

create view view1 as
select blog_name, blog_url as url ,UserName,WebName ,TypeInfo,count(*) as counts
from bloginfo
where webname='百度'
group by blog_name, blog_url ,UserName,WebName ,TypeInfo
Go

create proc proc1 @FROM int, @TO int as
begin
IF @FROM>@TO or @FROM<0
RETURN -1;

select * FROM
(
select top @TO-@FROM+1 * FROM
(
select top @TO * from view1
order by counts desc
) t1
ORDER BY counts asc
) t2
ORDER BY counts desc
end
dawugui 2009-01-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 binghaizjw 的帖子:]

是不是 select * from 视图名称 where webName=“百度”就可以在视图中查询到百度的记录。[/Quote]

是.

不过是单引号.

select * from 视图名称 where webName = '百度'

or

select * from 视图名称 where webName like '%百度%'

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧