Shadowing-Übung: SELECT COUNT (*) can impact your Backend Application Performance, here is why - Englisch Sprechen Lernen mit Video

Lektion wird erstellt...
1
aggregating large entries in the database to perform a count for
2
example is a lot of work the database has to sort
3
through large number of records whether it's in an index
4
or in the raw heap table itself doing this too often can impact the performance of both your database
5
and your application let us discuss why count can be slow and an alternative if you really don't want the actual count, but yes, you want an estimate.
6
Let's just jump into it.
7
All right, guys.
8
So there are many ways you can execute a count on your data.
9
So I have here a grades table, our famous student grades table.
10
So there is a G, which the grade itself.
11
That's the ID of the students, right?
12
And I think it has like around 60 million rows that I created.
13
So what I'm going to do here is do a select count G from grades,
14
where id between 1000 and 4000 give me the grades of those people
15
but i just want to count if you execute that it's
16
so fast right now don't pay attention to the speed
17
because i have caching going on i executed this query many times
18
but i want to pay attention to the number that come back
19
2900 and the reason this number is a little bit low from comparing 2000
20
and 4000 which around should be around 3000
21
because count g will return entries that are not null
22
and just just think about it i have an index on the id field
23
that means the database have used
24
that index to pull the rows in order to count them
25
so we're on the index but we asked the database to do a count on g
26
that means we had to go to the table to check those uh the value of g whether it's null
27
or not So let's do an explain.
28
Let's just add an explain analyze before these puppies.
29
Let's see what happened here.
30
Why do that?
31
Let's pay attention to what happened here.
32
We're doing an index scan on Postgres.
33
We have returned 3001 rows because guess what?
34
We're in the index.
35
The actual index entries, I don't have any deleted rows by the way here.
36
So 3001 is about right, but the final count has been reduced
37
because we went back to the table to get the value G because that's what we asked it.
38
We ask, hey, count the value G.
39
And when you do count G here or count any field, the database will filter through the fields and only count the not null values.
40
And I have few nulls there.
41
Okay.
42
So now it uses an index scan.
43
And that tells me that, hey, I scan the index
44
but i had to go to the table it's not an index only scan right let's spice things up
45
and see do the same thing here
46
but i'm going to do a select star this time
47
and a lot of people have a misconception
48
that count star actually goes to the table and fetch all the fields
49
and count the fields no almost no database do this anymore
50
right count star essentially means just count whatever entries you have right
51
this will include null values if you're scanning the index give me the values right
52
and you can see that we got a higher number
53
so let's take a look at the plan that did
54
that postgres used to do that stuff what did you use postgres tell me
55
if you will look at the plan now look at this it's It's an index only scan
56
and always index only scan always trumps
57
and always it's better than the actual index scan because I don't really need to go back to the table.
58
Again, don't pay attention to these numbers because I have caches all over the place.
59
I just want you to, the most important thing is to understand the plan.
60
Running these numbers don't mean anything right now because first of all, I'm in a container.
61
I have a large amount of memory, so the database will start caching these pages if I execute them over and over again.
62
But just understand that plan is the most important thing.
63
And as a result, the larger the number of rows come back, the more work the database is doing.
64
So 118 milliseconds.
65
Index only scan.
66
All right, Jose, what are you trying to do here?
67
Here's what I'm going to do.
68
I'm going to do an update now.
69
I'm going to do an update grades.
70
Set g equal 20, where id is between 1,000 and 4,000.
71
So those rows between 1,000 and 4,000, I'm going to slam all of them and update them.
72
Change this.
73
Let's see how the Postgres will freak out now.
74
What will happen?
75
I'm going to execute this count star, and then let's see what will happen.
76
all of a sudden guys look this number jumped again not by much
77
but it is significant the more rows the more actual real data you have this will go back
78
but but look at that it still says index only scan
79
but look at this heap features i want you to pay
80
attention to this the moment you start seeing heap features that means index only scan yeah we did only scan the index,
81
but we had to go back to the table 6,002 times, right?
82
For this amount of values, right?
83
I'm not sure these are the blocks or the actual rows.
84
I have to go back and check.
85
But we had to go back this amount of time, which is expensive, right?
86
Why?
87
because we have updated the values the visibility map told the index scanner
88
that hey by the way yeah you're scanning the index
89
and i have i'm going to give you only values in the index which is
90
usually fast again if you're not scanning the whole index
91
but these rows that i'm scanning in the index might have been updated might have been deleted
92
so i have to go back to the heap
93
where the actual visibility of the row exists to check if the row is actually deleted.
94
Because when you delete something in Postgres, the index is not immediately marked as deleted.
95
It just adds a new record and keeps the old records for MVCC reasons,
96
so multiple concurrency control, so other transactions can't see those old tuples.
97
All right.
98
So how do we fall?
99
How do you solve this problem?
100
Very simple.
101
You just vacuum the table.
102
Vacuuming the table will update the visibility map saying that, by the way, those old rows that you just updated, nobody's reading them.
103
Nope.
104
There are no running transactions that are reading them.
105
In a production system, there might be, but not now.
106
So if I do now the same query again, so if we do it again, you can see that we got heap features zero.
107
All right.
108
And guys, every time I increase that number 3004, you can see that this is going to get slower and slower and slower.
109
Just to show you that, for example, now it took one and a half second to execute.
110
And then if I increase that number a little bit, you can see, oh, you can feel it.
111
So count is not cheap, right?
112
It was cheap for the 3,000 rows that I'm going to return.
113
But every time you do that, the plan now changes.
114
It says, okay, we're still going to do an index, but I'm going to use threading.
115
The database decided to do multiple threads, multiple workers, not necessarily threads, multiple workers to scan that index.
116
So I can give you the results, right?
117
Still, we're good we didn't do any heap features
118
but it took six seconds to return this many rows right so every time you increase that number goes larger
119
the operation is going to go slower.
120
It's just proportional.
121
It's just so proportional.
122
So all right.
123
So what if I, I'm saying I don't care about the actual row count, right?
124
I don't really care about giving the actual exact value, right?
125
But here's what I want to do.
126
I want you just to give me an estimate.
127
If you do just an explain, right?
128
And let's just format this so it's JSON-y, right?
129
If you do that, without an analyze again, without an analyze, analyze actually execute the query.
130
Explain will not execute the query, but it will estimate it.
131
It will estimate that, hey, I'm going to use the index.
132
I am planning that I might get this much rows back, 2,868, right?
133
So compared to the select count, so it's not an accurate number.
134
It's 200 values up.
135
And a lot of people use this.
136
And I actually got this trick from a blog that I'm going to reference below.
137
Let's remember that the table itself has some statistics to update itself.
138
So Postgres, without actually looking at the table, it knows roughly how many rows are on the table.
139
roughly how much rows will come back are this are these actual correct numbers absolutely not
140
but if you're building instagram or you're building something
141
that like count the likes or something like
142
that this is way better right so i can quickly estimate this stuff
143
and as you update your table obviously a good idea to do an analyze on your grades table
144
or your table that will update the statistics to the correct numbers
145
and obviously this operation is going to take a long time all right guys that's it for me today
146
that was count and how count is essentially a lot of work for the database
147
and if you really don't need an actual count correct number especially
148
if it's in the millions why would you show the the user 60 million
149
and 320 exact number right almost no one does that right
150
and let's see yeah so it's on estimate always matter
151
if you're working with a fewer rows you can do an actual account but
152
if you expect the table to grow avoid you using an
153
excellent account use do an estimate with a planner like that
154
if that works for you hope
155
that that's way more than enough all right again guys i'm
156
gonna see you in the next one you guys stay awesome goodbye

Wortschatz und Sprechhinweise zu dieser Lektion

Diese Sprechübung auf Niveau C1 basiert auf dem Video „SELECT COUNT (*) can impact your Backend Application Performance, here is why“. Diese Wörter kommen am häufigsten vor: index, count, row, table, scan. Dieses Video enthält 156 Sätze und 1723 Wörter zum Nachsprechen. Der gesprochene Teil dauert 10:37. Der Sprecher spricht in natürlichem Tempo, etwa 162 Wörter pro Minute, ähnlich wie im Alltagsgespräch. 86 % der Wörter gehören zu den 3.000 häufigsten im Englischen; den Rest solltest du dir vor dem Üben ansehen.

Wichtiger Wortschatz in diesem Video

Die 15 anspruchsvollsten Wörter aus dem Video, mit Aussprache und Bedeutung:

WortAusspracheBedeutung
execute Verb/ˈɛksɪˌkjuːt/hinrichten, exekutieren
analyze Verb/ˈæn.əˌlaɪz/analysieren
query Substantiv/ˈkwɪɹ.i/Frage
cache Substantiv/kæʃ/Cache, Zwischenspeicher
visibility Substantiv/ˌvɪz.əˈbɪl.ɪ.ti/Sicht, Sichtbarkeit
proportional Adjektivproportional
vacuum Substantiv/ˈvæ.kjuːm/Vakuum
concurrency Substantiv/kəŋˈkʌɹənsi/Gleichzeitigkeit
millisecond Substantiv/ˈmɪlɪˌsɛkənd/Millisekunde
misconception Substantiv/ˌmɪs.kənˈsɛp.ʃən/Missverständnis, Irrglaube
planner Substantiv/ˈplænɚ/Organizer
scanner Substantiv/ˈskænə/Scanner, Abtaster
fetch Verb/fɛt͡ʃ/holen
aggregate Substantiv/ˈæɡ.ɹɪ.ɡət/Anhäufung, Ansammlung
spouse Substantiv/spaʊ̯s/Gatte, Gattin

Phrasal Verbs, die du hören wirst

WortBedeutung
come back Verbzurückkommen, zurückkehren
go back Verbzurückgehen, zurückfahren

Sätze zum Wiederholen

Kurze, vollständige Sätze aus dem Video, die du im Alltag wiederverwenden kannst:

  • Let's just jump into it.
  • Let's just add an explain analyze before these puppies.
  • Let's see what happened here.
  • Let's pay attention to what happened here.
  • How do you solve this problem?

Grammatik in diesem Video

Die Strukturen, die der Sprecher am häufigsten verwendet, mit den genauen Worten aus dem Video:

StrukturIm Video
Present Perfect have/has + Partizip Perfekt – eine vergangene Handlung, die jetzt noch wichtig isthave returned · has been reduced · have updated
Passiv be + Partizip Perfekt – wichtig ist, was geschieht, nicht wer es tuthas been reduced · have been updated · is actually deleted
Relativsätze who / which + Satz – eine Zusatzinformation über eine Person oder Sachetime, which is · index which is

Aussprache, auf die du achten solltest

Der Sprecher verwendet 40 Kurzformen und abgeschwächte Formen, zum Beispiel I'm, don't, you're. Sprich sie in der kurzen Form, so wie du sie hörst.

  • Die Laute „sh“ und „zh“: cache /kæʃ/, misconception /ˌmɪs.kənˈsɛp.ʃən/
  • Lange Wörter – auf die Betonung achten: visibility /ˌvɪz.əˈbɪl.ɪ.ti/, concurrency /kəŋˈkʌɹənsi/, millisecond /ˈmɪlɪˌsɛkənd/, misconception /ˌmɪs.kənˈsɛp.ʃən/

So übst du mit diesem Video

  1. Höre dir das ganze Video einmal an, ohne zu sprechen, und notiere die Wörter, die du nicht kennst.
  2. Beginne mit 0,75-facher Geschwindigkeit, sprich Satz für Satz nach und wechsle zur normalen Geschwindigkeit, sobald es leichtfällt.
  3. Nimm dich auf und vergleiche mit dem Original; achte dabei auf Wörter wie execute, analyze, query.

Was ist die Shadowing-Technik?

Shadowing ist eine wissenschaftlich fundierte Sprachlerntechnik, die ursprünglich für die professionelle Dolmetscherausbildung entwickelt und durch den Polyglotten Dr. Alexander Arguelles populär gemacht wurde. Die Methode ist einfach aber wirkungsvoll: Du hörst englisches Audio von Muttersprachlern und wiederholst es sofort laut — wie ein Schatten, der dem Sprecher mit nur 1–2 Sekunden Verzögerung folgt. Anders als passives Hören oder Grammatikübungen zwingt Shadowing dein Gehirn und deine Mundmuskulatur, gleichzeitig echte Sprachmuster zu verarbeiten und zu reproduzieren. Studien zeigen, dass es Aussprachegenauigkeit, Intonation, Rhythmus, verbundene Sprache, Hörverständnis und Sprechflüssigkeit signifikant verbessert — was es zu einer der effektivsten Methoden für die IELTS Speaking-Vorbereitung und reale englische Kommunikation macht.

Shadowing-Technik: die vollständige Schritt-für-Schritt-Anleitung lesen →