WEBVTT

1
00:00:00.148 --> 00:00:01.923
Your page makes a hundred trips to the

2
00:00:01.923 --> 00:00:03.710
database when one would do.

3
00:00:03.798 --> 00:00:06.125
This is the N plus one problem.

4
00:00:06.223 --> 00:00:08.973
Your code fetches a list, then asks the database

5
00:00:08.973 --> 00:00:10.697
again for every item on it.

6
00:00:10.807 --> 00:00:13.844
A hundred items means a hundred and one trips.

7
00:00:13.950 --> 00:00:16.800
Every trip costs time, a connection and database

8
00:00:16.800 --> 00:00:18.855
work, and that means electricity.

9
00:00:18.947 --> 00:00:21.860
GreenCode researchers list the N plus one query

10
00:00:21.860 --> 00:00:24.144
among the worst energy wasters in code.

11
00:00:24.300 --> 00:00:27.003
Here is the fix you can use today.

12
00:00:27.120 --> 00:00:29.870
Ask for everything in one go, with a join or one

13
00:00:29.870 --> 00:00:30.838
batched query.

14
00:00:30.957 --> 00:00:33.732
Many database libraries can load the related data

15
00:00:33.732 --> 00:00:35.212
for you, if you ask.

16
00:00:35.361 --> 00:00:37.919
Now it is two trips, not a hundred and one.

17
00:00:38.099 --> 00:00:40.462
So check any loop that has a database call

18
00:00:40.462 --> 00:00:41.430
inside it.

19
00:00:41.528 --> 00:00:44.178
GreenCode's Constitution skill can help an AI

20
00:00:44.178 --> 00:00:46.667
coding agent spot and fix these.

21
00:00:46.755 --> 00:00:49.193
Then see which program draws the power, using

22
00:00:49.193 --> 00:00:50.682
tools like Scaphandre.

23
00:00:50.774 --> 00:00:53.249
GreenCode helps tackle this by checking code for

24
00:00:53.249 --> 00:00:56.313
quality, security and energy waste together.

25
00:00:56.438 --> 00:00:58.663
Wasteful calls like these come out as ranked

26
00:00:58.663 --> 00:01:01.756
defects, beside bugs and security issues.

27
00:01:01.844 --> 00:01:04.294
That was GreenCode Tech Tip number one.

28
00:01:04.394 --> 00:01:07.300
Follow for the next tip, and find more on

29
00:01:07.300 --> 00:01:09.110
greencode.ai/tech-tips.
