Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 30, 2022 12:03 am GMT

The basic of the advanced SQL queries: Tuning

In this article I wanna show you things about SQL that I believe will make a difference in your queries.

First of all, how does SQL work? This point can be obvious for some one, but I think that could be mentioned here. Keep in mind, a query SQL works in order: FROM, WHERE, SELECT.

Look the table teachers

id_teacherid_schoolsituation
111
211
321
410

For example, in a query:

SELECT teachers.* FROM teachers WHERE teachers.id_school = 1

The database will search all teachers, then search which one has a column id_school equals 1, so after this put in columns from your select, in this case, all columns of table teachers.

Output:

id_teacherid_schoolsituation
111
211
410

Well, this is pretty simple, you know? Let's make this more interesting Think about, your employer tells you to make a query that returns all teachers who are actively working in some school.

Let's consider the table above to the teachers and the following table to the school:

id_schoolsituation
11
21
30

The first thing that comes to mind is something like this:

SELECT teachers.* FROM teachers JOIN school ON (school.id_school = teachers.id_school) WHERE teachers.situation = 1

Output:

id_teacherid_schoolsituation
111
211
321

Well this works, but how do I make this better? We know that SQL queries work on clause FROM first, after this, it goes to JOIN clause. With this in mind, what is easier to the database, search all of the teachers and then look who have a school linked or search all of the schools and then look who are the teachers?

The logic tells us that the table school is less than the table teachers, so we can conclude that it is easier for the database to look for the school table. Like this:

 SELECT teachers.*  FROM school  JOIN teachers ON (teachers.id_school = school.id_school) WHERE teachers.situation = 1

Output:

id_teacherid_schoolsituation
111
211
321

The same result, but with better performance. This is one of the most important lessons that I learned about SQL.

Okay, we make a little deep into tuning SQL queries, come one continue talking about. The second point that I want to talk about are subqueries. In most cases you DON'T need this!

Subqueries are a vampires of SQL query, they are common used when you want put values from other table per line, like when you want to put in line the sum values of items from a order with you SELECT.

Consider the follow tables to the order and order_itens:

Order:

idcostumer
11
21

Order Itens:

idid_ordervalue
1193.3
2112.99
3210.19
4212.5
5279.99
6399.13

For example:

SELECT order.*,       (SELECT SUM(order_itens.vl_item)        FROM order_itens        WHERE order_itens.id_order = order.id) AS vl_total_order FROM order

Output:

idcostumervl_total_order
11106.29
21102.68

This query returns what we want, all of the orders and the value of all items from the order. Okay, but how do I make this better?

The first thing that I suggest you do is detach this query, if this is possible, is the better option in my opinion. But if not, in SQL you have another option to do this exemple, we can make the query above with the WITH function.

From now i don't will talk more about this function, but maybe I can make another document just to talk about this. In fact if you don't know how to make a WITH correct, they can be more hindrance than a help, so be careful.

Ok, but sometimes we can run away from subqueries, for example you have to check if the line result exist on another table. Something like:

SELECT order_itens.* FROM order_itens WHERE order_itens.id_order IN (SELECT order.id                                FROM order)

Output:

idid_ordervalue
1193.3
2112.99
3210.19
4212.5
5279.99

Of course, the better way that we can do this, is with JOIN. But if you table don't have a PK and sequences in well defined, or something else, maybe with this will get more performance:

SELECT order_itens.* FROM order_itens WHERE EXISTS (SELECT 1               FROM order               WHERE order.id = order_itens.id)

Think about this, in this example we only need to know if line exists in another table, so just take the first one you find and done! This it may seem a small thing, but when we talk about tuning, we have a lot of small things that make a difference together.

Thats it! Thank you for reading, I will hopefully than this tips help you in your journey as a developer SQL. See you later!


Original Link: https://dev.to/leoramos/the-basic-of-the-advanced-sql-queries-tuning-21d5

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To