网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > SQL语法 > 正文

提高SQL性能

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

【网学网提醒】:本文主要为网学会员提供提高SQL性能,希望对需要提高SQL性能网友有所帮助,学习一下!


    1:SQL(StructuredQuaryLanguage)特性:
    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语句把过滤好的数据插入到worktable中
    having语句从worktable中对数据进行在过滤以得到最后的结果。
    6:一个select语句的执行顺序
    a:fromclause
    b:whereclause
    c:groupbyclause
    d:selectclause
    e:havingclause
    f:orderbyclause
    7:UnionVSUnionAll
    a:Union会把两个结果集排序,并且除去重复的元素(效率差,轻易不要用)
    b:UnionAll仅仅是把两个结果集合并,没有排序,也不去除重复元素(效率好)
    二:索引和查询参数
    1:ASE中有三种access数据方式
    a:clusteredIndex
    b:nonclusteredIndex
    c:tablescan
    2:CoveredQuery
    一个CoveredQuery仅仅从索引中得到数据,不用去扫描数据库表,这是最快的数据查询方式。
    限制1:只能在selece中生效
    限制2:所有被引用的列必须在同一个nonclusteredindex中
    3:functionalindex
    在ASE15.0以后才被支持,也就是说在ASE15.0以前的版本,下列语句是可定不会用上索引的
    sql代码
    
    selectcolumn1
    fromtable1
    whereupper(column2)='IVANL'
    4:如何查看执行计划
    sql代码
    
    setshowplanon
    go
    yoursql
    go
    setshowplanoff
    go
    5:如何查看IO
    sql代码
    
    setstatisticsioon
    setstatisticstimeon
    go
    yousql
    go
    setstatisticsiooff
    setstatisticstimeoff
    go
    6:使用Index的建议
    a:使用那些经常在where语句中使用的字段做index
    b:使index中包含的字段越少越好
    c:drop掉没用的index
    三:表连接
    1:什么是表连接
    表连接是从多表中查询数据,或者是从一个表中多次取数据。
    (AjoinisaTransanct-SQLoperationthanaccessrowsfrommulti-tablesorfromasingletalbemulti-times)
    2:表连接的类别
    a:innerjoin
    b:outerjoin
    c:crossjoin(fulljoin)
    3:ASE中不支持fulljoin但是通过union可以模拟fulljoin
    sql代码
    
    selectt1.colu1,t2.column2
    fromt1,t2
    wheret1.id*=t2.id
    union
    selectt1.colu1,t2.column2
    fromt1,t2
    wheret1.id=*t2.id
    (不建议使用,效率很差)
    4:ASE中最多支持50个table做表连接,ASE的查询优化器做的不是很好,Sybase推荐join表不超过4个(-_-~!)
    5:数据库中有三种方式来实现表连接
    a:nestedloopjoin
    b:mergejoin
    c:hashjoin
    (可以使用showplan来查看数据库选用哪种join来实现join语句)
    6:对表连接的建议:
    a:用showplan看使用了那种
    用join方式
    b:在join的列上加Index
    c:把多表的join才分成几个小表的join
    d:避免产生笛卡儿积
    四:使用Case语句
    1:case语句的两种形式
    sql代码
    
    a:
    case
    whensearch_conditionthenexpression
    [whensearch_conditionthenexpression]
    [elseexproestion]
    end
    b:
    caseexpression
    whenexpressionthenexpression
    [whenexproessionthenexpression]
    [elseexpression]
    end
    2:case的用途
    a:decodingcolumn
    sql代码
    
    selectcust_id,cust_name
    casecust_type
    when'R'then'Relation'
    when'I'then'International'
    when's'then'Small'
    else'Other'
    endascustomer_type
    b:conditionallydisplayingcolumnsorvalues
    sql代码
    
    selecttitle_id,total_sales,
    case
    whentotal_sales>5000then'hight'
    whentotal_sales<100then'low'
    else''
    endas'column'
    c:horizontalfrequencytableandsummarycalculation
    sql代码
    
    selectsum(casetypewhen'adv'then1else0end)asadv
    ,sum(casetypewhen'cus'then1else0end)ascus
    fromcustomer
    d:updatingonvariableconditions
    sql代码
    
    updatecustomer
    setcust_charge=cust_charte+casecust_type
    when'd'then1
    when'c'then2
    when'e'then3
    else0
    end
    [/code]
    e:rulesandcheckconstraints
    [code]
    createtablecust_order_info
    (
    order_numint,
    order_takerint,
    order_datechar(7)default
    case
    whendatepart(dw,getDate())between2and6then'weekday'
    else'weekend'
    end
    )
    五:事务和锁
    1:ASE中有两种事务模式
    a:ChainedMode
    b:unChainedMode(Sybase默认)
    unchainedmode显示的开始一个事务,chained隐式的开始一个事务
    unchainedmode使用'comminttran','rollbacktran'
    chainedmode使用'commintwork','rollbackwork'
    unchainedmode支持嵌套事务,chainedmode不支持
    2:Lockingschema
    a:Allpagestable,willlockdataandindexastheyareaccessed(可以有clusteredindex)
    b:ADatapagestablewilllockdatpagesastheyareaccessed,indexwillnotbelocked(无clusteredindex)
    c:ADataRowtablewilllockdatpagesastheyareaccessed,indexwillnotbelocked(无clusteredindex)
    3:Lockingtype
    ASE中最重要的三种locktype是
    a:sharedlocks(select,fetch)
    b:updatelocks(fetch,update,delete)
    c:exclusivelocks(insert,update,delete)
    4:隔离级别
    ASE中一共有四种隔离级别
    a:isolationlevel0(readuncommited),允许胀读
    b:isolationlevel1(readcomminted)(ASEDEFAULT),不允许胀读
    c:isolationlevel2(repeatableread),可重复读
    d:isolationlevel3(serializable),不允许幻影读
    sql代码
    
    settransactionisolationlevel{0|1|2|3}
    or
    select...
    atisolation{0|1|2|3}
    5:如何编写高效的transaction
    ForOLTPtransaction
    a:使transaction
    尽可能的短
    b:使用index来随机访问数据
    c:只有在必要的时候才使用transaction
    d:选取合适的Locktype和隔离级别
    e:使用乐观锁
    六:数据处理
    1:除以0
    使用coalesce()和nullif()
    先使用nullif()把0转换成null,在用coalesce()处理null的情况
    sql代码
    
    selectcoalesce(total_sales/nullif(sales,0),0)
    --coalesce(ex1,ex2,ex3...)返回第一个不是Null的表达式
    --nullif(expre,value)如果expre=value,则返回null
    2:找到重复的数据
    sql代码
    
    selecttype,count(*)
    fromtable
    where..
    groupbytype
    havingcount(*)>1
    3:找出重复次数最多的数据
    sql代码
    
    selecttype,count(*)
    fromtable
    where..
    groupbytype
    havingcount(*)=max(count(*))
    4:数据累加
    java代码
    selectt1.title_id,t1.advice,sum(t2.advice)ascumulative_total
    fromtitlet1,titlet2
    wheret1.title_id>=t2.title_id
    groupbyt1.title_id,t1.advice
    5:rankingdata
    sql代码
    
    selectrank=identity(10),title_id,total_sales
    into#topfromtitles
    where..
    orderbytotal_salesdesc
    go
    select*from#top
    go
    droptable#top
    go
    6:converbetweenjulianDateandgregoriandate
    sql代码
    
    selectdatepart(yy,@date)*1000+datepart(dy,@date)asjulina_date
    selectdateadd(dd,juliandate%1000,'12/31/'+convert(char(4),juliandate/1000-1))asgregorian_date
    7:计算本月有多少天
    sql代码
    
    datepart(dd,
    dateadd(dd,-1--lastdayofthismonth
    datead(mm,1--addamonth
    dateadd(dd--
    ,
    1-datepart(dd,getdate()--1-today
    getDate()))))--gettoday
    8:是否是闰年
    sql代码
    
    selectdatepart(dy,'03/01/'||convert(char(4),datepart(yy,getdate())))
    --=61是闰年
    --=60不是闰年
    
  • 上一篇资讯: 收藏的SQLServer技巧
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师