zoukankan      html  css  js  c++  java
  • SQL Server 2016 ->> T-SQL新特性

    1) TRUNCATE表分区而不是整表

    CREATE TABLE dbo.TruncatePartitionTest 
    (
        PrtCol INT,
        Col2 NVARCHAR(300)
    )
    ON [myPS1](PrtCol)
    GO
    
    INSERT dbo.TruncatePartitionTest 
    VALUES(1,'AAA'), (11,'AAA'), (100,'AAA'), (101,'AAA')
    GO
    
    -- TRUNCATE partitions 1 to 2
    TRUNCATE TABLE dbo.TruncatePartitionTest 
    WITH (PARTITIONS(1 TO 2));
    GO
    
    SELECT * FROM dbo.TruncatePartitionTest 
    GO
    
    TRUNCATE TABLE dbo.TruncatePartitionTest
    GO
    
    INSERT dbo.TruncatePartitionTest 
    VALUES(1,'AAA'), (11,'AAA'), (100,'AAA'), (101,'AAA')
    GO
    
    -- TRUNCATE partition 1
    TRUNCATE TABLE dbo.TruncatePartitionTest 
    WITH (PARTITIONS(1));
    GO
    
    SELECT * FROM dbo.TruncatePartitionTest 
    GO
    
    TRUNCATE TABLE dbo.TruncatePartitionTest
    GO
    
    INSERT dbo.TruncatePartitionTest 
    VALUES(1,'AAA'), (11,'AAA'), (100,'AAA'), (101,'AAA')
    GO
    
    -- TRUNCATE partitions 1 and 2
    TRUNCATE TABLE dbo.TruncatePartitionTest 
    WITH (PARTITIONS(1,2));
    GO
    
    SELECT * FROM dbo.TruncatePartitionTest 
    GO

    2) 新的查询提示NO_PERFORMANCE_SPOOL

    为了试验这个新的查询提示关键字,我对一个查询语句启用和不启用这个查询提示后的IO统计数据进行一个对比。

    添加了NO_PERFORMANCE_SPOOL的语句和IO统计数据

    SET STATISTICS IO ON
    GO
    
    SELECT *
    FROM [dbo].[vwPO]
    WHERE PODATE BETWEEN '2012-01-01' AND '2014-01-31'
    OPTION (NO_PERFORMANCE_SPOOL);  
    (5448 row(s) affected)
    Table 'Workfile'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Address'. Scan count 5, logical reads 49038, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'PurchaseOrder'. Scan count 5, logical reads 9267, physical reads 0, read-ahead reads 0, lob logical reads 17936, lob physical reads 0, lob read-ahead reads 0.

    未添加查询提示

    SET STATISTICS IO ON
    GO
    
    SELECT *
    FROM [dbo].[vwPO]
    WHERE PODATE BETWEEN '2012-01-01' AND '2014-01-31';  
    (5448 row(s) affected)
    Table 'Workfile'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Worktable'. Scan count 1, logical reads 24864, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'tblAddress'. Scan count 5, logical reads 15, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    lob read-ahead reads 0.
    Table 'tblPurchaseOrder'. Scan count 5, logical reads 9267, physical reads 0, read-ahead reads 0, lob logical reads 17936, lob physical reads 0, lob read-ahead reads 0.

    其实最后两者在时间上的差别基本没有,做法上的最大差别也显而易见且可想而知。SPOOL操作符的特点就是创建Work table来供后面的操作符使用。而既然禁用了SPOOL操作符也就意味着不使用Work table,这样上面的IO Statistics结果也反映了这一点。但是并不意味着这是好事。不使用work table意味着后面每次都需要扫描表,这种开销也蛮大的。这个例子里面因为涉及扫描的表小所以差异微乎其微。在我看来,这个功能在现实中的使用场景很少见。只能说有比没有好。本来执行计划的东西非不得已都是让SQL SERVER自己决定。微软宣称这个查询提示可以大大改善高并发场景下使用了SPOOL的查询语句性能。这点我承认。但是,在我看来,这是属于治标不治本的做法。一般使用到SPOOL的语句,很大可能是因为没有适用的索引,所以根本原因很可能是缺乏可用的索引。其次,适用了这个提示可能导致过多的表扫描,性能上未必有提升。真正应该考虑的是审查语句是否存在改写的可能性来降低复杂性从而改变执行计划使得预建的索引得以使用最终获得最优的执行计划,以及是否需要stage数据来解决高并发问题。

    3) DROP IF EXISTS 语句

    以前要DROP某张表某个存储过程总是需要IF EXISTS(SELECT * FROM sys.objects WHERE name = '' AND .....),现在终于有更加简洁的做法来实现。

    DROP  <TABLE|PROCEDURE|VIEW|FUNCTION|TRIGGER> IF EXISTS <name>

    它可以作用于下面这些数据库对象

    AGGREGATE

    PROCEDURE

    TABLE

    ASSEMBLY

    ROLE

    TRIGGER

    VIEW

    RULE

    TYPE

    DATABASE

    SCHEMA

    USER

    DEFAULT

    SECURITY POLICY

    VIEW

    FUNCTION

    SEQUENCE

     

    INDEX

    SYNONYM

     

    4) 在使用DBCC CHECKTABLE和DBCC CHECKDB这种语句的时候可以使用MAXDOP语句来避免对系统性能造成过多的影响,因为如果有业务关键作业正在工作,而服务器的处理器核心数不多,DBCC CHECKTABLE容易导致频繁的上下文切换。

     5)可以使用sp_set_session_context 来设置会话级别的上下文以及使用SESSION_CONTEXT来查看某个会话级别主键的上下文内容。

    EXEC sp_set_session_context 'user_id', 4;  
    SELECT SESSION_CONTEXT(N'user_id');  

    6)sp_execute_external_script 存储过程支持在SQL SERVER 2016中执行R语言脚本以及通过CREATE EXTERNAL RESOURCE POOL来为R语言创建资源池

    7)COMPRESS和DECOMPRESS函数可以使用GZIP压缩算法压缩字符串和使用ZIP算法解压缩字符串

    8)新的函数DATEDIFF_BIGINT和DATEDIFF作用是一样,不过区别是返回值是BIGINT,这样在处理millisecond这样的情况是就可以支持更长的时间差。新的函数AT TIME ZONE则可以输出带有时区的datetimeoffset

    SELECT getdate() AT TIME ZONE 'China Standard Time';  -- from select * from sys.time_zone_info

    9)两个新的字符串函数 STRING_SPLIT和 STRING_ESCAPE。前者是个表函数,输入text和分隔符就可以分割字符串变成表返回。问题是分隔符只支持1个字符长度的字符。而后者是为了帮助json格式的数据转义特殊字符,比如斜杠。

    10)新的数据库选项MIXED_PAGE_ALLOCATION 可以指定数据库是否默认情况下是否首先对索引分配混合分区,SQL SERVER 2016以前的版本某人情况下是分配混合分区先。

  • 相关阅读:
    mybatis绑定错误-- Invalid bound statement (not found)
    JAVA MyBatis配置文件用properties引入外部配置文件
    JAVA错误提示:The operation is not applicable to the current selection.Select a field which is not declared as type variable or a type that declares such fields.
    MySql5.5安装步骤及MySql_Front视图配置
    Failed to load AppCompat ActionBar with unknown error
    android:整理drawable(余下的)(三)
    android:整理drawable(shapdrawable)(二)
    与drawable的较量(一)
    gradle与android studio 关系及gradle配置
    键盘优雅弹出与ios光标乱飘解决方案
  • 原文地址:https://www.cnblogs.com/jenrrychen/p/6083657.html
Copyright © 2011-2022 走看看