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
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.
SQLCLR From 2005 to 2015Not how, but why and when
Ronen Ariely,
Senior consultant & architect
#360 | Israel 2015, January 19
SQLCLR
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.
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.
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
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.
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
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
Ronen Ariely,
Senior consultant & architect
Is T-SQL Dead ?!?
Is T-SQL Dead ?!?
Ronen Ariely, Senior consultant & architect, http://ariely.info
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
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
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
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
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
Split String, function
Ronen Ariely, Senior consultant & architect, http://ariely.info
http://sqlperformance.com/2012/07/t-sql-queries/split-strings
Resources & Time
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.
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
Random String, function
Ronen Ariely, Senior consultant & architect, http://ariely.info
Play Time
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!
חידה
Ronen Ariely, Senior consultant & architect, http://ariely.info
סרטיםשלושהבדיוקלבחורצריךמשתמש
שרתלמועבריםהנתונים.אוהבהכישהוא
.פרוצדורהאל,פסיקבהפרדתכשרשרת
השרשרתאתלנתחעלינוSQLהבשרת
אתלמצואהיא,היאשלנוהמטרה.שהגיעה
רוציםאנחנו.בלבדוהשלישיהשניהנתון.והשלישי שהגיעולהציג ולהכניס למסד הנתונים רק את המספר השני
?נעזר( וטכנולוגיה)באיזה פונקציה
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
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
Implementing new types
Ronen Ariely, Senior consultant & architect, http://ariely.info
Vectors and Matrices
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/
XML parser
Ronen Ariely, Senior consultant & architect, http://ariely.info
XML to Table / Table to XML
Working with Files / Folders
Ronen Ariely, Senior consultant & architect, http://ariely.info
xp_cmdshel vs CLR
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!
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
In-Memory Elements
Ronen Ariely, Senior consultant & architect, http://ariely.info
SQL Server 2005 EXPRESS and above
In-Memory Elements
Ronen Ariely, Senior consultant & architect, http://ariely.info
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/
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/
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"
}
]
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?!?
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.
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 !
Ronen Ariely, Senior consultant & architect, http://ariely.info
Please fill evaluation forms
Ronen Ariely, Senior consultant & architect, http://ariely.info
Special thanks to our great sponsors!
Ronen Ariely, Senior consultant & architect, http://ariely.info