<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/sql-2/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/sql-2/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Fri, 04 Feb 2022 16:14:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>.NET EF Core 6 support for groupby top(n) queries</title>
		<link>https://blogit.create.pt/telmorodrigues/2022/02/04/net-ef-core-6-support-for-groupby-topn-queries/</link>
					<comments>https://blogit.create.pt/telmorodrigues/2022/02/04/net-ef-core-6-support-for-groupby-topn-queries/#respond</comments>
		
		<dc:creator><![CDATA[Telmo Rodrigues]]></dc:creator>
		<pubDate>Fri, 04 Feb 2022 16:14:46 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[entity framework]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=12605</guid>

					<description><![CDATA[<p>EF Core 6 comes with some GroupBy queries improvements. In this post I wanna talk about the improvements related to &#8220;group by top(n)&#8221; queries. Let&#8217;s say we have the following table Documents: and that we want to get the two most recent documents for each user. For instance, if we have the following records: the [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/telmorodrigues/2022/02/04/net-ef-core-6-support-for-groupby-topn-queries/">.NET EF Core 6 support for groupby top(n) queries</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>EF Core 6 comes with some GroupBy queries improvements. In this post I wanna talk about the improvements related to &#8220;group by top(n)&#8221; queries.</p>



<p></p>



<p></p>



<p>Let&#8217;s say we have the following table Documents:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
CREATE TABLE &#x5B;dbo].&#x5B;Documents](
    &#x5B;Id] &#x5B;integer] PRIMARY KEY,
    &#x5B;UserId] &#x5B;integer] NOT NULL,
    &#x5B;Title] &#x5B;nvarchar](50) NOT NULL,
    &#x5B;Body] &#x5B;nvarchar](250) NOT NULL,
    &#x5B;CreatedOn] &#x5B;datetime] NOT NULL
)
</pre></div>


<p>and that we want to get the two most recent documents for each user. For instance, if we have the following records:</p>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" src="https://blogit.create.pt/wp-content/uploads/2022/02/image-6.png" alt="" class="wp-image-12606" width="523" height="183" srcset="https://blogit.create.pt/wp-content/uploads/2022/02/image-6.png 523w, https://blogit.create.pt/wp-content/uploads/2022/02/image-6-300x105.png 300w" sizes="(max-width: 523px) 100vw, 523px" /></figure>



<p>the query should return</p>



<figure class="wp-block-image size-full"><img decoding="async" width="520" height="146" src="https://blogit.create.pt/wp-content/uploads/2022/02/image-7.png" alt="" class="wp-image-12607" srcset="https://blogit.create.pt/wp-content/uploads/2022/02/image-7.png 520w, https://blogit.create.pt/wp-content/uploads/2022/02/image-7-300x84.png 300w" sizes="(max-width: 520px) 100vw, 520px" /></figure>



<p>To do this query using EF Core and LINQ we can try to group the Documents by UserId and then sort each group by the CreatedOn column to pick the first two documents for each user.</p>



<p>We can start by trying to group all documents for each user</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
 var usersDocs = await ctx
        .Documents
        .GroupBy(doc =&gt; doc.UserId)
</pre></div>


<p>and then for each group we try to sort its elements by the CreatedOn column</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
    var usersDocs = await ctx
        .Documents
        .GroupBy(doc =&gt; doc.UserId)
        .SelectMany(userDocs =&gt; userDocs.OrderByDescending(doc =&gt; doc.CreatedOn).Take(2))
        .ToArrayAsync();
</pre></div>


<p>If we try to execute this query using a previous version of EF Core 6 we get the following error:</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
    .OrderByDescending(doc =&amp;gt; doc.CreatedOn)&#039; could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to &#039;AsEnumerable&#039;, &#039;AsAsyncEnumerable&#039;, &#039;ToList&#039;, or &#039;ToListAsync&#039;. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.&#039;
</pre></div>


<p></p>



<p>This is because the previous versions of EF don&#8217;t know how to translate the GroupBy inner expressions to sql. After following their suggestion I ended up with this query</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
    var usersDocs = await ctx
        .Documents
        .Select(doc =&gt; doc.UserId)
        .Distinct()
        .SelectMany(userId =&gt; 
            ctx
            .Documents
            .Where(doc =&gt; doc.UserId == userId)
            .OrderByDescending(doc =&gt; doc.CreatedOn)
            .Take(2)
        )
        .ToArrayAsync();
</pre></div>


<p>However this query has two main issues. It does a distinct over the UserId column, loads all the user ids to application memory and then it does a query for each user resulting in a n+1 query problem.</p>



<p>One way to solve these issues is to forget LINQ and rewrite the query using raw sql with a partition by UserId and the ROW_NUMBER() window funtion, doing a CTE .</p>



<p>Now, with the EF Core 6 we can use the first version of the query, since the EF Core team has added the support for translating some GroupBy inner expressions and it can translate this LINQ query to a single sql query.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
    var usersDocs = await ctx
        .Documents
        .GroupBy(doc =&gt; doc.UserId)
        .SelectMany(userDocs =&gt; userDocs.OrderByDescending(doc =&gt; doc.CreatedOn).Take(2))
        .ToArrayAsync();
</pre></div>


<p>You can read more about these features and other improvements added to GroupBy queries&nbsp;<a href="https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/whatsnew#improved-groupby-support">here</a>&nbsp;and check the related github issues&nbsp;<a href="https://github.com/dotnet/efcore/issues/12088">12088</a>&nbsp;<a href="https://github.com/dotnet/efcore/issues/13805">13805</a>&nbsp;</p>



<p>Happy coding!</p>
<p>The post <a href="https://blogit.create.pt/telmorodrigues/2022/02/04/net-ef-core-6-support-for-groupby-topn-queries/">.NET EF Core 6 support for groupby top(n) queries</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/telmorodrigues/2022/02/04/net-ef-core-6-support-for-groupby-topn-queries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>NoSQL First Act &#8211; a historical introduction</title>
		<link>https://blogit.create.pt/goncalomelo/2019/08/13/nosql-first-act-a-historical-introduction/</link>
					<comments>https://blogit.create.pt/goncalomelo/2019/08/13/nosql-first-act-a-historical-introduction/#respond</comments>
		
		<dc:creator><![CDATA[Gonçalo Melo]]></dc:creator>
		<pubDate>Tue, 13 Aug 2019 17:57:42 +0000</pubDate>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[Cluster]]></category>
		<category><![CDATA[Database history]]></category>
		<category><![CDATA[Database model]]></category>
		<category><![CDATA[Horizontal scale]]></category>
		<category><![CDATA[impedance mismatch]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Relational database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Vertical scale]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=9739</guid>

					<description><![CDATA[<p>NoSQL databases introduction and dominant features. A historical perspective to their appearance in a world dominated by relational model databases.</p>
<p>The post <a href="https://blogit.create.pt/goncalomelo/2019/08/13/nosql-first-act-a-historical-introduction/">NoSQL First Act &#8211; a historical introduction</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>NoSQL gets a lot of &#8220;heat&#8221; about not having a good direct definition. And the term NoSQL only gives somes clues of what this is not. </p>



<p>NoSQL is like a new definition of something that is a database but is different than the usual relational model. Likewise, making a parallel by going back 30 years ago. Back then probably no one knew what a relational database was also. Let&#8217;s use this and start with a small historical and motivational point of view for NoSQL. PS: I was not a developer back then.</p>



<p>This article will be a first part of a series of articles. The goal will be to unite some knowledge about this topic. </p>



<h2 class="wp-block-heading">Starting in the 1980&#8217;s</h2>



<p>Relational databases emerged bringing ACID properties &#8211; Atomicity, Consistency, Isolation and Persistency. Properties taken for granted nowadays. Also, they also brought the SQL language. SQL is common enough across different systems for one to use. Although there are different flavors, can almost be considered a standard.</p>



<p>Relation databases also allowed for a simple and very common integration mechanism between 2 or more systems. Data can be easily shared by reading or writing data in a table on a shared relational database. This is today still a very common integration pattern.</p>



<h2 class="wp-block-heading">In the 1990&#8217;s</h2>



<p>Object database&#8217;s started to appear with more strength. They had been around for some time. Most important, they implement a different database paradigm model. This new paradigm tried to solve the impedance mismatch problem. The impedance mismatch was caused by relational databases. It emerges from the need to map objects used in memory in our application model to tables in a relational database. This page <a href="https://en.wikipedia.org/wiki/Object-relational_impedance_mismatch">https://en.wikipedia.org/wiki/Object-relational_impedance_mismatch</a> is a good reference for this issue.</p>



<p>Saving an application object into these database model should be a direct operation. Conceptually no mapping would be necessary when an object database type was used. Albeit, these gains, object databases were not able to substitute relational databases. </p>



<h2 class="wp-block-heading">In the 2000&#8217;s</h2>



<p>As internet availability grows 2 things started to have a big impact:</p>



<ul class="wp-block-list"><li>Generation of enormous amount of data that had to be stored and processed;</li><li>Accesses from anywhere in the globe became more and more frequent. Therefore latency was an issue to consider. And even speed of light limitation can contribute significantly to this. Data can now be stored far from where we are. For example, a distance of 10.000 km will add about 100 ms on each round trip. Therefore, data needs to spread around the globe to provide a quick access.</li></ul>



<p>In this environment,
relational databases do not always solve customer needs, either because of
flexibility, price or performance issues:</p>



<ul class="wp-block-list"><li>Relational databases enforces a ridged set of rules in the relational model. These will impact the flexibility of the application development;</li><li>Price because of the need for more powerful machines and also the software licenses that go with this;</li><li>Performance, because relational databases do not naturally meet the increasing workload. Huge CPU, memory, storage and throughput is needed to be perform in this environment. And for relational model, vertical scaling can only get you so far.</li></ul>



<h2 class="wp-block-heading">Scaling Vertically versus Horizontally</h2>



<p>Relational databases usual scaling method is a vertical one. As a result whenever the workload incresses we will use a bigger machine with more hardware resources. </p>



<p>On the other hand, the big internet companies adopted an horizontal scaling paradigm. This is a cluster type environment with lots and lots of machines. In other words, this means that more machines will be used to handle bigger workloads.</p>



<p>Relational databases do not thrive very well in this paradigm. They hardly take any benefit of using more machines. Hence, NoSQL like databases arises to take advantage of the horizontal scale paradigm. Also companies like Google and Amazon started researching in this area. As a result, Google created BigTable and Amazon DynamoDB.</p>



<h2 class="wp-block-heading">Relational databases dominated the market, why are NoSQL databases being used now?</h2>



<p>I think this is important. Dominance and usage factors are usually a combination of several aspects. What has been changing:</p>



<ul class="wp-block-list"><li>Developers hide databases behind integration layers. This makes it simpler to use. And in addition, easier to replace one database or persistence method per other; </li><li>Cloud growth in some cases means we can try different approaches with less effort regarding infrastructure;</li><li>Handling big quantities of data introduced new needs. NoSQL provides a good development flexibility. And in addition can also take advantage of cluster solutions.</li></ul>



<p>Finally, what is NoSQL?</p>



<h2 class="wp-block-heading">Common NoSQL databases characteristics</h2>



<p>NoSQL doesn’t have a clear direct meaning. The term alludes to something like &#8220;Not Only SQL&#8221;. A more exact explanation should be &#8220;Non Relational database&#8221;. This would reinforce the new model paradigm and not the SQL language itself. The fact that some NoSQL databases actually supports some form of SQL language adds even more to the confusion.</p>



<p>But, the name is catchy and so common that will probably stay. The point is: defining NoSQL is hard. Therefore, we will do the next best thing. Introduce the dominant traits of these database systems:</p>



<ul class="wp-block-list"><li>Non relational;</li><li>Usually (not all) cluster-friendly;</li><li>Most of them are open source;</li><li>No-schema / schema-less;</li><li>Have a big internet drive (cluster and bigdata friendly).</li></ul>



<p>By these common traits, almost any non relational database can be a NoSQL database! Also, there are probably published studies in all these areas dating back 30 or 40 year ago. Out of curiosity, the name NoSQL itself is referred to being born by the end of 2009. The term emerged as a twitter hashtag for a meetup were people talked about these subjects. Even though some of these traits were not new at the time.</p>



<h2 class="wp-block-heading">Next steps</h2>



<p>NoSQL has a lot to explore. It will provide a more direct paradigm that will make simpler specific needs like this ones to <a href="https://blogit.create.pt////diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/">query Json objects inside Sql Server</a> and <a href="https://blogit.create.pt////goncalomelo/2018/12/20/query-performance-for-json-objects-inside-sql-server/">performance considerations</a>.</p>



<p>My plan for the next article(s) will be to:</p>



<ul class="wp-block-list"><li>Present some of the NoSQL data models; </li><li>Talk about the no schema or the schema less feature;</li><li>Discuss the aggregate concept. And how this relates to the CAP theorem.</li></ul>



<h2 class="wp-block-heading">Further reads</h2>



<p>There is a lot of information online. For me, I like Martin Fowler approach. Some of these information&#8217;s are from his content. You can check his website here: <a href="https://martinfowler.com/nosql.html">https://martinfowler.com/nosql.html</a>. He also has a book: <a href="https://martinfowler.com/books/nosql.html">https://martinfowler.com/books/nosql.html</a>.</p>



<p></p>
<p>The post <a href="https://blogit.create.pt/goncalomelo/2019/08/13/nosql-first-act-a-historical-introduction/">NoSQL First Act &#8211; a historical introduction</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/goncalomelo/2019/08/13/nosql-first-act-a-historical-introduction/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Query performance for JSON objects inside SQL Server using JSON_VALUE function</title>
		<link>https://blogit.create.pt/goncalomelo/2018/12/20/query-performance-for-json-objects-inside-sql-server/</link>
					<comments>https://blogit.create.pt/goncalomelo/2018/12/20/query-performance-for-json-objects-inside-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Gonçalo Melo]]></dc:creator>
		<pubDate>Thu, 20 Dec 2018 09:39:12 +0000</pubDate>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=8077</guid>

					<description><![CDATA[<p>Following up on this article about querying JSON Data I would like to talk about how to improve searches on JSON data inside SQL Server. Starting SQL Server 2016 Microsoft deployed a set of functions that allow us to work with JSON data in a structured way inside SQL Server. I will introduce a small [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/goncalomelo/2018/12/20/query-performance-for-json-objects-inside-sql-server/">Query performance for JSON objects inside SQL Server using JSON_VALUE function</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Following up on <a href="https://blogit.create.pt////diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/">this article about querying JSON Data</a> I would like to talk about how to improve searches on JSON data inside SQL Server. Starting SQL Server 2016 Microsoft deployed a set of functions that allow us to work with JSON data in a structured way inside SQL Server.</p>



<p>I will introduce a small usage sample for the <strong>JSON_VALUE </strong>function in combination with indexes to improve information retrieval from a table containing one JSON object. For our testes, we have a <strong>UserDetailTest </strong>table that has more than 500k rows with 2 columns: an UserId and a nvarchar(max) to hold a small JSON details string like the following:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT *, LEN(DetailsJSON) AS &#x5B;Len(DetailsJSON)] FROM UserDetailTest
</pre></div>


<figure class="wp-block-image"><img decoding="async" width="937" height="188" src="https://blogit.create.pt////wp-content/uploads/2018/12/sampleTable.png" alt="" class="wp-image-8153" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/sampleTable.png 937w, https://blogit.create.pt/wp-content/uploads/2018/12/sampleTable-300x60.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/sampleTable-768x154.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/sampleTable-696x140.png 696w" sizes="(max-width: 937px) 100vw, 937px" /></figure>



<p>I will activate time statistics and clean SQL Server cache between each query to have some consistency across the execution times using the following SQL statements:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
CHECKPOINT
DBCC DROPCLEANBUFFERS
SET STATISTICS TIME ON
</pre></div>


<h2 class="wp-block-heading">JSON_VALUE Function intro</h2>



<p>The JSON_VALUE function extracts a value from a JSON string. This functions receives 2 arguments, the first being an expression for the JSON value and the second a path for the value we want to obtain. A simple sample with an inline JSON object would be the following:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT JSON_VALUE('{"PostalCode":"376-3765","PhoneNumber":"351003765718"}', '$.PhoneNumber')
</pre></div>


<figure class="wp-block-image"><img decoding="async" width="1245" height="123" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2018/12/selectResult0.png?fit=696%2C69&amp;ssl=1" alt="" class="wp-image-8182" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult0.png 1245w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult0-300x30.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult0-768x76.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult0-1024x101.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult0-696x69.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult0-1068x106.png 1068w" sizes="(max-width: 1245px) 100vw, 1245px" /></figure>



<p>The return is a scalar value (nvarchar(4000)) for the PhoneNumber element.</p>



<h2 class="wp-block-heading">Query scenarios</h2>



<p>Starting with one of the <strong>really&nbsp;worst </strong>case scenario (yes, this can happen&#8230;):</p>



<figure class="wp-block-image"><img decoding="async" width="1024" height="240" src="https://blogit.create.pt////wp-content/uploads/2018/12/selectResult20-1024x240.png" alt="" class="wp-image-8197" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult20-1024x240.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult20-300x70.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult20-768x180.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult20-696x163.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult20-1068x250.png 1068w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult20.png 1275w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>More than 17 seconds really seems like a worst scenario! Next, we will use the JSON_VALUE function to get the PhoneNumber from the JSON string and use it in our where clause:</p>



<figure class="wp-block-image"><img decoding="async" width="1266" height="301" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2018/12/selectResult22.png?fit=696%2C165&amp;ssl=1" alt="" class="wp-image-8198" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult22.png 1266w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult22-300x71.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult22-768x183.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult22-1024x243.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult22-696x165.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult22-1068x254.png 1068w" sizes="(max-width: 1266px) 100vw, 1266px" /></figure>



<p>This still takes more than 3 seconds. A good improvement, but yet a high cost if we need to search information in this way.</p>



<h2 class="wp-block-heading" id="mce_9">Index creation</h2>



<p>Let&#8217;s add a new virtual column to table that displays the result from the JSON_VALUE function &#8211; this will allow us to create an index and simplify the SELECT queries.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
ALTER TABLE UserDetailTest 
	ADD vPhoneNumber AS 
	CAST(JSON_VALUE(DetailsJSON, '$.PhoneNumber') AS NVARCHAR(255));
</pre></div>


<p>The cast truncates the output from the JSON_VALUE&nbsp;to ensure that it does not exceed the maximum lenght for the index key. Now, if we search using the column, the result is still more or less the same 3 seconds has before:</p>



<ul class="wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><img decoding="async" width="1274" height="311" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2018/12/selectResult23.png?fit=696%2C170&amp;ssl=1" alt="" data-id="8199" data-link="https://blogit.create.pt////?attachment_id=8199" class="wp-image-8199" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult23.png 1274w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult23-300x73.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult23-768x187.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult23-1024x250.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult23-696x170.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult23-1068x261.png 1068w" sizes="(max-width: 1274px) 100vw, 1274px" /></figure></li></ul>



<p>Let&#8217;s create an index and perform the search again:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
CREATE INDEX idx_vPhoneNumber ON &#x5B;UserDetailTest] (vPhoneNumber);

</pre></div>


<ul class="wp-block-gallery columns-1 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><img decoding="async" width="1270" height="316" src="https://i1.wp.com/blogit.create.pt/wp-content/uploads/2018/12/selectResult24.png?fit=696%2C173&amp;ssl=1" alt="" data-id="8201" data-link="https://blogit.create.pt////?attachment_id=8201" class="wp-image-8201" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult24.png 1270w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult24-300x75.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult24-768x191.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult24-1024x255.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult24-696x173.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult24-1068x266.png 1068w" sizes="(max-width: 1270px) 100vw, 1270px" /></figure></li></ul>



<p>Consequently the improvement is huge, 13 miliseconds is more interesting! But what&#8217;s the cost?</p>



<h2 class="wp-block-heading">The cost</h2>



<p>When we create an index, we&#8217;re basically trading space for time &#8211; more occupied space versus faster operations. Therefore let&#8217;s see what the increase is using&nbsp;<strong>sp_spaceused</strong> function before and after index creation:</p>



<table class="wp-block-table is-style-regular"><tbody><tr><td></td><td><strong>rows</strong></td><td><strong>reserved</strong></td><td><strong>data</strong></td><td><strong>index_size</strong></td><td><strong>unused</strong></td></tr><tr><td><strong>Initial</strong></td><td>557801              </td><td>1120904 KB</td><td>1115584 KB</td><td>5224 KB</td><td>96 KB</td></tr><tr><td><strong>After Index</strong></td><td>557801</td><td>1148752 KB</td><td>1115584 KB</td><td>32992 KB</td><td>176 KB</td></tr></tbody></table>



<p>Probably, an interesting comparison would be if the PhoneNumber column was an explicit column in that table containing the value. Let&#8217;s do that!</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
ALTER TABLE UserDetailTest  
	ADD PhoneNumberCopy NVARCHAR(255)

UPDATE UserDetailTest
	SET PhoneNumberCopy = vPhoneNumber
</pre></div>


<p>The sp_spaceused remained the same &#8211; SQL Server internal black magic regarding space allocation (for another time!). But performance wise, for this approach in my machine this query still took more than 2 seconds to complete:</p>



<figure class="wp-block-image"><img decoding="async" width="1269" height="362" src="https://i2.wp.com/blogit.create.pt/wp-content/uploads/2018/12/selectResult26.png?fit=696%2C198&amp;ssl=1" alt="" class="wp-image-8203" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult26.png 1269w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult26-300x86.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult26-768x219.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult26-1024x292.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult26-696x199.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult26-1068x305.png 1068w" sizes="(max-width: 1269px) 100vw, 1269px" /></figure>



<p>Slightly better than the query with the JSON function but the advantage is that in this scenario SQL can better optimize the searches. The following runs of the same query without clearing the cache returns results much faster &#8211; each took around 220 miliseconds to complete:</p>



<figure class="wp-block-image"><img decoding="async" width="1263" height="343" src="https://i2.wp.com/blogit.create.pt/wp-content/uploads/2018/12/selectResult27.png?fit=696%2C189&amp;ssl=1" alt="" class="wp-image-8204" srcset="https://blogit.create.pt/wp-content/uploads/2018/12/selectResult27.png 1263w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult27-300x81.png 300w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult27-768x209.png 768w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult27-1024x278.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult27-696x189.png 696w, https://blogit.create.pt/wp-content/uploads/2018/12/selectResult27-1068x290.png 1068w" sizes="(max-width: 1263px) 100vw, 1263px" /></figure>



<h2 class="wp-block-heading">Summary Results</h2>



<p>Just to sum up all the results for the different types of searches comparing the first execution after cache clean up and the following runs.&nbsp;I did several iterations for each step just to make sure the results were consistent&nbsp;although the goal was just to have a baseline. Certanly, the use of this will depend on each case.</p>



<table class="wp-block-table"><tbody><tr><td><strong>WHERE clausule type</strong></td><td><strong>First run</strong></td><td><strong>Following runs</strong></td></tr><tr><td><pre class="brush: sql; gutter: false; title: ; notranslate">WHERE DetailsJson LIKE '%PhoneNumber&quot;:&quot;351003765718&quot;%'</pre></td><td>17754 ms</td><td>around the same</td></tr><tr><td><pre class="brush: sql; gutter: false; title: ; notranslate">WHERE JSON_VALUE(DetailsJson, '$.PhoneNumber') = '351003765718'</pre></td><td>3375 ms</td><td>1958 ms</td></tr><tr><td><pre class="brush: sql; gutter: false; title: ; notranslate">WHERE vPhoneNumber = '351003765718'</pre>vPhoneNumber is not indexed&nbsp;</td><td>3339 ms</td><td>1332 ms</td></tr><tr><td><pre class="brush: sql; gutter: false; title: ; notranslate">WHERE vPhoneNumber = '351003765718'</pre> vPhoneNumber is indexed</td><td>13 ms</td><td>0 ms</td></tr><tr><td><pre class="brush: sql; gutter: false; title: ; notranslate">WHERE PhoneNumberCopy  = '351003765718'</pre>PhoneNumberCopy&nbsp; is of type nvarchar(255) and explicitly contains all values</td><td>2309 ms</td><td>222 ms</td></tr></tbody></table>



<p></p>
<p>The post <a href="https://blogit.create.pt/goncalomelo/2018/12/20/query-performance-for-json-objects-inside-sql-server/">Query performance for JSON objects inside SQL Server using JSON_VALUE function</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/goncalomelo/2018/12/20/query-performance-for-json-objects-inside-sql-server/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Query a JSON array in SQL</title>
		<link>https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/</link>
					<comments>https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/#comments</comments>
		
		<dc:creator><![CDATA[Diogo Guiomar]]></dc:creator>
		<pubDate>Mon, 26 Feb 2018 11:07:45 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[openjson]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/diogoguiomar/?p=44</guid>

					<description><![CDATA[<p>For the purpose of this post, lets not evaluate the db design option and lets focus on the operations on the json column. Lets say we have a table of customers where we have an Id, a CompanyName and an Address, although these customers can have some information related to a service, for example they [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/">Query a JSON array in SQL</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>For the purpose of this post, lets not evaluate the db design option and lets focus on the operations on the json column.</p>
<p>Lets say we have a table of customers where we have an Id, a CompanyName and an Address, although these customers can have some information related to a service, for example they may have a specific customer id for each service.</p>
<pre>Customer

Id: the id

CompanyName: the company name

Address: the address

--- ServiceId: 1

--- ServiceCustomerId: the customer id for service 1

--- ServiceId: 2

--- ServiceCustomerId: the customer id for service 2</pre>
<p>One way we could store this on a single table could be with these columns: Id / Name / Address / ServicesDataInJson</p>
<p>So for this example, the information on the ServicesDataInJson is an array of &#8216;objects&#8217; that contains the information of our customer on each service. Since there is no native JSON format on SQL Server, the data type of this column is just a nvarchar(max)</p>
<p>Here&#8217;s how a simple SELECT looks like:</p>
<p><img decoding="async" class="alignnone size-full wp-image-74" src="http://blogit-create.com/wp-content/uploads/2018/02/image.png" alt="" width="1044" height="60" srcset="https://blogit.create.pt/wp-content/uploads/2018/02/image.png 1044w, https://blogit.create.pt/wp-content/uploads/2018/02/image-300x17.png 300w, https://blogit.create.pt/wp-content/uploads/2018/02/image-768x44.png 768w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1024x59.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/02/image-696x40.png 696w" sizes="(max-width: 1044px) 100vw, 1044px" /></p>
<p>Now we want to query our customers table by a customer service id (which is inside that json). How can we query this?</p>
<pre class="brush: sql; title: ; notranslate">

SELECT *

FROM Customers c

CROSS APPLY OPENJSON(c.ServicesDataInJson)

WITH (ServiceId int '$.ServiceId',

ServiceCustomerId nvarchar(255) '$.ServiceCustomerId') as jsonValues

WHERE jsonValues.ServiceCustomerId = @TheIdWeWantToSearchFor

</pre>
<p><strong>The OPENJSON is a table-valued function that parses the json into a row/column result and the WITH clause let us define how we want that output.</strong></p>
<p>Here is the query <strong>without</strong> the WHERE clause:</p>
<p><img decoding="async" class="alignnone size-full wp-image-114" src="http://blogit-create.com/wp-content/uploads/2018/02/image-1.png" alt="" width="970" height="82" srcset="https://blogit.create.pt/wp-content/uploads/2018/02/image-1.png 970w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1-300x25.png 300w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1-768x65.png 768w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1-696x59.png 696w" sizes="(max-width: 970px) 100vw, 970px" /></p>
<p>Note: The OPENJSON function will not work if the database compatibility level is lower than 130.</p>
<p>To change it:</p>
<pre class="brush: sql; title: ; notranslate">ALTER DATABASE DatabaseName SET COMPATIBILITY_LEVEL = 130</pre>
<p>The post <a href="https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/">Query a JSON array in SQL</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Converting a Vertical Table to an Horizontal Table in SQL Server</title>
		<link>https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/</link>
					<comments>https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Ricardo Costa]]></dc:creator>
		<pubDate>Thu, 27 Oct 2016 21:24:13 +0000</pubDate>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tsq]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/ricardocosta/?p=1421</guid>

					<description><![CDATA[<p>Today I&#8217;ve encountered a vertical table in an SQL Database and I wanted to transform it to an horizontal one. A vertical table is described as an EAV model. Imagine you have this table CREATE TABLE VerticalTable ( Id int, Att_Id varchar(50), Att_Value varchar(50) ) INSERT INTO VerticalTable SELECT 1, 'FirstName', 'John' UNION ALL SELECT [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/">Converting a Vertical Table to an Horizontal Table in SQL Server</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Today I&#8217;ve encountered a vertical table in an SQL Database and I wanted to transform it to an horizontal one. A vertical table is described as an <a href="https://goo.gl/5aym8G" target="_blank" rel="noopener">EAV model</a>.</p>
<p>Imagine you have this table</p>
<pre class="brush: sql; title: ; notranslate">

CREATE TABLE VerticalTable
(
Id int,
Att_Id varchar(50),
Att_Value varchar(50)
)

INSERT INTO VerticalTable
SELECT 1, 'FirstName', 'John' UNION ALL
SELECT 1, 'LastName', 'Smith' UNION ALL
SELECT 1, 'Email', 'john.smith@dummy.com' UNION ALL
SELECT 2, 'FirstName', 'Jack' UNION ALL
SELECT 2, 'LastName', 'Daniels' UNION ALL
SELECT 2, 'Email', 'jack.daniels@dummy.com'

</pre>
<p>If you run</p>
<pre class="brush: sql; title: ; notranslate">

SELECT * FROM VerticalTable

</pre>
<p>you get this</p>
<table class="sqloutput">
<tbody>
<tr>
<th></th>
<th>Id</th>
<th>Att_Id</th>
<th>Att_Value</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>FirstName</td>
<td>John</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>LastName</td>
<td>Smith</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>Email</td>
<td>john.smith@dummy.com</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>FirstName</td>
<td>Jack</td>
</tr>
<tr>
<td>5</td>
<td>2</td>
<td>LastName</td>
<td>Daniels</td>
</tr>
<tr>
<td>6</td>
<td>2</td>
<td>Email</td>
<td>jack.daniels@dummy.com</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>To convert this into an horizontal table I&#8217;m going to use <a href="https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx" target="_blank" rel="noopener">PIVOT</a>.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT &#x5B;Id], &#x5B;FirstName], &#x5B;LastName], &#x5B;Email] 
FROM
(
 SELECT Id, Att_Id, Att_Value FROM VerticalTable
) as source
PIVOT
(
 MAX(Att_Value) FOR Att_Id IN (&#x5B;FirstName], &#x5B;LastName], &#x5B;Email])
) as target

</pre>
<p>And I will get this</p>
<table class="sqloutput">
<tbody>
<tr>
<th></th>
<th>Id</th>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>John</td>
<td>Smith</td>
<td>john.smith@dummy.com</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>Jack</td>
<td>Daniels</td>
<td>jack.daniels@dummy.com</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>You can find the code <a href="http://rextester.com/NSKR13741" target="_blank" rel="noopener">here</a>.</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/">Converting a Vertical Table to an Horizontal Table in SQL Server</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/ricardocosta/2016/10/27/converting-a-vertical-table-to-an-horizontal-table-in-sql-server/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>How to Access the Previous Row and Next Row value in SELECT statement?</title>
		<link>https://blogit.create.pt/ricardocosta/2014/11/07/how-to-access-the-previous-row-and-next-row-value-in-select-statement/</link>
					<comments>https://blogit.create.pt/ricardocosta/2014/11/07/how-to-access-the-previous-row-and-next-row-value-in-select-statement/#respond</comments>
		
		<dc:creator><![CDATA[Ricardo Costa]]></dc:creator>
		<pubDate>Fri, 07 Nov 2014 11:09:37 +0000</pubDate>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/ricardocosta/?p=741</guid>

					<description><![CDATA[<p>LAG &#8211; http://msdn.microsoft.com/en-us/library/hh231256.aspx USE AdventureWorks2012; GO SELECT BusinessEntityID, YEAR(QuotaDate) AS SalesYear, SalesQuota AS CurrentQuota, LAG(SalesQuota, 1,0) OVER (ORDER BY YEAR(QuotaDate)) AS PreviousQuota FROM Sales.SalesPersonQuotaHistory WHERE BusinessEntityID = 275 and YEAR(QuotaDate) IN ('2005','2006'); LEAD &#8211; http://msdn.microsoft.com/en-us/library/hh213125.aspx USE AdventureWorks2012; GO SELECT BusinessEntityID, YEAR(QuotaDate) AS SalesYear, SalesQuota AS CurrentQuota, LEAD(SalesQuota, 1,0) OVER (ORDER BY YEAR(QuotaDate)) AS NextQuota FROM Sales.SalesPersonQuotaHistory WHERE [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2014/11/07/how-to-access-the-previous-row-and-next-row-value-in-select-statement/">How to Access the Previous Row and Next Row value in SELECT statement?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>LAG</strong> &#8211; <a id="x_lnk563653" href="http://msdn.microsoft.com/en-us/library/hh231256.aspx" target="_blank"></a><a id="x_lnk618706" title="http://msdn.microsoft.com/en-us/library/hh231256.aspx&lt;br /&gt;&lt;br /&gt;
Ctrl+click or tap to follow link" href="http://msdn.microsoft.com/en-us/library/hh231256.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/hh231256.aspx</a></p>
<pre>USE AdventureWorks2012;
GO
SELECT BusinessEntityID, YEAR(QuotaDate) AS SalesYear, SalesQuota AS CurrentQuota, 
       LAG(SalesQuota, 1,0) OVER (ORDER BY YEAR(QuotaDate)) AS PreviousQuota
FROM Sales.SalesPersonQuotaHistory
WHERE BusinessEntityID = 275 and YEAR(QuotaDate) IN ('2005','2006');</pre>
<p><strong>LEAD</strong> &#8211; <a id="x_lnk627900" href="http://msdn.microsoft.com/en-us/library/hh213125.aspx" target="_blank"></a><a id="x_lnk774047" href="http://msdn.microsoft.com/en-us/library/hh213125.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/hh213125.aspx</a></p>
<pre>USE AdventureWorks2012;
GO
SELECT BusinessEntityID, YEAR(QuotaDate) AS SalesYear, SalesQuota AS CurrentQuota, 
    LEAD(SalesQuota, 1,0) OVER (ORDER BY YEAR(QuotaDate)) AS NextQuota
FROM Sales.SalesPersonQuotaHistory
WHERE BusinessEntityID = 275 and YEAR(QuotaDate) IN ('2005','2006');</pre>
<p>SQL SERVER 2012 and 2014</p>
<p>&nbsp;</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2014/11/07/how-to-access-the-previous-row-and-next-row-value-in-select-statement/">How to Access the Previous Row and Next Row value in SELECT statement?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/ricardocosta/2014/11/07/how-to-access-the-previous-row-and-next-row-value-in-select-statement/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Strange behavior with &#8220;Merge Join&#8221; in SSIS</title>
		<link>https://blogit.create.pt/ricardocosta/2014/10/24/strange-behavior-with-merge-join-in-ssis/</link>
					<comments>https://blogit.create.pt/ricardocosta/2014/10/24/strange-behavior-with-merge-join-in-ssis/#respond</comments>
		
		<dc:creator><![CDATA[Ricardo Costa]]></dc:creator>
		<pubDate>Fri, 24 Oct 2014 10:04:36 +0000</pubDate>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSIS]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/ricardocosta/?p=691</guid>

					<description><![CDATA[<p>If the datasources of the Merge Join block in SSIS aren&#8217;t sorted the merge will not work correctly. Strange behavior will occur if the datasources aren&#8217;t sorted equal by the same key. http://msdn.microsoft.com/en-us/library/ms141775.aspx &#8220;The Merge Join Transformation requires sorted data for its inputs.&#8221; http://msdn.microsoft.com/en-us/library/ms137653.aspx</p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2014/10/24/strange-behavior-with-merge-join-in-ssis/">Strange behavior with &#8220;Merge Join&#8221; in SSIS</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If the datasources of the Merge Join block in SSIS aren&#8217;t sorted the merge will not work correctly.</p>
<p><a href="http://blogit-create.com/wp-content/uploads/2014/10/mergejoin.png"><img decoding="async" class="alignnone size-full wp-image-701" src="http://blogit-create.com/wp-content/uploads/2014/10/mergejoin.png" alt="Merge Join" width="148" height="62" /></a></p>
<p>Strange behavior will occur if the datasources aren&#8217;t sorted equal by the same key.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms141775.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms141775.aspx</a></p>
<p><em>&#8220;The Merge Join Transformation requires sorted data for its inputs.&#8221;</em></p>
<p><a title="Sort Data for the Merge and Merge Join Transformation" href="http://msdn.microsoft.com/en-us/library/ms137653.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms137653.aspx</a></p>
<p>The post <a href="https://blogit.create.pt/ricardocosta/2014/10/24/strange-behavior-with-merge-join-in-ssis/">Strange behavior with &#8220;Merge Join&#8221; in SSIS</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/ricardocosta/2014/10/24/strange-behavior-with-merge-join-in-ssis/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
