一:SQL Bisic
◆1:SQL(Structured Quary Language)特性:
a:标准化
b:非过程化的
c:可优化的
d:面向集合操作的
◆2:ASE中的数据类型
a:Numberic
b:Character
c:Date/Time
d:Lobs
◆3: convert(varchar, textColumn),如果不指定varchar(n)n那么默认是30
◆4:where 在sql中的作用
a:过滤数据
b:做表连接(sql92以前)
c:选择索引
◆5:whare 和 having的区别
where语句把过滤好的数据插入到work table中
having语句从work table中对数据进行在过滤以得到最后的结果。
◆6:一个select语句的执行顺序
a:from clause
b:where clause
c:group by clause
d:select clause
e:having clause
f:order by clause
◆7:Union VS Union All
a:Union 会把两个结果集排序,并且除去重复的元素(效率差,轻易不要用)
b:Union All仅仅是把两个结果集合并,没有排序,也不去除重复元素(效率好)
二:索引和查询参数
◆1:ASE中有三种access数据方式
a:clustered Index
b:nonclustered Index
c:table scan
◆2:Covered Query
一个Covered Query 仅仅从索引中得到数据,不用去扫描数据库表,这是最快的数据查询方式。
限制1:只能在selece中生效
限制2:所有被引用的列必须在同一个nonclustered index中
◆3:functional index
在ASE15.0以后才被支持,也就是说在ASE15.0以前的版本,下列语句是可定不会用上索引的
sql 代码
select column1
from table1
where upper(column2) = 'IVANL'
◆4:如何查看执行计划
sql 代码
set showplan on
go
your sql
go
set showplan off
go
◆5: 如何查看IO
sql 代码
set statistics io on
set statistics time on
go
you sql
go
set statistics io off
set statistics time off
go
◆6:使用Index的建议
a:使用那些经常在where语句中使用的字段做index
b:使index中包含的字段越少越好
c:drop掉没用的index
三:表连接
◆1:什么是表连接
表连接是从多表中查询数据,或者是从一个表中多次取数据。
(A join is a Transanct-SQL operation than access rows from multi-tables or from a single talbe multi-times)
◆2:表连接的类别
a:inner join
b:outer join
c:cross join(full join)
◆3:ASE中不支持full join但是通过union可以模拟full join
sql 代码
select t1.colu1, t2.column2
from t1, t2
where t1.id *= t2.id
union
select t1.colu1, t2.column2
from t1, t2
where t1.id =* t2.id
(不建议使用,效率很差)
◆4:ASE中最多支持50个table做表连接,ASE的查询优化器做的不是很好,Sybase推荐join表不超过4个
◆5:数据库中有三种方式来实现表连接
a:nested loop join
b:merge join
c:hash join
(可以使用show plan来查看数据库选用哪种join来实现join语句)
◆6:对表连接的建议:
a:用showplan 看使用了那种用join方式
b:在join的列上加Index
c:把多表的join才分成几个小表的join
d:避免产生笛卡儿积
四:使用Case语句
◆1:case语句的两种形式
sql 代码
a:
case
when search_condition then expression
[when search_condition then expression]
[else exproestion]
end
b:
case expression
when expression then expression
[when exproession then expression]
[else expression]
end
◆2:case的用途
a:decoding column
sql 代码
select cust_id, cust_name