刚刚知道这样的语句: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
想请教一下,第二种方法的的每条查询所得的结果能不能在输出时达到和第一条一样的效果,那么该怎么组合第二种方法结果记录集。谢谢
还不如下面的三句语句的速度快,在数据量上百万的时候,
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
想请教一下,第二种方法的的每条查询所得的结果能不能在输出时达到和第一条一样的效果,那么该怎么组合第二种方法结果记录集。谢谢
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
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
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
不知道这样一来效率会怎样呢?
请楼主说出结果...
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
这样会引起全表扫描。
这个不行的,因为中间可能没有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
这个写法先我试下,谢谢了,
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要快很多啊,如果数据达到上百万是否也会很快
再次感谢各位的帮忙,给我一两天时间测试完了就结贴
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
看看行不行
哪位教教我```
纠正一个你的小问题,如果仅仅是作一个你描述的查询,是不需要用存储过程的,存储过程不是这么用的。
回答你的问题,如果你只需要最简单的方法,并且是百万条的数据,那么,你首先考虑做的是一个基于表的视图,视图在查询方面优点不说了。
解决你问题的视图在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的时间,添加或者删除一条数据最少五分钟以上,索引不是乱加的。不懂咱们别瞎说行不?
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建立索引.