<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: João Pedro</title>
    <description>The latest articles on DEV Community by João Pedro (@jhonpedro).</description>
    <link>https://dev.to/jhonpedro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F635291%2F9e15664f-f6ea-4ad9-b112-857c27dca0da.jpeg</url>
      <title>DEV Community: João Pedro</title>
      <link>https://dev.to/jhonpedro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhonpedro"/>
    <language>en</language>
    <item>
      <title>Unit Tests in NodeJS</title>
      <dc:creator>João Pedro</dc:creator>
      <pubDate>Mon, 14 Aug 2023 22:26:26 +0000</pubDate>
      <link>https://dev.to/jhonpedro/unit-tests-in-nodejs-53jk</link>
      <guid>https://dev.to/jhonpedro/unit-tests-in-nodejs-53jk</guid>
      <description>&lt;p&gt;This will be a series of Unit Tests in X language (or runtime in this case haha). But for now we will stick with the unit tests and then, maybe, we can evolve into integration tests.&lt;/p&gt;

&lt;p&gt;I want to start this series as a way of learning tests in some languages and analyze the different aproaches that every language gives to us to start in that important topic. &lt;/p&gt;

&lt;p&gt;The application that we will be testing is a simple ideia that I had with free and open API's. &lt;strong&gt;Unit tests does not involve API's&lt;/strong&gt; but I got the idea from one of these API's. &lt;/p&gt;

&lt;h2&gt;
  
  
  And why Unit testing does not involve API's?
&lt;/h2&gt;

&lt;p&gt;I don't know, but I think that some people would disagree with me in this point. But in my opinion, &lt;strong&gt;Unit tests should just test the business rule of your app&lt;/strong&gt;. And being an API is just part of your app that you deliver to the client, your application, for example, could answer smoke signals with a sensor, or it could receive messages from a PUB/SUB third party, there are millions of options. If you disagree, PLEASE, chat with me in the comments, I would love to talk to you, and I am not even joking, I really would love to understand why you think that way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuing
&lt;/h2&gt;

&lt;p&gt;Free API's I said. The idea came from free food API's being more specific &lt;a href="https://www.themealdb.com/api.php"&gt;this one&lt;/a&gt;. Awesome API here, take a look, maybe you have an idea for a cool project.&lt;/p&gt;

&lt;p&gt;Our little project will be a function (that can be atached to anything, from API's to Workers, to recomendation Algorithms), I tried to build it in a very &lt;a href="https://en.wikipedia.org/wiki/Hexagonal_architecture_(software)"&gt;hexagonal&lt;/a&gt; way because testing really depends on your architecture, aside from testing you need a good arch that allow you to test without many work arounds (in portuguese we call "gambiarra").&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;GetRandomMealUseCase&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;userFavorites&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;potatoes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;beef&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;mealSerice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMealService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMealRecipe&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;choiceLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RandomMealChoice&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt;
  &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;meal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mealSerice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getRandomMeal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;howManyMealTheUserLikes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;meal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ingredients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userFavorites&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLocaleLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;meal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;choiceLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RandomMealChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Average&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;howManyMealTheUserLikes&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;choiceLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;RandomMealChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReallyGood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;howManyMealTheUserLikes&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;choiceLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;RandomMealChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Good&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically this is our whole little project. We have a service which just make a HTTP request and some basic error handling, I have choose as a error treatment something like Either where in the first position of the tuple is the error and in the second the response. I added the variable &lt;code&gt;userFavorites&lt;/code&gt; fixed in the code, but this could be a parameter for our &lt;code&gt;execute&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;We have 4 tests that I see for this little file. 1 for each &lt;code&gt;choiceLevel&lt;/code&gt; and 1 for the error. Let's see how we can make those tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mocha&lt;/strong&gt; and &lt;strong&gt;SinonJS&lt;/strong&gt;. We will be using those two fantastic libs for our tests with NodeJS. Mocha for the test cases and SinonJS for our mockings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit tests
&lt;/h2&gt;

&lt;p&gt;I started creating a mock service so that I could inject it in the use case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getRandomMealBaseReturn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMealRecipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ingredients&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;measure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt;
  &lt;span class="na"&gt;instructions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mock&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MealServiceMock&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IMealService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;getRandomMeal&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;IMealRecipe&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;getRandomMealBaseReturn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is our mock of MealService. This is guy has a default return that we will mock in other tests so that it satisfies our asserts.&lt;/p&gt;

&lt;p&gt;Our first test we just need this base implementation of the meal service to create it so look at it below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GetRandomMeal service tests&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Should return a random meal sucessfuly with average meal choice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;GetRandomMealUseCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MealServiceMock&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;useCase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;strictEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;strictEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;choiceLevel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RandomMealChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Average&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This first test has just the ideia of returning a meal with average choice and no errors, our next 3 tests will be just mocking the response so that we can test the other branches of the &lt;code&gt;GetRandomMealUseCase&lt;/code&gt; and the error handling.&lt;/p&gt;

&lt;p&gt;And that is it! Check the project over &lt;a href="https://github.com/jhonpedro/unit-tests-in-different-langs/tree/main/nodejs"&gt;here&lt;/a&gt;. Like I said this will be a series of articles with this same project so, keep an eye here and study tests in other langs with me :)&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

</description>
      <category>node</category>
      <category>testing</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Query Execution Plan inside SQL Server</title>
      <dc:creator>João Pedro</dc:creator>
      <pubDate>Fri, 04 Aug 2023 01:03:14 +0000</pubDate>
      <link>https://dev.to/jhonpedro/query-execution-plan-inside-sql-server-4585</link>
      <guid>https://dev.to/jhonpedro/query-execution-plan-inside-sql-server-4585</guid>
      <description>&lt;p&gt;Performance is a huge topic when dealing with databases, but for each case you will need a different approach for your data. And as always it is good to understand how the things work under the hood. Today we will be talking about &lt;strong&gt;Query Execution Plan inside a SQL Server instance&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Disclaimer: This is not an advanced tutorial. We will cover just the basics over here&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;As we know &lt;strong&gt;SQL&lt;/strong&gt; is &lt;strong&gt;present&lt;/strong&gt; in our lives &lt;strong&gt;maybe even&lt;/strong&gt; since our &lt;strong&gt;birth&lt;/strong&gt; depending on when you have born. In my case I was born close to the 2000's so I am definitely in one of my governments SQL databases since then. And sometimes it is good to understand how the things work, I say sometimes but you usually need that when you are trying to improve something, and so that you can improve it you need to understand it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple SELECT
&lt;/h2&gt;

&lt;p&gt;Inside this article over &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/query-processing-architecture-guide?view=sql-server-ver16"&gt;here&lt;/a&gt; from Microsoft, they describe: &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/query-processing-architecture-guide?view=sql-server-ver16#process-a-select-statement"&gt;&lt;em&gt;Process a SELECT statement&lt;/em&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In this section we can see clearly the steps behind one SQL statement execution.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The basic steps that SQL Server uses to process a single SELECT statement include the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The parser scans the SELECT statement and breaks it into logical units such as keywords, expressions, operators, and identifiers.&lt;/li&gt;
&lt;li&gt;A query tree, sometimes referred to as a sequence tree, is built describing the logical steps needed to transform the source data into the format required by the result set.&lt;/li&gt;
&lt;li&gt;The Query Optimizer analyzes different ways the source tables can be accessed. It then selects the series of steps that return the results fastest while using fewer resources. The query tree is updated to record this exact series of steps. The final, optimized version of the query tree is called the execution plan.&lt;/li&gt;
&lt;li&gt;The relational engine starts executing the execution plan. As the steps that require data from the base tables are processed, the relational engine requests that the storage engine pass up data from the rowsets requested from the relational engine.&lt;/li&gt;
&lt;li&gt;The relational engine processes the data returned from the storage engine into the format defined for the result set and returns the result set to the client.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Looking at this list we can see that the first two steps are just some manipulation so that the third step can happen. At the final of the third step we will have the &lt;strong&gt;execution plan&lt;/strong&gt;. &lt;/p&gt;




&lt;p&gt;&lt;em&gt;The article above also describe a whole other world of details about everything that I am writing for you guys, so definitely check that out!&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;There are some interesting things about the plan that Microsoft describes in this article. They explain the general idea behind the the execution plan and give some interesting examples. Let's go through some facts about the execution plan:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is chosen by its cost (That is measured by amount of computing resources)&lt;/li&gt;
&lt;li&gt; It chooses the lowest estimated cost (In cases of thousands of execution plans it uses some complex algorithms to choose the best one without searching too much)&lt;/li&gt;
&lt;li&gt;Tries to be the fastest as possible&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A cool example with SQL Server 2022
&lt;/h2&gt;

&lt;p&gt;I have these two tables over here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;tblAuthors&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="k"&gt;identity&lt;/span&gt; &lt;span class="k"&gt;primary&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Author_name&lt;/span&gt; &lt;span class="n"&gt;nvarchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="n"&gt;nvarchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;tblBooks&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="k"&gt;identity&lt;/span&gt; &lt;span class="k"&gt;primary&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Auhthor_id&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="k"&gt;foreign&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="k"&gt;references&lt;/span&gt; &lt;span class="n"&gt;tblAuthors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Edition&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I just copied them from &lt;a href="https://www.mssqltips.com/sqlservertip/5148/populate-large-tables-with-random-data-for-sql-server-performance-testing/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I inserted some data to both of the tables and executed the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;TOP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tblAuthors&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tblBooks&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Auhthor_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And boom I got the following execution plan:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VeuNHA8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1poky1hn7c4hscnny8mu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VeuNHA8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1poky1hn7c4hscnny8mu.png" alt="Execution plan" width="777" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And look how amazing these algorithms are, they have chosen to start from the join table, look at how many rows we have fetched from tblBooks and how many from tblAuthors. They have chosen this way due to the index available in that table, in this case we could just get 50 records from tblBooks and make a index scan in tblAuthors with those 50 records.&lt;/p&gt;

&lt;p&gt;If you guys want to test an make your won execution plan download &lt;a href="https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver16"&gt;SQL Server Management Studio&lt;/a&gt;, unfortunately it is just avaiable for Windows. In linux over here I downloaded &lt;a href="https://dbeaver.com/download/lite/"&gt;DBeaver Lite Edition&lt;/a&gt;, and unfortunately it is under license this version, but anyway, there is a lot of things that we can do with the DBeaver Comunity version too.&lt;/p&gt;

&lt;h3&gt;
  
  
  And why make all of these plans and stuff??
&lt;/h3&gt;

&lt;p&gt;The Microsoft it self explains an example: In a construction, there are two companies responsible for building a house, each of them with the same blueprint, which one will be faster? The one that plans before it starts or the one that starts building without planning?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://br.freepik.com/fotos-gratis/centro-de-dados-com-racks-de-servidores-em-uma-sala-de-corredor-renderizacao-3d-de-dados-digitais-e-tecnologia-de-nuvem_20668653.htm#page=4&amp;amp;query=database&amp;amp;position=46&amp;amp;from_view=search&amp;amp;track=sph"&gt;Cover Image from DCStudio&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>programming</category>
      <category>database</category>
    </item>
  </channel>
</rss>
