Goto main content

help desk

sql and performance

How can you optimize or avoid to have a sql request inside a for loop.

Asked on 2020-01-24 00:00:00

OFFICIAL ANSWER

Consider the following code:

for i sql(…) do 
    …   
    res = sql(…);
    …
endfor

If possible, try to combine the 2 sql using a join or a stored procedure.

Here are the different types of the JOINs in SQL:

  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

Reference :
w3school : https://www.w3schools.com/sql/sql_join.asp

Answer by:
Pierre Laplante

Replied on: 2020-01-24 00:00:00