search
HomeDatabaseMysql TutorialSQLSERVER2008中CTE的Split与CLR的性能比较 <转>

之前曾有一篇POST是关于用CTE实现Split,这种方法已经比传统的方法高效了。今天我们就这个方法与CLR实现的Split做比较。在CLR实现Split函数的确很简单,dotnet framework本身就有这个function了。 我们新建一个c#-数据库工程,然后建立一个用户自定义函数,Co

 

     之前曾有一篇POST是关于用CTE实现Split,这种方法已经比传统的方法高效了。今天我们就这个方法与CLR实现的Split做比较。在CLR实现Split函数的确很简单,dotnet framework本身就有这个function了。
    我们新建一个c#-数据库工程,然后建立一个用户自定义函数,Code像这样:

<span>   1:  </span>    <span>/// <summary></summary></span>
<span>   2:  </span>    <span>/// SQLs the array.</span>
<span>   3:  </span>    <span>/// </span>
<span>   4:  </span>    <span>/// <param name="str">The STR.</span>
<span>   5:  </span>    <span>/// <param name="delimiter">The delimiter.</span>
<span>   6:  </span>    <span>/// <returns></returns></span>
<span>   7:  </span>    <span>/// 1/8/2010  2:41 PM   author: v-pliu</span>
<span>   8:  </span>    [SqlFunction(Name = <span>"CLR_Split"</span>,
<span>   9:  </span>    FillRowMethodName = <span>"FillRow"</span>,
<span>  10:  </span>    TableDefinition = <span>"id nvarchar(10)"</span>)]
<span>  11:  </span> 
<span>  12:  </span>    <span>public</span> <span>static</span> IEnumerable SqlArray(SqlString str, SqlChars delimiter)
<span>  13:  </span>    {
<span>  14:  </span>        <span>if</span> (delimiter.Length == 0)
<span>  15:  </span>            <span>return</span> <span>new</span> <span>string</span>[1] { str.Value };
<span>  16:  </span>        <span>return</span> str.Value.Split(delimiter[0]);
<span>  17:  </span>    }
<span>  18:  </span> 
<span>  19:  </span>    <span>/// <summary></summary></span>
<span>  20:  </span>    <span>/// Fills the row.</span>
<span>  21:  </span>    <span>/// </span>
<span>  22:  </span>    <span>/// <param name="row">The row.</span>
<span>  23:  </span>    <span>/// <param name="str">The STR.</span>
<span>  24:  </span>    <span>/// 1/8/2010  2:41 PM   author: v-pliu</span>
<span>  25:  </span>    <span>public</span> <span>static</span> <span>void</span> FillRow(<span>object</span> row, <span>out</span> SqlString str)
<span>  26:  </span>    {
<span>  27:  </span>        str = <span>new</span> SqlString((<span>string</span>)row);
<span>  28:  </span>    }


然后编译,部署一切OK后,在SSMS中执行以下测试T-sql:

<span>   1:  </span><span>DECLARE</span> @<span>array</span> <span>VARCHAR</span>(<span>max</span>)
<span>   2:  </span><span>SET</span>  @<span>array</span> = <span>'39,15,93,68,64,43,90,58,39,9,26,26,89,47,91,57,98,16,55,9,63,29,69,16,41,76,34,60,68,64,61,53,32,30,11,72,57,63,36,43,22,14,60,38,24,5,66,26,26,21,22,99,55,18,7,10,46,76,27,88,9,29,89,75,48,72,94,59,35,19,0,35,79,11,87,49,68,30,91,35,9,7,34,47,41,61,98,13,22,1,26,80,35,48,34,92,24,85,90,51'</span>
<span>   3:  </span><span>SELECT</span> id <span>FROM</span> dbo.CLR_Split(@<span>array</span>,<span>','</span>)

 

我们来看它的Client Statistic:

SQLSERVER2008中CTE的Split与CLR的性能比较 <转>

接着我们执行测试T-sql使用相同的array:

<span>   1:  </span><span>DECLARE</span> @<span>array</span> <span>VARCHAR</span>(<span>max</span>)
<span>   2:  </span><span>SET</span>  @<span>array</span> = <span>'39,15,93,68,64,43,90,58,39,9,26,26,89,47,91,57,98,16,55,9,63,29,69,16,41,76,34,60,68,64,61,53,32,30,11,72,57,63,36,43,22,14,60,38,24,5,66,26,26,21,22,99,55,18,7,10,46,76,27,88,9,29,89,75,48,72,94,59,35,19,0,35,79,11,87,49,68,30,91,35,9,7,34,47,41,61,98,13,22,1,26,80,35,48,34,92,24,85,90,51'</span>
<span>   3:  </span><span>SELECT</span> item <span>FROM</span> strToTable(@<span>array</span>,<span>','</span>)

 

CTE实现的Split function的Client statistic:

SQLSERVER2008中CTE的Split与CLR的性能比较 <转>

通过对比,你可以发现CLR的performance略高于CTE方式,原因在于CLR方式有Cache功能,并且把一个复杂的运算放到程序里比DataBase里更加高效。

 

您还可以参考:

Split string in SQL Server 2005+ CLR vs. T-SQL

 

Author:Petter Liu    http://wintersun.cnblogs.com/

希望这篇POST对您有帮助。

出现禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项错误。

解决方法:查询分析器中运行如下代码即可:

exec sp_configure 'show advanced options', '1';
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
go
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
reconfigure;
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
go
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
exec sp_configure 'clr enabled', '1'
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
go
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
reconfigure;
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
exec sp_configure 'show advanced options', '1';
SQLSERVER2008中CTE的Split与CLR的性能比较 <转>
go

 

即可启用
"
配置选项 'show advanced options' 已从 0 更改为 1。请运行 RECONFIGURE 语句进行安装。
配置选项 'clr enabled' 已从 0 更改为 1。请运行 RECONFIGURE 语句进行安装。
配置选项 'show advanced options' 已从 1 更改为 1。请运行 RECONFIGURE 语句进行安装。
"
 

解释:
sp_configure [ [ @configname = ] 'option_name'
      [ , [ @configvalue = ] 'value' ] ]
 

备注
使用 sp_configure 可以显示或更改服务器级别的设置。若要更改数据库级别设置,请使用 ALTER DATABASE。若要更改仅影响当前用户会话的设置,请使用 SET 语句。
更新运行的配置值
为 option 指定新 value 时,结果集的 config_value 列中将显示该值。该值最初与 run_value 列中的值不同,后者显示当前运行的配置值。若要更新 run_value 列中的运行配置值,系统管理员必须运行 RECONFIGURE 或 RECONFIGURE WITH OVERRIDE。
RECONFIGURE 和 RECONFIGURE WITH OVERRIDE 对每个配置选项都有效。但是,基本 RECONFIGURE 语句会拒绝处于合理范围之外或可能导致选项冲突的任何选项值。例如,如果 recovery interval 的值大于 60 分钟,或 affinity mask 的值与 affinity I/O mask 的值重叠,则 RECONFIGURE 会生成错误。与此相反,RECONFIGURE WITH OVERRIDE 则接受具有正确数据类型的任何选项值,并使用指定的值强制进行重新配置。
有些配置选项(例如 affinity mask 和 recovery interval)被指定为高级选项。默认情况下,无法查看和更改这些选项。若要使这些选项可用,请将 Show Advanced Options 配置选项设置为 1。
使用 clr enabled 选项可以指定 Microsoft SQL Server 是否可以运行用户程序集。clr enabled 选项提供下列值。
值 说明
0
 不允许在 SQL Server 上执行程序集。
 
1
 允许在 SQL Server 上执行程序集。
 

clr enabled 选项是一个高级选项。如果使用 sp_configure 系统存储过程来更改该设置,则只有在 show advanced options 设置为 1 时才能更改 clr enabled。该设置在运行 sp_configure 后立即生效。不需要重新启动 SQL Server 实例。
注意:
运行 RECONFIGURE 时,clr enabled 选项的运行值将从 1 改为 0,所有包含用户程序集的应用程序域将立即被卸载

 

http://www.cnblogs.com/wintersun/archive/2010/01/08/1642265.html
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

What are the different types of indexes in MySQL?What are the different types of indexes in MySQL?Apr 25, 2025 am 12:12 AM

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

How do you create an index in MySQL?How do you create an index in MySQL?Apr 25, 2025 am 12:06 AM

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

How does MySQL differ from SQLite?How does MySQL differ from SQLite?Apr 24, 2025 am 12:12 AM

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

What are indexes in MySQL, and how do they improve performance?What are indexes in MySQL, and how do they improve performance?Apr 24, 2025 am 12:09 AM

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Explain how to use transactions in MySQL to ensure data consistency.Explain how to use transactions in MySQL to ensure data consistency.Apr 24, 2025 am 12:09 AM

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)