<?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: Amit Tiwary</title>
    <description>The latest articles on DEV Community by Amit Tiwary (@amitiwary999).</description>
    <link>https://dev.to/amitiwary999</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%2F656372%2F8d004bb2-9311-4778-8275-997921063881.png</url>
      <title>DEV Community: Amit Tiwary</title>
      <link>https://dev.to/amitiwary999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amitiwary999"/>
    <language>en</language>
    <item>
      <title>context in Golang</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sun, 28 Jan 2024 14:18:34 +0000</pubDate>
      <link>https://dev.to/amitiwary999/context-in-golang-39g7</link>
      <guid>https://dev.to/amitiwary999/context-in-golang-39g7</guid>
      <description>&lt;p&gt;There are times when we are required to abandon some task, for any reason, and also want to include some helpful reason for the cancellation. 'context' can be very handy in such a scenario. Golang has a 'context' package that contains the ‘Context' type and cancellation function. 'context' package was not a standard go package initially but it is so useful that later, in version 1.7, it was included in the Golang standard package(the latest version while writing this blog is 1.21). &lt;br&gt;
The ‘Context' type is an interface with four methods Done, Err, Deadline, and value. We are not required to implement this method to use context. These functions are implemented in the package and the 'context' package exposes functions WithCancel, WithCancelCause, WithDeadline, WithDeadlineCause, WithTimeout, and WithTimeoutCause that we can use. These functions return 'CancelFunc' and 'Context'(derived context). The below code explains the usage of context and how it can be used to abandon a task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func doJob(firstChildContext context.Context, secondChildContext context.Context) {
    defer wg.Done()
    for {
        select {
        case &amp;lt;-firstChildContext.Done():
            fmt.Println("first child exit")
            return
        default:
        }

        select {
        case &amp;lt;-secondChildContext.Done():
            fmt.Println("second child exit")
            return
        default:
        }
    }
}

func TryContext(flag int) {
    parentCtx, parentCancel := context.WithCancel(context.Background())
    firstChildContext, firstChildCancel := context.WithCancel(parentCtx)
    secondChildContext, secondChildCancel := context.WithCancel(parentCtx)

    go func() {
        wg.Add(1)
        doJob(firstChildContext, secondChildContext)
        fmt.Println("done with job")
    }()

    time.Sleep(5 * time.Second)
    switch flag {
    case 1:
        parentCancel()
    case 2:
        firstChildCancel()
    case 3:
        secondChildCancel()
    }
    wg.Wait()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TryContext function take a flag as input and based on flag value we cancel parent context or first context or second context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flag value 1:
first child exit
done with job

flag value 2:
first child exit
done with job

flag value 3:
second child exit
done with job
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the flag value is 1, we cancel the parent context and it signal all child Done channel but return from the first context. For the flag value 3, we cancel the second context and it signal the second context 'Done' and return etc.&lt;/p&gt;

&lt;p&gt;Another version of WithCancel function is WithCancelCause that take cause as parameter in the CancelFunc and we can get the error message using the context.Cause function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstChildContext, firstChildCancel := context.WithCancelCause(parentCtx)
firstChildCancel(errors.New("cancel first child"))

select {
    case &amp;lt;-firstChildContext.Done():
            fmt.Printf("first child exit %v\n", context.Cause(firstChildContext))
            return
    default:
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above code it is clear that the Context.Done is a channel. Context doesn’t automatically cancel what we are doing but we get function like WithCancel or WithTimeout that provide CancelFunc and CancelFunc signal Context Done channel and, then we have a handle in code that when we receive the signal then stop what we are doing. Calling the parent CancelFunc remove the parent reference to child, and it means that garbage collector is free to garbage collect the child.&lt;/p&gt;

&lt;p&gt;Similarly WithDeadline, WithTimeout function provide the CancelFunc but WithDeadline also signal the Done channel once the deadline cross i.e current time is greater than the time provided in deadline and WithTimeout when it timeout i.e when current time is greater than the duration plus the time when the WithTimeout function triggered.&lt;/p&gt;

&lt;p&gt;We can use the context to signal and stop the ongoing http request because it is taking too long to respond or something went wrong and we don’t the response any more. It will help us to save the resource. I will cover this example in different blog, 'network request in golang'&lt;/p&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>Defer in Golang</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 20 Jan 2024 16:28:17 +0000</pubDate>
      <link>https://dev.to/amitiwary999/defer-in-golang-1lk0</link>
      <guid>https://dev.to/amitiwary999/defer-in-golang-1lk0</guid>
      <description>&lt;p&gt;In my last &lt;a href="https://dev.to/amitiwary999/recover-from-panic-in-go-5hki"&gt;blog&lt;/a&gt; I used defer function and use resolve inside it. defer is one of the important feature of golang. defer statement execute just before the function, in which defer added, returns.&lt;/p&gt;

&lt;p&gt;One of the main uses of a &lt;strong&gt;defer&lt;/strong&gt; statement is to clean resources like network connection, open files etc. After program is done with these resources then these resources should be closed to avoid program limit exhaustion and the resource wastage. In last &lt;a href="https://dev.to/amitiwary999/recover-from-panic-in-go-5hki"&gt;blog&lt;/a&gt; recover is added inside defer function that make sure that if program panic then before exit defer statement execute and recover from panic. Take below code as an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;TryPanic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;defer&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inside main function first defer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;recover&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}()&lt;/span&gt;
    &lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inside main function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;panic in main&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;inside&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;
&lt;span class="nf"&gt;inside&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;defer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function panic still program doesn’t exit because we have recover inside defer.&lt;/p&gt;

&lt;p&gt;Example of free up resource after the use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;createFile&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="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inside-defer.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;defer&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;failed to create file&lt;/span&gt;&lt;span class="dl"&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;err&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;failed to write to file&lt;/span&gt;&lt;span class="dl"&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;err&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;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we are done with the file and before the program exit, it’s important to close the file so that resource can be saved.&lt;/p&gt;

&lt;p&gt;We can add multiple defer statement in a function although single defer statement can work. Multiple defer statement can helps to write the readable code where we clean different resource in separate defer statement. The multiple defer statement execute in LIFO order i.e that last defer execute first.&lt;/p&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>recover from panic in Golang</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 13 Jan 2024 12:22:43 +0000</pubDate>
      <link>https://dev.to/amitiwary999/recover-from-panic-in-go-5hki</link>
      <guid>https://dev.to/amitiwary999/recover-from-panic-in-go-5hki</guid>
      <description>&lt;p&gt;I am back after almost a year. Next few blogs will be on golang. Go is good language to build efficient and reliable software. Like any other programming language, there is possibility of error also in Go. Compile time errors detected by compiler before application run. Runtime time error occur during program execution, and if it is not handle properly, it can break the application and abort program. While writing code, a programmer try to cover all the error scenario, but there is possibility we might not handle some errors. When runtime error occur, it refer as program panicked. Runtime error is panic in golang.&lt;/p&gt;

&lt;p&gt;Recover is a built-in function provided by go to handle panic. A recover can stop a panic from aborting the program. A recover function can handle the panic in the goroutine that has implemented the recover and not all goroutine. You can refer the below code to understand how recover handle the panic in respective goroutine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package gortntry

import "fmt"

func panicFun(flag int) {
    if flag == 2 {
        defer func() {
            recover()
        }()
    }
    fmt.Println("inside the panic func")
    panic("panic in goroutine")
}

func TryPanic(flag int) {
    if flag == 1 || flag == 2 {
        defer func() {
            recover()
        }()
    }
    go panicFun(flag)
    panic("panic in main")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have two function TryPanic and panicFunc. TryPanic is the main entry function, and we call panicFunc inside it. Recover enabled in TryPanic for flag value 1 or 2 and in panincFunc for value 2. I have used defer here that I will explain in different blog post.&lt;/p&gt;

&lt;p&gt;When we pass flag value &lt;strong&gt;1&lt;/strong&gt; in TryPanic the output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;panic: panic in goroutine

goroutine 5 [running]:
golibcustom/goroutineplgrnd.panicFun(0x0?)
    /Users/amitt/Documents/Personal Data/personalproj/go-practice/goroutineplgrnd/gortn-panic.go:13 +0x50
created by golibcustom/goroutineplgrnd.TryPanic in goroutine 1
    /Users/amitt/Documents/Personal Data/personalproj/go-practice/goroutineplgrnd/gortn-panic.go:22 +0x64
exit status 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It means recover in TryPanic handle the main goroutine panic but not the panic that occur in other goroutine in panicFunc.&lt;/p&gt;

&lt;p&gt;For flag value &lt;strong&gt;2&lt;/strong&gt; there is no panic because both goroutine has recover function.&lt;/p&gt;

&lt;p&gt;If we use any &lt;strong&gt;other&lt;/strong&gt; value for example 3 then main goroutine panic and it abort the program without waiting for the panicFunc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;panic: panic in main

goroutine 1 [running]:
golibcustom/goroutineplgrnd.TryPanic(0x2)
    /Users/amitt/Documents/Personal Data/personalproj/go-practice/goroutineplgrnd/gortn-panic.go:23 +0x78
main.main()
    /Users/amitt/Documents/Personal Data/personalproj/go-practice/main.go:36 +0x20
exit status 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>go</category>
      <category>panic</category>
    </item>
    <item>
      <title>Understanding MySQL EXPLAIN output</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Mon, 27 Feb 2023 05:30:20 +0000</pubDate>
      <link>https://dev.to/amitiwary999/understanding-mysql-explain-output-part-2-2eg0</link>
      <guid>https://dev.to/amitiwary999/understanding-mysql-explain-output-part-2-2eg0</guid>
      <description>&lt;p&gt;In my last &lt;a href="https://dev.to/amitiwary999/get-useful-information-from-mysql-explain-2i97"&gt;blog&lt;/a&gt;, I wrote about how to read the output of the explain query in MySQL. I provided the information for all the columns except &lt;strong&gt;type&lt;/strong&gt; and &lt;strong&gt;extra&lt;/strong&gt;. In this blog post, I will cover &lt;strong&gt;type&lt;/strong&gt; column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;type&lt;/strong&gt; column provides information about how the tables are joined. Different types of the &lt;strong&gt;type&lt;/strong&gt; are described below.&lt;br&gt;
I used two tables student_details and address table. I have attached the table structure and respective index structure at the end of this blog.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;const&lt;/strong&gt;: If the &lt;strong&gt;type&lt;/strong&gt; is &lt;strong&gt;const&lt;/strong&gt; it means that there is at most one matching row. Since the number of matched rows is maximum one, it is treated as constant. When we used all the primary keys (if the primary key is a composite primary key) or all the ‘unique’ indexes to compare to a constant value to query data then the &lt;strong&gt;type&lt;/strong&gt; is &lt;strong&gt;const&lt;/strong&gt;.&lt;br&gt;
For example, in below query I used both the unique index(class, roll_no) to query the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM student_detail sd INNER JOIN 
address ad ON sd.address_key = ad.id 
WHERE roll_no = 12 and class = 4;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figz5z04erjozs3x1fvfp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figz5z04erjozs3x1fvfp.png" alt="const type" width="800" height="79"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;eq_ref&lt;/strong&gt;: It shows that one row is fetched from this table for each combination of rows of the previous table. If all the parts of the primary index or the unique not null index are used to fetch the data then the &lt;strong&gt;type&lt;/strong&gt; is &lt;strong&gt;eq_ref&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM student_detail sd INNER JOIN 
address ad ON sd.address_key = ad.id 
WHERE roll_no = 12;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiyoehi3w0lhv267y5spc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiyoehi3w0lhv267y5spc.png" alt="eq ref" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here all the rows of the student_detail tables is scanned because roll_no is not indexed (class, roll_no is indexed but not only roll_no. MySql can use class or class and roll_no for index but not only roll_no). There is only one row in the address table for each combination in the student_detail table(We used the id of address to join and it is primary key in address table).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ref&lt;/strong&gt;: If there are multiple rows in the table for each combination of rows of the previous table then the &lt;strong&gt;type&lt;/strong&gt; is &lt;strong&gt;ref&lt;/strong&gt;. If the index is not a primary index or not a unique index or the leftmost prefix of the index is used, that means it is not possible to get the single row for each column, then the type is ref.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM student_detail sd INNER JOIN 
address ad ON sd.address_key = ad.id 
WHERE state = 'Karnataka'; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl3m59tivpxsz7cnvijom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl3m59tivpxsz7cnvijom.png" alt="ref example 1" width="800" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;state is not primary or unique index, so for address table type is ref because there can be multiple rows for each state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;one more example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN SELECT * FROM address ad &lt;br&gt;
WHERE ad.state = 'Karnataka';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdfhw4m5305s5ts2h4ez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdfhw4m5305s5ts2h4ez.png" alt="ref example 2" width="800" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index_merge&lt;/strong&gt;: Sometimes MySQL may choose to merge the rows fetched after multiple range queries on the same table. This is shown as index_merge in the type&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;range&lt;/strong&gt;: If only the rows mentioned in the range are retrieved using the index then it appears as the range in the type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index&lt;/strong&gt;: MySQL saves all the indexes separately in storage. Sometimes for the query, only the index need to be scanned and it satisfies all the requirement.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the index is a covering index for the queries and can be used to satisfy all data required from the table, only the index tree is scanned. In this case, the Extra column says Using index. An index-only scan usually is faster than ALL because the size of the index usually is smaller than the table data.&lt;/li&gt;
&lt;li&gt;A full table scan is performed using reads from the index to look up data rows in index order. Using index does not appear in the Extra column.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPLAIN SELECT class FROM student_detail;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbk2und10vb8paj6hzv36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbk2und10vb8paj6hzv36.png" alt="index" width="800" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we query only the class column. It is indexed in the student_detail table and directly fetched from the index instead of going to table and fetch the data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ALL&lt;/strong&gt;: A full table scan is required to get the requested data. Lets take this query as example, we have used same query above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM student_detail sd INNER JOIN 
address ad ON sd.address_key = ad.id 
WHERE roll_no = 12;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here roll_no col is not indexed so its going to scan the every row of first table and then find the match row in the other join table. Normally, you can avoid ALL by adding indexes. &lt;br&gt;
But even if we add index we can't avoid it completely. Sometimes it happens that we are fetching too many rows in a query and query planner may decide that going through index and then fetching the row from the leaf of the B-Tree(index structure) is more costly then full table scan. For full table scan it needs to get the leftmost leaf once and then traverse only leaves because leaves are connected in chain.&lt;/p&gt;

&lt;p&gt;EXPLAIN is a powerful tool for optimizing MySQL queries. By understanding the output of EXPLAIN, you can identify performance issues and make changes to improve query speed.&lt;/p&gt;

&lt;p&gt;In my next blog, I will cover how to use the output of &lt;strong&gt;EXPALIN&lt;/strong&gt; to optimize the query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;student_detail table structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdbo45zd9rzfdlamek0mh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdbo45zd9rzfdlamek0mh.png" alt="student_detail" width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;student_details indexes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yreh0oahkzez8sop2gp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yreh0oahkzez8sop2gp.png" alt="student_detail indexes" width="800" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;address table structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqaopb6fj84i1fx2a7sy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqaopb6fj84i1fx2a7sy.png" alt="address table" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;address table indexes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppym28dib5g3h7fpgaq9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppym28dib5g3h7fpgaq9.png" alt="address table index" width="800" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>Understanding MySQL EXPLAIN output</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 18 Feb 2023 18:32:12 +0000</pubDate>
      <link>https://dev.to/amitiwary999/get-useful-information-from-mysql-explain-2i97</link>
      <guid>https://dev.to/amitiwary999/get-useful-information-from-mysql-explain-2i97</guid>
      <description>&lt;p&gt;MySQL is one of the most used RDMS. We write different types of queries in MySQL. Some used to be simple, and some quries used to be complex that includes even more than 4 - 5 tables. When we write complex queries, understanding the effect of the query is essential. Sometimes poor written complex queries can be the reason for a database's poor performance. We can use &lt;strong&gt;EXPLAIN&lt;/strong&gt; to find the keys used in the query and how we can optimize them. This blog is about how we can get useful information from the result of &lt;strong&gt;EXPLAIN&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The output of explain contains information on each table used in the query and in the order in which MySQL read the tables while processing the query. It means that MySQL reads the data from the table present in the first row, then finds the matching row in the table present in the second row and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9oqm086yaxa6vn6l0bz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9oqm086yaxa6vn6l0bz.png" alt="explain output" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Columns present the EXPLAIN output are &lt;strong&gt;id, select_type, table, partitions, possible_keys, key, key_len, ref, rows, filtered, type and extra.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;select_type&lt;/strong&gt;: There are different types of select. This column shows the types of select. The various types of selects are SIMPLE, UNION, SUBQUERY, and DERIVED.&lt;/p&gt;

&lt;p&gt;1.1 &lt;strong&gt;&lt;em&gt;SIMPLE&lt;/em&gt;&lt;/strong&gt;: Select is simple. It means no sub-query or union is used here.&lt;/p&gt;

&lt;p&gt;1.2 &lt;strong&gt;&lt;em&gt;Subquery&lt;/em&gt;&lt;/strong&gt;: Any select query used in another select query but not in the FROM clause. For example,&lt;br&gt;
&lt;code&gt;SELECT * FROM student_detail where address_key in (SELECT id from address where state = 'Karnataka');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr64zr7khlfvu70otxtfz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr64zr7khlfvu70otxtfz.png" alt="subquery" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.3 &lt;strong&gt;&lt;em&gt;DERIVED&lt;/em&gt;&lt;/strong&gt;: SELECT query used in the FROM clause of another SELECT query is a DERIVED select_type. For example, &lt;code&gt;SELECT id FROM (SELECT * FROM address) ad;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmopqo7dnksy7rutuigku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmopqo7dnksy7rutuigku.png" alt="derived" width="800" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.4 &lt;strong&gt;&lt;em&gt;UNION&lt;/em&gt;&lt;/strong&gt;: Second SELECT query in UNION or subsequent SELECT query type is a UNION select_type. For example, &lt;br&gt;
&lt;code&gt;SELECT state FROM address where id = 2 UNION SELECT state from address where id = 4 UNION SELECT state from address where id = 3;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fftloqxnjpb7eycwk1pla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fftloqxnjpb7eycwk1pla.png" alt="union" width="800" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;table&lt;/strong&gt;: Name of the table. It can be the table name or the name provided by us during a query or (derived table in the row with id vale n).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;partitioned&lt;/strong&gt;: Partition of the table from which the data is fetched. If the table is not partitioned then it shows a NULL value, or it may be possible that this column is not present in the output result if none of the tables is partitioned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;possible_keys&lt;/strong&gt;: It shows all the possible indexes from which MySQL can use the index to find the rows in the table. But this key is not dependent on the order of the table in the output result i.e it does not depend on the order of the table in which MySQL process the query. So it may be possible that none of the keys is used. I have explained it below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;key&lt;/strong&gt;: It shows the actual key that is used to find the rows in the table. Normally this key used to be one of the keys present in possible_keys, but it can be a different key too. For example, If it is optimized to fetch all the required columns from an index directly using the index scan then MySQL can use the index scan instead of the data row scan.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;key_len&lt;/strong&gt;: It shows the length of the key used to get the data. If the key is a multi-part key then key_len helps us to find how many parts of the multi_part key is used to get the data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ref&lt;/strong&gt;: It indicates which column is used against the column present in the key to fetch the data from the table. It can be const too i.e a const value is used to compare. For example, SELECT * FROM student_detail sd INNER JOIN address ad ON sd.address_key = ad.id WHERE sd.id = 2. In this query, the key used in the JOIN clause is also present in the where clause and it means that we are fetching a fixed number of rows using that key from the table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;rows&lt;/strong&gt;: It indicated the approx(not actual) number of rows need to examine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;filtered&lt;/strong&gt;: The filtered column indicates an estimated percentage of table rows filtered by the table condition. The maximum value is 100, which means no filtering of rows occurred. Values decreasing from 100 indicate increasing amounts of filtering. rows show the estimated number of rows examined and rows × filtered shows the number of rows joined with the following table. For example, if the row is 1000 and the filtered is 50.00 (50%), the number of rows to be joined with the following table is 1000 × 50% = 500. You can check the official document the MySQL.&lt;/p&gt;

&lt;p&gt;I will explain &lt;strong&gt;type&lt;/strong&gt; and &lt;strong&gt;extra&lt;/strong&gt; column usage in the next blog. Both required detailed explanation and one blog is not enough for this.&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>How SQL engine implement the isolation levels in MySQL</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 24 Dec 2022 18:47:21 +0000</pubDate>
      <link>https://dev.to/amitiwary999/how-sql-engine-implement-the-isolation-levels-in-mysql-4l5o</link>
      <guid>https://dev.to/amitiwary999/how-sql-engine-implement-the-isolation-levels-in-mysql-4l5o</guid>
      <description>&lt;p&gt;Isolation is one of the ACID properties of DBMS. Isolation ensures that each transaction is isolated from other transactions and that transactions don’t affect each other. There are multiple ways a transaction can be affected by other transactions. It is known as a &lt;strong&gt;read phenomenon&lt;/strong&gt;. Different types of read phenomena are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dirty read&lt;/strong&gt;: It happens when a transaction reads data written by another transaction that has not been committed yet. This is bad because we are not sure if that other transaction will eventually be committed or rolled back. So we might end up using incorrect data in case rollback occurs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non-repeatable read&lt;/strong&gt;: It happens when a transaction read the same row multiple times and the value is different each time because the row is modified by another transaction that is committed after the previous read. This is unexpected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Phantom read:&lt;/strong&gt; If the same query is executed multiple times but returns a different number of rows, due to some changes made by other recently-committed transactions, such as inserting new rows or deleting existing rows, it is known as a phantom read. For example, we want to query students that have heights between 5’’ to 6’’. It might possible that a new student is added, while the previous read transaction is still in progress, whose height is between 5 to 6 feet, in that case, the query will return this newly added user too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lost Update&lt;/strong&gt;: It happens when two different transactions are trying to update the same column on the same row of a table and one transaction overrides the update of another transaction. &lt;/p&gt;

&lt;p&gt;For example, there are 4 students with id id1, id2, id3 and id4. id1, id2 belongs to team A and id3, id4 belongs to team B.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;select&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;students_team&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="c1"&gt;----+-------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;team&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="c1"&gt;----+-------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;id1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;id2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;id3&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;id4&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first transaction updated team A to B but before it &lt;br&gt;
commit, the second transaction already started that updated team B to A. Once the first transaction commits team A converted to team B.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;After&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;update&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;transaction&lt;/span&gt; &lt;span class="nx"&gt;doing&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;read&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;same&lt;/span&gt; &lt;span class="nx"&gt;transaction&lt;/span&gt;
&lt;span class="nx"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;students_team&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+----+-------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+----+-------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id3&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id4&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So all the students belong to team B now. The second transaction converts team B to A. Because all students belong to team B now, the second transaction converts all of them to team A.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="nx"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;students_team&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+----+-------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+----+-------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id3&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;id4&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SQL engine offers four isolation levels to handle the read phenomenon. In this blog post, we are going to discuss what are those four isolation levels and how SQL engines ensure these isolation levels.&lt;/p&gt;

&lt;p&gt;SQL use locking to protect a transaction from seeing or changing data that is being queried or changed by other transactions. SQL use different locking strategy to for different isolation levels&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read uncommitted&lt;/strong&gt;: In read uncommitted isolation level one transaction can read the changes of another transaction even if the other transaction update is not committed. &lt;strong&gt;SQL engines don’t lock any row in this isolation level&lt;/strong&gt;. So one transaction can affect the other transactions. All the read phenomenon is possible at this isolation level.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read committed&lt;/strong&gt;: If a transaction tries to read a row which is already locked by another transaction then until that transaction is completed it will wait and once the transaction is committed new changed value will be used. &lt;strong&gt;Here exclusive lock is used by the SQL engine&lt;/strong&gt;. So if any update transaction comes, it acquires the exclusive lock and changes the value and releases the lock. Other transactions read the value after that. Read committed resolve the dirty read issue but another read phenomenon is still. Because it doesn’t prevent the new insertion or update in data even if any other transaction is reading the same row.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Repeatable Read&lt;/strong&gt;: Repeatable Read isolation is achieved by &lt;strong&gt;placing a shared lock on the row read by each transaction&lt;/strong&gt; and the lock is held until the transaction is committed. Until the lock is removed other transactions can’t change the data of row held by a different transaction. But it doesn’t prevent the new row insertion. So it prevents dirty reads and non-repeatable read but phantom read is still possible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Serializable&lt;/strong&gt;: SQL engine holds the lock on the row until is committed. It is similar to a Repeatable read but if required locks can be acquired on the key range, preventing other transactions from updating or inserting rows into the data set until the transaction is complete. It helps to prevent phantom reads. This is the most strict isolation level and it prevents all the read phenomena.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;MySQL innodb engine default isolation level is Repeatable Read.&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>How sql engine handle join internally - part 2</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 10 Dec 2022 13:17:05 +0000</pubDate>
      <link>https://dev.to/amitiwary999/how-join-works-internally-in-sql-part-2-142m</link>
      <guid>https://dev.to/amitiwary999/how-join-works-internally-in-sql-part-2-142m</guid>
      <description>&lt;p&gt;In the last &lt;a href="https://dev.to/amitiwary999/how-join-works-in-sql-2ja4"&gt;blog&lt;/a&gt;, I wrote about the physical joins used by the SQL engine. Before version 8, MySQL had only nested loop join, but hash join was included in version 8. Postgresql and sql server has nested loop join, hash join and merge join. In this blog post, I will explain how SQL engine decide which physical join to use.&lt;/p&gt;

&lt;p&gt;When two tables joined then, both tables traversed once or multiple times to find the matched data. &lt;strong&gt;Merge join&lt;/strong&gt; is best because it requires traversing both tables once. Merge join requires both inputs to be sorted on join keys or the join key is clustered index. It traverses the outer table and finds the matching data in the inner table. Once it found the data, then it moved to the next row. If data is not found, then move to the next row of the table that has the lowest value. &lt;/p&gt;

&lt;p&gt;Since both tables are sorted on the join key, it can move to the next row of the table that has the lowest value because the value in the next row will be higher than the current row. There is a chance that the value in the other table has a high value that can be found in the next row. If the table is not already sorted, then the SQL engine may decide to sort it and do the inner join if it is more optimized. But for that, it is necessary that the SQL engine should be able to sort the data on the join key. Also, one more important thing for merge join is that there should be at least one equijoin(=) expression.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nested loop&lt;/strong&gt; join traverses through the inner table multiple times. For each value of the outer table, it traverses the inner table to find the matched value. If the join key is indexed in the inner table then it will use to find the data in a more optimized way. A nested inner join is used when one table is small and the other is large. The small table is used as the outer table and the large table is used as the inner table. The inner table is traversed for each row of the outer table that’s why it is more time-consuming. &lt;/p&gt;

&lt;p&gt;If the outer table has n elements and the inner table has m elements then the time complexity can be n*m or n*logm(if the join key is indexed, then the index will use to find the data in the table). If it requires sorting the data on the join key and this operation is expensive then the loop through all rows to find the matching data, then nested loop join might prefer over merge join.&lt;/p&gt;

&lt;p&gt;We discussed merge join and nested loop join and &lt;strong&gt;hash join&lt;/strong&gt; need to discuss. There are two phases in hash join. One phase is the build phase and the other is the probe phase. One of the tables is used in the build phase and the other is used in the probe phase.&lt;/p&gt;

&lt;p&gt;In the build phase, all the rows of the build table are scanned and used to generate the hash key(using a hash function) and save it in the in-memory hash table. In the probe phase, we go through all the rows of the probe table and generate the hash key using the hash function and the join key of the probe table. Find the hash key in the in-memory hash table. If the key is found then join the row of both tables. Key lookup is fast in the hash table. Like merge join, at least one equijoin(=) expression requires in hash join too.&lt;/p&gt;

&lt;p&gt;The hash table generated during the build phase is saved in the in-memory that’s why we chose the small table for the build phase so that less memory is used. If there is a memory issue, some of the partitions of the hash table are swapped to tempdb and whenever there is a need it is loaded in the cache.&lt;/p&gt;

&lt;p&gt;If the join table is large and there is no index on the join key then the hash join can be very efficient. Merge join works If the join key is indexed, nested loops join need to go through all the rows of the inner table again and again and if the table is very large then it can be expensive.&lt;/p&gt;

&lt;p&gt;You don’t need to specify which join type SQL should use. SQL engine is intelligent enough to find the optimized physical join to use depending on the conditions. But you can make sure you are doing your best to design the database schema, index the column and do the join query using the correct columns.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>mysql</category>
      <category>database</category>
    </item>
    <item>
      <title>How sql engine handle join internally-part 1</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 03 Dec 2022 18:36:21 +0000</pubDate>
      <link>https://dev.to/amitiwary999/how-join-works-in-sql-2ja4</link>
      <guid>https://dev.to/amitiwary999/how-join-works-in-sql-2ja4</guid>
      <description>&lt;p&gt;There are different types of logical join in SQL. Inner join and outer join are mainly used join. When we need to get the data from tables we use the join. But how do these joins work? Physical join is implemented inside RDMS. The user uses a logical join to write the query and a physical join is used by RDMS to do join operations. Part 2 of this blog post is &lt;a href="https://dev.to/amitiwary999/how-join-works-internally-in-sql-part-2-142m"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are three different physical joins Nested loop join, Hash Join and merge join. Depending on the data in the joined tables, RDMS use any one physical join. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nested loop joins&lt;/strong&gt;: In nested loop join, out of the two tables, the table with a smaller number of records is selected, and it will loop through the second table until matches are found. This is available in MYSQL, Postgres and even in SQL servers too. This is not a scalable option for a large table. It is used mostly when the join operator doesn’t use equality. MySQL has only this option in versions lower than MYSQL 8.&lt;br&gt;
For example lets say we need to get the students that height is smaller then the teacher.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
    s.student_id, 
    t.teacher_id, 
    s.student_name, 
    t.teacher_name 
FROM 
    student s, teacher t 
WHERE 
    s.height &amp;lt; t.height
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Hash Join&lt;/strong&gt;: Hash join is a way of executing a join where the hash table is used to find the match record. A Hash table creates in the memory. If the data is very large and memory is not sufficient to hold the data then it writes to disk. It is efficient compared to the Nested loop join. During the execution, RDMS build the in-memory hash table where rows from one of the join tables is stored using the join attributes as the key. Once it is done then the server starts reading rows from another table and finds the matching row from the hash table. This is used mostly when the join operators use equality. &lt;br&gt;
Lets say we created two table. First table is student table and second table is class table. Each student belongs to one of the class. Now when we join these two table using the class id to get the student and details of class that this student belongs to, then sql server may use the hash join.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 
    s.student_Id ,
    s.student_name ,
    c.fund ,
    c.strength 
FROM 
    student AS s 
    JOIN
    class AS c ON 
        s.class_id = c.id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4l331cl052uximpmwqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4l331cl052uximpmwqi.png" alt="hash-table" width="372" height="347"&gt;&lt;/a&gt;&lt;br&gt;
When we do the join sql server create the hash table using the class table. 'class_id' is the key here. Now when sql go through student table, it get the class data from hash table using the class_id.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Merge Join&lt;/strong&gt;: Merge join is used if the join condition uses an equality operator and both sides of the join are large. Merge join uses the sorted data inputs. So if there is an index on the expressions used in the join column then it is used to get the sorted data. But if the server is doing any operation to sort the data then please look at the indexes and better try to modify the indexes in order to achieve better results.&lt;/p&gt;

&lt;p&gt;Read part 2 &lt;a href="https://dev.to/amitiwary999/how-join-works-internally-in-sql-part-2-142m"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>How spring boot application work as web application</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 26 Nov 2022 18:31:23 +0000</pubDate>
      <link>https://dev.to/amitiwary999/how-spring-boot-application-work-as-web-application-abb</link>
      <guid>https://dev.to/amitiwary999/how-spring-boot-application-work-as-web-application-abb</guid>
      <description>&lt;p&gt;Spring boot has everything to create a web application and makes web application development fast and hassle-free. The embedded server makes web application creation easy in Spring boot. If embedded server doesn't exist, then we required to install web server like tomcat and deploy application there. After that, only we can use our Java application as web application. But with the help of embedded server, when we create the deployable unit of application i.e JAR, web-server becomes part of it.&lt;/p&gt;

&lt;p&gt;Tomcat is the default embedded server for the Spring boot application. So we don't need to do anything to add tomcat in our Spring boot application because it comes with spring boot starter web.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;

 &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
 &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;

&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If any client makes a request to our Java server, tomcat handles the request.This functionality is made possible by the HTTP Connector element. Two important attributes of HTTP connector are protocol and SSLEnabled. The protocol attribute defines the protocol the connector will use to communicate and bydefault set to HTTP/1.1. SSLEnabled attribute helps connector to decide to use SSL handshake/encryption/decryption. Tomcat has more than one HTTP connector, and we can use anyone. In this blog we will talk about NIO connector. &lt;/p&gt;

&lt;p&gt;The NIO connector do non blocking I/O. NIO connector has two thread pools, one holds the poller thread, and other holds the worker thread. Whenever a client makes a request to our Java server, NIO connector uses a thread from poller thread pool and keeps the connection alive. It push these request to handle by the thread from worker threads. Size of both pools are configurable.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
    </item>
    <item>
      <title>Hide credentials in spring boot</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 12 Nov 2022 16:39:43 +0000</pubDate>
      <link>https://dev.to/amitiwary999/hide-credentials-in-spring-boot-1oc0</link>
      <guid>https://dev.to/amitiwary999/hide-credentials-in-spring-boot-1oc0</guid>
      <description>&lt;p&gt;Spring boot make it easy to create spring applications. It makes it easier and faster to set up, configure and run the web application. When I was working on one of the projects, I was required to use some credentials. But I can't use it directly in the code. It should be hidden and still accessible in the code. We know that we can add the variable in the application.properties file and then use it in the java code. But I was not ablw to find how can add the env variable in a file and then access it in the application.properties file. If variables can be added in a file and accessed in the application.properties then the file can be included in the gitignore, and code can be pushed to github and shared with others without exposing the credential. &lt;/p&gt;

&lt;p&gt;So I did research and find out that we can import a file in the application.properties and use the variables. We are going to use the env.properties file to save the credentials. Create a env.properties file. I created this file in the resources folder, so that I can easily access in the application.properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flks00ax9mx84sahfva87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flks00ax9mx84sahfva87.png" alt="folder to save the env.properties" width="410" height="202"&gt;&lt;/a&gt;&lt;br&gt;
Add some credentials in the env.properties file like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB_USER=name_of_sql_db_user
DB_DATABASE_NAME=name_of_database
DB_PASSWORD=database_password
GOOGLE_API_KEY=google_api_credential
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the secret info and can't be shared with everyone. But it is required to connect with the database server or to use the google service.&lt;/p&gt;

&lt;p&gt;Now to access these variables in our java file, we have to import these variables in the application.properties file. Import the env.properties file so that we can get these variables in the application.properties file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.config.import = env.properties
spring.datasource.username = DB_USER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;env.properties file is in the same folder with the application.properties so it can be imported directly using the file name. We are telling the code to use the config from the import file i.e from env.properties.&lt;/p&gt;

&lt;p&gt;Now to make sure that the credentials is not commited and pushed to github, we have to make sure that env.properties file is included in .gitignore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/src/main/resources/env.properties
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>springboot</category>
      <category>security</category>
    </item>
    <item>
      <title>How FullText index works in MySQL</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sun, 18 Sep 2022 06:56:05 +0000</pubDate>
      <link>https://dev.to/amitiwary999/fulltext-index-in-mysql-m1f</link>
      <guid>https://dev.to/amitiwary999/fulltext-index-in-mysql-m1f</guid>
      <description>&lt;p&gt;MySQL is one of the most used relational database management systems. We all know that it is fast and easy to use. In MySQL, storage engines are the components that handle the SQL operations. InnoDB is that widely used storage engine in MySQL. One of the reasons that make SQL operations fast is the index. If you create the index correctly, your query can be very fast. We can create an index on int, char type(one word) columns, but we can also create a FULLTEXT index on the text-based columns where we save multi-word strings. In this blog, I will explain how the FULLTEXT index works.&lt;br&gt;
We can create a full-text index while creating a table or altering the table and creating the index. InnoDB full-text indexes have an inverted index design. What is the inverted index? Inverted index stores a list of words and mapping of that word to the document in which that word appears. When we create the FULLTEXT index, InnoDB creates a hidden column FTS_DOC_ID. This FTS_DOC_ID is used in the index table to map the word to a document. What is an index table? When we create a FULL-TEXT index, then a set of index tables also get created automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1axafcp9cb42iznssuct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1axafcp9cb42iznssuct.png" alt=" " width="800" height="347"&gt;&lt;/a&gt;&lt;br&gt;
The index table is associated with the indexed table by the hex value. For example, in the above image, the indexed table table_id is 2243 and, in hex, it is 8c3. So index table FTS_00000000000008c3&lt;br&gt;
The first six index table saves the mapping of the word and document id(FTS_DOC_ID) in which that word appears. When any new document(data in a column that has a FULLTEXT index) is inserted, it is tokenized and an individual word(known as a token) is inserted in the index table along with the doc_id of that document and the position of the word in the document. We don’t insert the stop words. StopWords are the list of commonly-used words that we can ignore for the purpose of the FULLTEXT index. YOu can get the default stop words from the table INNODB_FT_DEFAULT_STOPWORD. The words are partitioned among six index tables based on the character set sort weight of each word. If any word is already present in the index table then we update and include the doc_id of the new document. If we do this full process for each document, it may result in numerous insertions in the index table that can make concurrent access to these tables a point of contention. To avoid this problem InnoDB uses the cache. InnoDB temporarily caches index table insertion for recent rows, and holds the insertion until the cache is full. Once the cache is full, InnoDB does batch flushes to disk. The caching and batch flushing behaviour avoids frequent updates to index tables that could result in concurrent access issues during busy insert and update times. The batching technique also, avoids multiple insertions for the same word and minimizes duplicate entries. Instead of flushing each word individually, insertions for the same word are merged and flushed to the disk as a single entry, improving insertion efficiency while keeping index tables as small as possible. INNODB_FT_CACHE_INDEX_TABLE holds the newly inserted rows in a FULLTEXT index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Index deletion&lt;/strong&gt;&lt;br&gt;
Deleting a record that has a full-text index column could result in numerous small deletions in the index tables, making concurrent access to these tables a point of contention. To avoid this problem, the DOC_ID of a deleted document is logged in a special FTS_&lt;em&gt;&lt;em&gt;DELETED[1] table whenever a record is deleted from an indexed table, and the indexed record remains in the full-text index. Before returning query results, information in the FTS&lt;/em&gt;&lt;/em&gt;_DELETED table is used to filter out deleted DOC_ID. The benefit of this design is that deletions are fast and inexpensive. The drawback is that the size of the index is not immediately reduced after deleting records. To remove full-text index entries for deleted records, run OPTIMIZE_TABLE on the indexed table with innodb_optimize_fulltext_only=ONto rebuild the full-text index.&lt;/p&gt;

&lt;p&gt;MySQL performs a full-text search using the MATCH() AGAINST() syntax. We provide all the columns, separated by a comma, that need to be searched in MATCH and a string that need to search in AGAINST. Let's assume that the columns provided in the MATCH are indexed. When we query, then InnoDB checks index tables for the string. If the string is present then it fetches the DOC_ID and gets the row using the DOC_ID.&lt;/p&gt;

&lt;p&gt;[1]: FTS_00000000000008c3_DELETED in above image contain the document IDs (DOC_ID) for documents that are deleted but whose data is not yet removed from the full-text index. FTS_00000000000008c3_DELETED_CACHE is in-memory version of FTS_00000000000008c3_DELETED. We first save it in cache and once cache is full, flush it to FTS_*_DELETED table. &lt;/p&gt;

</description>
      <category>mysql</category>
      <category>search</category>
      <category>sql</category>
      <category>index</category>
    </item>
    <item>
      <title>localhost not point to correct ip address in node 18</title>
      <dc:creator>Amit Tiwary</dc:creator>
      <pubDate>Sat, 02 Jul 2022 17:53:38 +0000</pubDate>
      <link>https://dev.to/amitiwary999/localhost-not-point-to-correct-ip-address-in-node-18-4onl</link>
      <guid>https://dev.to/amitiwary999/localhost-not-point-to-correct-ip-address-in-node-18-4onl</guid>
      <description>&lt;p&gt;Nodejs is becoming very popular among developers for backend development. There are lots of resources to learn it. Recently, I faced an issue when I was running my node application. I am using MySQL for the database and use MySQL npm library to connect to my database and query data. Before you start doing the query, you need to config the MySQL library and then only it can connect to your database. You must pass the IP address where your database is hosted, user name, and password. I was testing locally, so I ran the MySQL server on my machine and used the localhost as the IP address to connect to it. But I was getting the continuous error that the library was not able to make connection to SQL. After research, I found that I am using node 18 in my system and in node 18 localhost is converted to ipv6 instead of ipv4. So I need to add the IP address of localhost i.e 127.0.0.1 to connect to my local SQL server.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
