+ All Categories
Home > Documents > SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect &...

SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect &...

Date post: 22-May-2020
Category:
Upload: others
View: 8 times
Download: 1 times
Share this document with a friend
41
Disclaimer Ronen Ariely, Senior consultant & architect, http://ariely.info This presentation created for personal use only! All data and information provided, is for informational and accessories purposes, as part of lecture, given by Ronen Ariely . Any use of the presentation, the mentioned codes, or it's content, will be at your own risk! Authorization to download this presentation is intended for personal use only, and does not include authorization to distribute the presentation in any way! Feel free to send insights, comments and feedback, follow my publications, and announcements, on my Facebook homepage, or at my website. http ://ariely.info/ https ://www.facebook.com/ronen.ariely
Transcript
Page 1: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Disclaimer

Ronen Ariely, Senior consultant & architect, http://ariely.info

This presentation created for personal use only!

All data and information provided, is for informational and accessories

purposes, as part of lecture, given by Ronen Ariely.

Any use of the presentation, the mentioned codes, or it's content, will be

at your own risk!

Authorization to download this presentation is intended for personal use

only, and does not include authorization to distribute the presentation in

any way!

Feel free to send insights, comments and feedback, follow my publications,

and announcements, on my Facebook homepage, or at my website.

http://ariely.info/

https://www.facebook.com/ronen.ariely

Page 2: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Abstract2005, Big buzz…. Microsoft added a NEW FEATURE to the SQL Server,

which allow users to extend the Server abilities, by importing external

CLR libraries. Hundreds of articles and discussions were held around the

same time, asking: is Transact-SQL dead? Today we are celebrating

Tenth Birthday to CLR feature, and it is best time to open the discussion

and get some insights regarding this OLD FEATURE.

In this session we are going to focus on the questions WHY to use

SQLCLR, and not less important WHEN to use it over transact-SQL. We

are not going to talk about HOW to use it. We are going to demonstrate

the SQLCLR abilities in practice (all code will be publish latter), we are

going to compare different solutions, explain why you have to use it, and

show that in practice we all using SQLCLR, yet most DBAs do not write

their own code. As part of the demonstrations, we will show how to

implement new features from SQL server 2012 / 2014 on older versions.

Page 3: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more
Page 4: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

SQLCLR From 2005 to 2015Not how, but why and when

Ronen Ariely,

Senior consultant & architect

#360 | Israel 2015, January 19

SQLCLR

Page 5: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

About, Ronen Ariely (Blog, website, Facebook)

Ronen Ariely, Senior consultant & architect, http://ariely.info

>> Senior consultant, architect & professional programmer,

architecting SQL & BI enterprise level solutions, with more

than 15 years of experience in variety of programming

languages and technologies, leading and managing

development teams and BI projects.

>> graduate BA in Physics and continued MA studies by specialization in

Geo-physics and seismic data analysis, at the Tel-Aviv University.

>> Active in communities in the field of Programming, SQL Server, T-SQL,

under the nickname “pitoach” or “pituach”.

>> Serve several years as Moderator at MSDN Israel communities, and at

the MSDN Global communities.

>> Writing technical blogs and Microsoft TechNet articles, and serve as one

of the leaders of the TechNetWIKI global communities.

Page 6: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Agenda

Ronen Ariely, Senior consultant & architect, http://ariely.info

We are going to show the power of SQLCLR.

We will focus on the questions WHY to use SQLCLR, and not less

important WHEN to use it, over transact-SQL. We are not going to

talk about HOW to use it.

We are going to demonstrate the SQLCLR abilities in practice,

and compare different solutions, explain why you have to use it,

and show that in practice we all using SQLCLR, yet most DBAs do

not write their own code.

As part of the demonstrations, we will show how to implement

new features from SQL server 2012 / 2014 on older versions.

Page 7: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Target Audience

Ronen Ariely, Senior consultant & architect, http://ariely.info

Session Level: Intermediate

Everyone can enjoy the demos :-), in order to get the

idea, of why we need to use SQL CLR.

The implementation of some of the demonstrations,

might required a bit more knowledge and preparation

Page 8: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

SQLCLR, 2005

Ronen Ariely, Senior consultant & architect, http://ariely.info

SQL Server 2005, Big buzz !!!

NEW FEATURE, which allow users to extend the Server

abilities, by importing external CLR libraries.

Hundreds of articles and discussions were held around

the same time, asking: is Transact-SQL dead? Today we

are celebrating Tenth Birthday to CLR feature, and it is

best time to open the discussion and get some insights

regarding this OLD FEATURE.

Page 9: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Dot.Net

Assemblies.Net

Classes

SQL

Server

Engine

User

Defined

Function

TriggerStored

Procedure

Agrgreg

ate

What can we do?

Ronen Ariely, Senior consultant & architect, http://ariely.info

Page 10: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Dot.Net

Assemblies.Net

Classes

SQL

Server

Engine

User

Defined

Function

TriggerStored

Procedure

Agrgreg

ate

What can we do?

Ronen Ariely, Senior consultant & architect, http://ariely.info

Page 11: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Ronen Ariely,

Senior consultant & architect

Is T-SQL Dead ?!?

Is T-SQL Dead ?!?

Ronen Ariely, Senior consultant & architect, http://ariely.info

Page 12: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Ronen Ariely,

Senior consultant & architect

Today inCLR's tenth birthdaywe can say thatthe answer was NO

Is T-SQL Dead ?!?

Ronen Ariely, Senior consultant & architect, http://ariely.info

Page 13: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Best Security: Never turn on your machine!

If you turned your machine + you posted your

Administrator password on the network, then please

don’t talk about bad security!

With great power,

there must also come…

great responsibility!

* Peter Parker's

(Spider-Man)

moral lesson

Security

Ronen Ariely, Senior consultant & architect, http://ariely.info

Page 14: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Accessories Database

Ronen Ariely, Senior consultant & architect, http://ariely.info

It is recommended to use Accessories Database!

Tables: Numbers table, Dates table, etc.

Functions: Split, Un-Split, Random String, Regular

Expressions, etc.

Management and comfortable work, in one place

Fast to move from one server to other

No need for duplicate elements (Code re-use)

Security! Easy to control and manage.

READ_ONLY

Faster to use

Additional protection layer

Page 15: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Split String, function

Ronen Ariely, Senior consultant & architect, http://ariely.info

Returns table with

substrings, that are

delimited by specified

string.

Split( ‘a@b@c@d’ , ’@’)

What can We use it for?

a

b

c

d

>> passing multiple parameters.

>> Formatting value

>> Instead of Dynamic Query

'where x In(' + '1,2,3' + ')'

where x In ( select Item from Split('1,2,3') )

>> Return specific Item from list

Page 16: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Split String, function

Ronen Ariely, Senior consultant & architect, http://ariely.info

Play Time

SQLCLR String Splitting

http://sqlblog.com/blogs/adam_

machanic/archive/2009/04/28/s

qlclr-string-splitting-part-2-even-

faster-even-more-scalable.aspx

Arrays and Lists in SQL Server

http://www.sommarskog.se/arra

ys-in-sql-2005.html

Page 17: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Split String, function

Ronen Ariely, Senior consultant & architect, http://ariely.info

http://sqlperformance.com/2012/07/t-sql-queries/split-strings

Resources & Time

Page 18: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Split String, function

Ronen Ariely, Senior consultant & architect, http://ariely.info

Flexibility

By using XML's solution, we are bound by rigid format, or we have to

prepare the text firstly, which that mean, we need to use more resources.

Page 19: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Random String, function

Ronen Ariely, Senior consultant & architect, http://ariely.info

T-SQL: Random String (compare options)http://social.technet.microsoft.com/wiki/contents/articles/21196.t-sql-

random-string.aspx

SQLCLR: Create Random Stringhttp://social.technet.microsoft.com/wiki/contents/articles/21219.sql-

server-create-random-string-using-clr.aspx

What can We use it for?>> Generate Password

>> Fill Testing tables

>> Default values

Page 20: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Random String, function

Ronen Ariely, Senior consultant & architect, http://ariely.info

Play Time

Page 21: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Un-Split function, (Aggregate function)

Ronen Ariely, Senior consultant & architect, http://ariely.info

CLR much faster and less resources

CLR much more Flexible, no need to format text.

CLR much simpler to read, write and understand

Using CLR we can create a new aggregate function!

Page 22: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

חידה

Ronen Ariely, Senior consultant & architect, http://ariely.info

סרטיםשלושהבדיוקלבחורצריךמשתמש

שרתלמועבריםהנתונים.אוהבהכישהוא

.פרוצדורהאל,פסיקבהפרדתכשרשרת

השרשרתאתלנתחעלינוSQLהבשרת

אתלמצואהיא,היאשלנוהמטרה.שהגיעה

רוציםאנחנו.בלבדוהשלישיהשניהנתון.והשלישי שהגיעולהציג ולהכניס למסד הנתונים רק את המספר השני

?נעזר( וטכנולוגיה)באיזה פונקציה

Page 23: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Regular Expression, functions

Ronen Ariely, Senior consultant & architect, http://ariely.info

What can We use it for? Functions:

1) Match

2) Matches

3) Group

4) Groups

5) Replace

>> Check if value Mach Expression

>> Get all maches in string

>> Split using several seperators

>> Return a matching expression in string

>> Return all matching expression in string

>> Format Dates / Time

>> Extract alphanumeric characters from a string

Page 24: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Implementing new types

Ronen Ariely, Senior consultant & architect, http://ariely.info

Rational number is any number that can be expressed as

the quotient or fraction p/q of two integers, p and q, with the

denominator q not equal to zero.

Complex number is a

number that can be expressed in

the form a + bi, where a and b are

real numbers and i is the

imaginary unit, that satisfies the

equation i = root of -1

Page 25: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Implementing new types

Ronen Ariely, Senior consultant & architect, http://ariely.info

Vectors and Matrices

Page 26: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Special types

Ronen Ariely, Senior consultant & architect, http://ariely.info

SQL Server 2008 helps to address this issue with new spatial data types.

The two spatial data types, GEOGRAPHY and GEOMETRY, allow

developers to store location-specific data directly into the database

without having to break those data elements down into formats that fit

other standard data types.

The geography spatial data type, geography, is implemented as a .NET

common language runtime (CLR) data type in SQL Server. This type

represents data in a round-earth coordinate system. The SQL

Server geography data type stores ellipsoidal (round-earth) data, such as

GPS latitude and longitude coordinates.

We can get a full implementation of those types for any SQL Server from

2005, using simple CLR function!

http://msftengprodsamples.codeplex.com/

Page 27: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

XML parser

Ronen Ariely, Senior consultant & architect, http://ariely.info

XML to Table / Table to XML

Page 28: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Working with Files / Folders

Ronen Ariely, Senior consultant & architect, http://ariely.info

xp_cmdshel vs CLR

Page 29: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Question

Ronen Ariely, Senior consultant & architect, http://ariely.info

Can you guess what version of SQL Server I am using? & and what's

unique in the table, that leads us to insert data so fast, and with no disk

activity?

In the following example, I insert records into

database table elements.

1. In the first table, the insertion speed is

many times faster than in the second table.

2. When inserting to the first table element,

we see no disk activity!

Page 30: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

In-Memory Elements

Ronen Ariely, Senior consultant & architect, http://ariely.info

WHY?

Temp Logs

Disk Activity vs Memory

Collect data for bulk insert

Memory queue

Temp Counters

Fast Updates

Page 31: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

In-Memory Elements

Ronen Ariely, Senior consultant & architect, http://ariely.info

SQL Server 2005 EXPRESS and above

Page 32: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

In-Memory Elements

Ronen Ariely, Senior consultant & architect, http://ariely.info

Page 33: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

JSON (JavaScript Object Notation)

Ronen Ariely, Senior consultant & architect, http://ariely.info

>> Convert relational data to hierarchical data and vice versa.

>> Convert Objet into text and vice versa.

>> Serialize data in DataTable to a JSON formatted string. Deserialize

JSON formatted text into DataTable object.

Why JSON? lightweight data transmission. Character count (excluding

whitespace) is about 26% reduction in overhead comparing to XML!

How? There are several JSON parsing frameworks available for .NET.

This code based on JSON.Net CodePlex project. Json.NET is a personal

open source project, Started in 2006.

https://json.codeplex.com/

Page 34: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

JSON (JavaScript Object Notation)

Ronen Ariely, Senior consultant & architect, http://ariely.info

Supporting JSONNoSQL databases like Mongodb, CouchBase, Riak, RavenDB, etc.

allready have good support for JSON and the important querying

and parsing of JSON structures.

SQL Server have no native support!

Performance: >> Generally you can bulk load 100K JSON records to 100K table

rows in about 5 seconds.

JSON SQL:http://www.sqlservercentral.com/articles/SQLCLR/74160/

Page 35: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

JSON in 1 min:

Ronen Ariely, Senior consultant & architect, http://ariely.info

basic structure:

<Element's Name>: < Element Value>

* name is not a must!

There are 3 types of elements:

Object - mark with {}

Array - mark with [] , speared with comma

String -

[

{

"Column1": 14

},

{

"Column1": 1,

"Column2": 2,

"Column3": "3"

}

]

Page 36: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Conclusions and summary

Ronen Ariely, Senior consultant & architect, http://ariely.info

Why to Use SQLCLR? Resources

Execution Time

Flexibility

When It is the only option

When to use CLR?!?

Page 37: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Conclusions and summary

Ronen Ariely, Senior consultant & architect, http://ariely.info

When to use CLR? Those are golden rules!

and you should always check and compare!

Tabular databases adjusted to work with SETs.

Actions between tables, like JOIN, usually have best

performance using T-SQL.

Tabular databases do not perform well, on parsing values,

in most cases.

If there is a built-in solution, then there is a good chance

that is was developed well, and it have better

performance than what we can develop in short time.

Page 38: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Conclusions and summary

Ronen Ariely, Senior consultant & architect, http://ariely.info

What next? Every DBA team must have

some knowledge in .NET programing!

Before you build a solution,

always remember that it might be best to use SQLCLR!

Have fun !

Page 39: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Ronen Ariely, Senior consultant & architect, http://ariely.info

Please fill evaluation forms

Page 40: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Ronen Ariely, Senior consultant & architect, http://ariely.info

Special thanks to our great sponsors!

Page 41: SQLCLR From 2005 to 2015 - gallery.technet.microsoft.com · >> Senior consultant, architect & professional programmer, architecting SQL & BI enterprise level solutions, with more

Ronen Ariely, Senior consultant & architect, http://ariely.info


Recommended