博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQLSERVER CLR无序自增(支持并发)性能对比
阅读量:7115 次
发布时间:2019-06-28

本文共 2376 字,大约阅读时间需要 7 分钟。

CLR函数脚本

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

using System;

using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;

/// <summary>

/// 用户自定CLR 函数,用来生成一个序列
/// </summary>

public partial class test

{
    
    public static Hashtable ht=new Hashtable(); //创建一个Hashtable实例
 
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlInt64 GetRowNum(SqlGuid Guid1)
    {
        try
        {
            if (!ht.Contains(Guid1)) //判断哈希表是否包含特定键,其返回值为true或false
            {
                ht[Guid1] = 0;
            }
            Int64 i = Convert.ToInt64(ht[Guid1].ToString());
            i++;
            ht[Guid1] = i.ToString();
            return i;

        }

        catch
        {
            return -1;
        }
    }

    /// <summary>

    /// 删除哈希表值
    /// </summary>
    /// <param name="Guid1"></param>
    [Microsoft.SqlServer.Server.SqlProcedure ]
    public static void ClearGuid(SqlGuid Guid1)
    {
        try
        {                
            ht.Remove(Guid1);
            return ;
        }
        catch
        {

        }

    }
};

 

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

启动CLR

exec sp_configure 'show advanced options','1';

go
reconfigure  WITH OVERRIDE
go
exec sp_configure 'clr enabled','1'
go
reconfigure WITH OVERRIDE 

ALTER DATABASE   test SET TRUSTWORTHY On

-----------------------------------------------------------------------
drop FUNCTION  GetRowNum
drop proc ClearGuid

drop ASSEMBLY testss

CREATE ASSEMBLY testss FROM 'C:\Documents and Settings\Administrator\桌面\test\SqlServerProject1\SqlServerProject1\bin\Debug\SqlServerProject1.dll' WITH PERMISSION_SET = UnSAFE;

 

go

创建函数

CREATE FUNCTION dbo.GetRowNum
(
 @Guid uniqueidentifier
)
RETURNS bigint
AS EXTERNAL NAME
  testss.test.GetRowNum

 

CREATE proc ClearGuid

(
  @Guid uniqueidentifier
)
AS EXTERNAL NAME
  testss.test.ClearGuid

 

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

 

GetRowNum与identity对比

 

declare @Guid uniqueidentifier

set @Guid=newid()

select dbo.GetRowNum(@Guid)

  from list

 

--清理哈希表

exec ClearGuid  @Guid

 

select identity(int ,1,1) as id into #t  from list

 
select id  from #t
drop table #t

数据量943761

 

时间匆忙,经多次测试,初步结果

1.GetRowNum(5591)消耗时间比identity(6278)少近700毫秒

2.GetRowNum(2171)的CPU比identity(922)高很多

3.GetRowNum(2352)读是identity(4828)的一半

4.GetRowNum没有写入,identity的写入是1519

 

初步结论

1.GetRowNum消耗更多的CPU,进行运算

2.GetRowNum的磁盘消耗比identity低,而且是没有写入的

3.如果表已经有自增(不连续),identity就不能用,GetRowNum可以

4.GetRowNum瞬间返回数据,identity要停顿

 

......................

转载地址:http://dgghl.baihongyu.com/

你可能感兴趣的文章
httpclient,java跨系统调用,第三接口调用实例
查看>>
Objective-C征途:BOOL Party
查看>>
mysql c-api 预处理语句
查看>>
functions 示例
查看>>
C#编译基础知识(三)
查看>>
插入订单并且输出订单号的sql存储过程
查看>>
虚拟Ip技术如何实现备机容灾
查看>>
windows快捷启动命令
查看>>
Python几个算法实现
查看>>
about selection and range
查看>>
linux性能系列--内存
查看>>
php开发中的一些常用统计的日期
查看>>
洛谷 3951 小凯的疑惑——找规律/数学
查看>>
bzoj 4300 绝世好题——DP
查看>>
Jquery小记
查看>>
SQL Server 2008连接字符串写法大全
查看>>
序列模型(4)----门控循环单元(GRU)
查看>>
Python画三维图-----插值平滑数据
查看>>
librbd 分析
查看>>
sql里的in对应linq的写法 及 IQueryable转化为Dictionary
查看>>