接任务,赚取用不完的金币中国教程网
《Photoshop专家讲堂》光盘热售
参与论坛活动,快速赚取金币精品素材,中英文字体
发新话题
打印

二个问题

二个问题


1,select  *  from  A  where  A.a  like  '1234%'  

     select  *  from  A  where  substring(A.a,1,4)  

     上面两个哪个快?  
2,  表studinfo  字段studid,studname  
     表studscore  字段  studid,score  
     如何写出显示全部学生,如果有成绩则列出其成绩(是全部学生的情况)  

---------------------------------------------------------------  

1,select  *  from  A  where  A.a  like  '1234%'  
快  
2,  select  studname,isnull(score,0)  as  score  from  studinfo  a  left  join  studscore  b  on  a.studid=b.studid  
---------------------------------------------------------------  

what's  problem?  

test  as:  
select  *  from  A  where  substring(ltrim(A.a),1,4)='1234'  
or  
select  *  from  A  where  left(ltrim(A.a),4)='1234'  
---------------------------------------------------------------  

你的select  *  from  A  where  substring(A.a,1,4)语句没有写完吧?  
不过我了解你的意思了。  

如果说速度快,理论上是精确查询要比模糊查询快(sql内部处理精确查询和处理模糊查询的算法是不一样的),不过具体的速度要看你的表的设计,索引的建立情况,以及表的大小等等而定,不是一定的,但是能用精确查询条件的语句,就不要用模糊查询,这是优化查询的基本思想。  

第二个问题的答案:  
SELECT  A.studid,  A.studname,  B.score  
FROM  studinfo  A  LEFT  OUTER  JOIN   
         studscore  B  ON  A.studid  =  B.studid  

---------------------------------------------------------------  

感觉上应当是LIKE慢而SUBSRING快,LIKE终究是查找+比较,而SUBSTRING仅仅是截取+比较很显然LIKE要慢一点,甚至可以举个极端的例子,如果A字段非常非常大,比如有1000个字母的大小,LIKE势必要扫描这1000个字符,而SUBSTRING则不用。  
使用服务器数据库的时候,网络资源往往是瓶井,是不是你LIKE和SUBSTRING查询出来的数据量不一样,所以造成的明显差异?  
但总的来说除非非常极端的情况LIKE和SUBSTRING如果查询出来的数据是相同的,那么他们的速度应该相差无几  
---------------------------------------------------------------  

PLS  TRY:  
SET  SHOWPLAN_ALL  ON  
GO  
select  *  from  A  where  A.a  like  '1234%'  
select  *  from  A  where  substring(A.a,1,4)  =  '1234'  
GO  
SET  SHOWPLAN_ALL  OFF  
你可以比较一下,EstimateRows  和  TotalSubtreeCost  的值,数值小的比较好.
发新话题 返回列表