之前曾有一篇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:
接着我们执行测试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:
通过对比,你可以发现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';go
reconfigure;
go
exec sp_configure 'clr enabled', '1'
go
reconfigure;
exec sp_configure 'show advanced options', '1';
go
即可启用 解释: 备注 clr enabled 选项是一个高级选项。如果使用 sp_configure 系统存储过程来更改该设置,则只有在 show advanced options 设置为 1 时才能更改 clr enabled。该设置在运行 sp_configure 后立即生效。不需要重新启动 SQL Server 实例。
"
配置选项 '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 上执行程序集。
注意:
运行 RECONFIGURE 时,clr enabled 选项的运行值将从 1 改为 0,所有包含用户程序集的应用程序域将立即被卸载

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.

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.

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.

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.

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

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.

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

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

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 latest version

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
God-level code editing software (SublimeText3)
