二个问题

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 的值,数值小的比较好.

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus