57.3 - Execution of An SQL Statement..mp4
57.3 - Execution of An SQL Statement..mp4
question arises, how is an SQL statement like the one that we saw? Right? Very simple SQL
statement. We want to select all the movie names from the movies table where the year is
greater than zero. We are just what we want. Now the big question here is how does an SQL
statement get executed, get executed to result in the solution that we want. So every
database, right? A database has a lot of software which helps us execute the SQL. That's one
of the big advantages of databases over flack files. You can just give your SQL query, you can
shoot your SQL query to the database. The database would return, would return the answer
that you want. That's the power of a database. So internally, what's happening here is an
SQL. So given an SQL query internally, what's happening? There are a couple of stages. I'll
briefly explain them to deeply understand them. There is a lot of computer science that you
need to learn. For example, so as soon as an SQL query is thrown into a database, the
database itself will contain lots of things. One of them is called a parser. A parser and a
compiler. What this does is it takes the SQL query, it understands the SQL query, it tries to
understand what is the SQL query trying to obtain. Okay. It tries to understand the query, it
tries to understand the query. And once it understands the query, parsing actually tries to
break your SQL statement or SQL query into smaller components. For example, it will say,
okay, it wants data only from movies table and it only wants the column movie name. And it
wants this. Wherever the year is greater than zero, it tries to parse this information, right?
And the compiler then generates code. Compiler then generates code in one of the
procedural languages like C or C Plus plus or Java or Python. Whatever is a programming
language the database uses. And most databases typically use C or C plus plus. Some
databases also use Java because they're extremely fast. So the parser and compiler takes the
SQL query, understands what is there in the SQL query, compiles them into a procedural
program. That's the first step. The second step is once this part is done, there is something
called a query optimizer. This is a very, very important part. So query optimizer says, given
this query, what is the optimal, what is the optimal way, what is the optimal way to execute
this query? And query optimizer is a whole area of research in itself. Like I know people at
top universities whose career has been spent researching on query optimizers. It's a vast
vast area. But in a nutshell it says, given this, given what? Because we already understood
what it wants, we have some code snippet. Again, code snippets in some databases can be
generated by the compiler. In some databases it can be generated after the query optimizer
also, right? Once the query optimizer decides what is the best way, what are the best data
structures to use, what is the best sequence in which I have to perform operations, right.
There are many, many components of a query optimizer. And then there is a query executor,
right? The query executor at the end of the day, sorry, the query executor at the end of the
day executes the query, right, the query executor. The query executor at the end of the day
executes the query, literally the code on the database. And whatever it retrieves, we get it
back, we get the results back, right? So these are, again, I'm simplifying it a lot. I'm
simplifying it by a wide, I have skipped lot of things, but it's good to understand that there is
a component within the database which parses the SQL query so that if there is any error in
the SQL query syntax, the parser throws an error saying yes, there is an error in your SQL
query. Right? The query optimize, the compiler again converts this into some procedural
language so that it can actually execute this. See, at the end of the day, you can't execute an
SQL query. You need code in C, C, Java, Python, because your computers will again convert
this code into machine level language and execute them. To understand how parsers and
compilers work very deeply, you have to know a whole subject called compiler design.
There is a whole subject called compiler design which explains how parsers and compilers
are designed. And this is a subject that we typically learn at undergraduate level computer
science, right? Again, a query optimizer, and this is a semester long subject, okay? It's not a
topic, it's a whole subject. Similarly, query optimizers is a very interesting area that typically
people learn at graduate level, at masters or PhD level, people who are pursuing PhDs or
masters in computer science or databases, they tend to learn deeper into how query
optimizers work. And then the executor basically ensures that all of the code, all of the
optimized code is executed and the results are given back. Right? So again, this is a very,
very simplified view. This is a very very simplified view, trust me. Right, and I'm just
informing you this because there is something called a query optimizer, which internally
optimizes your queries, and every database MySQL might have a different query optimizer.
IBM's databases will have a different query optimizer, Oracle's databases will have a
different query optimizer, etc. The parser compiler part of it also will differ from each
implementation. The standard here is the SQL. SQL is a standard. How the parser is
implemented how the query optimizer is implemented is up to the database designer, which
is either MySQL, or the engineers at Oracle or the engineers at Microsoft when they design
SQL server, et cetera. It all depends on how these guys, how the engineers at these teams
implemented these things. But SQL itself is an industry wide.