Mastering PyCharm Transcripts
Chapter: Performance and profiling
Lecture: Optimizing the database access

Login or purchase this course to watch this video and the rest of the course contents.
0:01 That's good, now let's focus on this get records thing.
0:04 You'll see there's this read data, there's this read row and so on,
0:08 let's go down here to this run query, and it turns out read row is slow
0:12 to create connection maybe, we could use connection pooling
0:15 that's a possibility, we could think about how that might work,
0:19 but let's go over here, we'd have to simulate this
0:23 in a real database app it would actually automatically do that
0:26 so let's go check this one out.
0:28 Now, let's suppose we've looked at this read row business
0:32 and you can see I'm just faking this for our little demo
0:34 we learned that there was actually no index on this database
0:40 on this particular query that we're running and there's a lot of data,
0:44 so the difference between having a large amount of data within index and not
0:50 can easily be a thousand times faster,
0:52 so let's suppose we go over to the database
0:54 and we're like, we're going to put an index and make it a hundred times faster,
0:58 so we improved it by adding an index,
1:02 so we're going to go run this again and see how we're doing,
1:09 I don't want regular to run, I want profile to run
1:15 I got our answers, let's zoom in again and see what we got going on
1:20 so now we look get records is actually still slow
1:23 but it's really about creating this connection
1:27 and this whole part about reading the rows,
1:30 like literally fell off the map
1:33 because it was under some sort of threshold,
1:35 let's go back and actually imagine
1:37 we could do some sort of connection pooling
1:41 again this is just fake, like I said,
1:43 real database connections, systems like SQLAlchemy would take care of this
1:46 but let's just go say this and let's imagine that we could say
1:59 if we have it, we're just going to return the connection
2:01 otherwise we have to create it, and we'll set the connection to that
2:05 and then we'll return the connection.
2:08 This is what connection,
2:11 at least some sort of shared connection would look like,
2:14 connection pooling will have a set we could get from
2:17 but let's see how this works, I think we probably got our get records
2:19 just completely flying now,
2:23 all right put that over here, have a look at the call graph
2:27 so down here we call get records nine times
2:29 we call get connection and it only takes that time that it took for one of them
2:33 and then that was it, it took a while to create the first connection
2:37 but no matter how many times we call it
2:40 now it's going to be open and it's going to be flying
2:42 so these things are really going pretty well
2:44 and this one is looking worse and worse compared to its friends,
2:47 it's red right, so let's go and work on this one
2:50 and then we'll see what else we can do.