select  top 5 * from shelf 
where Sortid=377
 and Sortid=463
 and Sortid=438 
 and Sortid=646 
order by sortid

解决方案 »

  1.   

    一楼的确实错误,同意 paoluo(一天到晚游泳的鱼),在语句上可能唯一可以优化的也就是*了,楼主再想优化可以从索引和服务器方面着手了
      

  2.   

    请问 bugchen888(臭虫) 对*进行优化可以提高多快的速度啊!
      

  3.   

    如果你有10个字段Select COl1,COl2 from TableName
    肯定比
    Select * from TableName要快,具体快多少,不能一概而定的。
      

  4.   

    优化,那就从语句间接,速度快来讲吧,你真应该好好看看原来的那个帖子,
    回复人: changechange(http://access911.net 是我的个人网站,欢迎光临) ( ) 信誉:378 
    不是给你一个参考页面吗?
    http://access911.net/fixhtm/72FAB31E10DCEEF3.htm
    我看了一下,收获不小,按照你说的sortid,我按照参考页面说的表结构,并加多了一个sort id
    --888.如下
    create table #t (aid int identity(2,1),a_desc varchar(10))insert into #t values('11')
    insert into #t values('22')
    select ident_current('#t')
    select @@identityselect ident_incr('#t')
    select ident_seed('#t')create table #tt (id varchar(10),rid varchar(10))insert into #tt values('001','377')
    insert into #tt values('002','377')
    insert into #tt values('003','377')
    insert into #tt values('011','377')
    insert into #tt values('012','377')
    insert into #tt values('013','377')
    insert into #tt values('004','463')
    insert into #tt values('014','463')
    insert into #tt values('015','463')
    insert into #tt values('016','463')
    insert into #tt values('005','463')
    insert into #tt values('006','463')
    insert into #tt values('007','438')
    insert into #tt values('008','438')
    insert into #tt values('017','438')
    insert into #tt values('018','438')
    insert into #tt values('019','438')
    insert into #tt values('009','646')
    insert into #tt values('010','646')
    insert into #tt values('020','646')
    insert into #tt values('021','646')
    insert into #tt values('022','646')
    insert into #tt values('023','646')
    insert into #tt values('024','646')
    insert into #tt values('025','888')
    insert into #tt values('026','888')
    insert into #tt values('027','888')
    select * from #tt
    /*
    id         rid        
    ---------- ---------- 
    001        377
    002        377
    003        377
    011        377
    012        377
    013        377
    004        463
    014        463
    015        463
    016        463
    005        463
    006        463
    007        438
    008        438
    017        438
    018        438
    019        438
    009        646
    010        646
    020        646
    021        646
    022        646
    023        646
    024        646
    025        888
    026        888
    027        888(27 row(s) affected)
    */select * 
    from #tt as a
    --where id in(select top 5 id from #tt where rid=a.rid order by id)
    where exists  (select top 1 id  from (select top 5 id from #tt where rid=a.rid order by id) b 
    where b.id = a.id)
    and  a.rid in('377','463','438','646')--这句或许你不需要,你可能也是按照sortid取出各自的前5笔,按照参考页面的回答人说的where id in(select top 5 id from #tt where rid=a.rid order by id),数据量大的时候,很慢,如果转化为exists会快点,具体快多少,那与很多因素有关,不过同样的环境就是比in要快得多,后面加的and  a.rid in('377','463','438','646')--速度不慢,因为直接比较不需要去表中整个搜索,速度也会加分,感谢changechange提供的东西,好像也到了好多东西~