刚刚知道这样的语句:select s_no,s_name from user_s where s_grade in (1,2,3)
还不如下面的三句语句的速度快,在数据量上百万的时候,
select s_no,s_name from user_s where s_grade=1
select s_no,s_name from user_s where s_grade=2
select s_no,s_name from user_s where s_grade=3
想请教一下,第二种方法的的每条查询所得的结果能不能在输出时达到和第一条一样的效果,那么该怎么组合第二种方法结果记录集。谢谢

解决方案 »

  1.   

    以下两种方法能达到你的要求.不需要存储过程
    select s_no,s_name from user_s where s_grade=1 or s_grade=2 or s_grade=3select s_no,s_name from user_s where s_grade=1
    union all
    select s_no,s_name from user_s where s_grade=2
    union all
    select s_no,s_name from user_s where s_grade=3
      

  2.   

    以下两种方法能达到你的要求.不需要存储过程
    select s_no,s_name from user_s where s_grade=1 or s_grade=2 or s_grade=3 order by s_gradeselect s_no,s_name from user_s where s_grade=1
    union all
    select s_no,s_name from user_s where s_grade=2
    union all
    select s_no,s_name from user_s where s_grade=3
      

  3.   

    select s_no,s_name from user_s where s_grade=1
    UNION ALL 
    select s_no,s_name from user_s where s_grade=2
    UNION ALL 
    select s_no,s_name from user_s where s_grade=3
    不知道这样一来效率会怎样呢?
    请楼主说出结果...
      

  4.   

    在s_grade建立索引,然后如下
    select s_no,s_name from user_s where s_grade >=1 and s_grade<=3
    就可以很好的用到索引尽量不要 用or,in之类,如
    select s_no,s_name from user_s where s_grade=1 or s_grade=2 or s_grade=3 
    这样会引起全表扫描。
      

  5.   

    s_grade >=1 and s_grade<=3如果有1.1,1.2之类的就不行了.
      

  6.   

    s_grade 应该是年级吧,年级应该不会出现小数,让楼主自己说说才知道。
      

  7.   

    s_grade >=1 and s_grade<=3
    这个不行的,因为中间可能没有2,也就是后面的数字不一定是连续的,可能是:14,5,8,10
    举这个grade是个例子,不好意思,
    select s_no,s_name from user_s where s_grade=1
    union all
    select s_no,s_name from user_s where s_grade=2
    union all
    select s_no,s_name from user_s where s_grade=3
    这个写法先我试下,谢谢了,
      

  8.   

    对了,这里能不能用exists来实现,有同学告诉我说这个会好点,可我写上这个竟然出错了,
      

  9.   

    再问个问题:
    select s_no,s_name from user_s where s_grade=1
    union all
    select s_no,s_name from user_s where s_grade=2
    union all
    select s_no,s_name from user_s where s_grade=3
    这个是不是比用in要快很多啊,如果数据达到上百万是否也会很快
    再次感谢各位的帮忙,给我一两天时间测试完了就结贴
      

  10.   

    select s_no,s_name from user_s where s_grade=1
    union
    select s_no,s_name from user_s where s_grade=2
    union
    select s_no,s_name from user_s where s_grade=3
    看看行不行
      

  11.   

    用什么东西可以测出SQL语句运行所需要的时间啊?
    哪位教教我```
      

  12.   

    楼主
    纠正一个你的小问题,如果仅仅是作一个你描述的查询,是不需要用存储过程的,存储过程不是这么用的。
    回答你的问题,如果你只需要最简单的方法,并且是百万条的数据,那么,你首先考虑做的是一个基于表的视图,视图在查询方面优点不说了。
    解决你问题的视图在SQL server中是这样:
    ----------------------
    USE DATABASENAME
    CREATE VIEW V_student
    AS
    SELECT s_no,s_name,s_grade
    FROM user_s
    WHERE (s_grade=1) OR
          (s_grade=2) OR
          (s_grade=3)
    ----------------------
    但是建议你考虑查询效率更好的方法;分区视图,但是比较麻烦。
    对的话别忘记给分。谢谢还有楼上的,楼主要求是百万条数据的流量,你让人家加索引,请问你加什么类型的索引,加一个索引你还让不让人添加或者删除数据了,查询是快了,要是添加一条数据呢?如果加顺序索引(查询最快),光数据库排序操作就得四五分钟,硬盘得几万转啊?加上网络的延时和页面返回的时间,还得不算RAID的时间,添加或者删除一条数据最少五分钟以上,索引不是乱加的。不懂咱们别瞎说行不?
      

  13.   

    select s_no,s_name from user_s where s_grade=1
    union
    select s_no,s_name from user_s where s_grade=2
    union
    select s_no,s_name from user_s where s_grade=3
    这个不错的.大数据量时尽量不要用or,in,如果可以的话s_grade建立索引.