Anvil: Web apps with nothing but Python Transcripts
Chapter: Databases: Storing and querying data
Lecture: Improving database performance

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So our app is working pretty well.
0:01 Let's actually clear this out
0:03 and click around a little bit.
0:05 When I go_compare, notice there's a little spinner
0:09 and Ajax busy type of thing, and I go_home
0:13 and it spins, and I go here, and it spins.
0:15 And I go there, and it spins.
0:18 What's happening? Well, every interaction we're going and getting
0:21 the user object from the Anvil server.
0:25 In this Home one, we're actually getting the user
0:27 and we're getting all the measurements, every time.
0:31 That seems a little silly.
0:33 Maybe we could get it once.
0:35 We had some location, some centralized place
0:37 where we could do that, that would be great.
0:39 We do, of course, that's why we created
0:41 this data_access thing.
0:42 So let's work on measurements first here.
0:45 Let's create a private variable here called __measurements
0:50 and we'll start it out being empty like that.
0:52 Say global, that thing, and here we'll say
0:56 if there are measurements, return __measurements.
0:59 Otherwise, the __measurements equal to this
1:01 you know, this is like a cursor thing into the database
1:04 so let's convert this to a local list
1:06 and then return Measurements.
1:09 Just doing that one simple thing should make our app
1:11 work a little bit better.
1:12 Let's do some clean up, and then run it.
1:16 Are we ready?
1:17 Notice that we had a little spinner, we go to compare
1:19 that's the user spinner, we go home.
1:23 That's quicker, quicker, there was more spinner action
1:26 going on before, but we're still getting
1:29 a little spinner here. So what's that for?
1:31 Well that's for accessing the user.
1:36 So let's have a function here called def the_user.
1:40 We're just going to use that whenever we want
1:43 to access the user.
1:52 You may be noticing a pattern.
2:03 Okay, so now we have this other function called the_user
2:06 and we just need to make sure that wherever we were
2:08 working with the user before, so in this part right there
2:13 let's just change this to data_access
2:17 which we have not imported in this place
2:23 the_user, something like that. Okay?
2:26 And let's just cruise around and see if there's
2:29 anywhere else we're doing this, maybe in navigation.
2:37 Over in require_accounts, actually we're going to need
2:39 to import data_access here, just to make sure that works.
2:46 Again, we put data_access user, and if it's there
2:50 we return it, otherwise we're going to log in.
2:52 All right, this is looking pretty good for the user there.
2:56 Let's see if we're doing anything about the user
2:59 in these other places.
3:04 Not there, but I think we are compare.
3:06 Okay, we're probably ready to go.
3:08 Let's do a quick clean up and re-run it.
3:10 See if this is working.
3:14 So it did the little spinny thing when it was starting up
3:17 and we got that output. That's looking good.
3:20 Let's go here. Wait a minute, that was faster.
3:24 That was a lot faster. Let's go to compare.
3:27 Let's go home.
3:28 Oh, so there's still something happening on home.
3:30 But notice, this is much, much quicker.
3:33 Let's go see what we're missing on the home bit here.
3:36 Yeah, I'm not sure we're missing anything.
3:38 I think it's pretty good.
3:40 There, maybe, maybe I'm missing it, but I don't see it.
3:43 Let's do one final thing.
3:44 Let's just do a little print statement
3:46 where we show that we're returning the cached measurements
3:49 for that, and maybe also, the same thing for the user.
3:53 Something like print using cached user
4:00 I think it's email, let's double check.
4:04 Email, perfect.
4:08 And when it first runs, we output our three measurements
4:10 but there's no comment on caching.
4:13 But as we work around now, as we click around
4:16 notice we're using the cached user.
4:18 We're not going back to the database.
4:20 We go home, we're using our cached measurements.
4:23 No more data access, and look how incredibly quick that is.
4:27 I mean maybe, not sure how well it comes across
4:29 in the video, but it feels nearly instant.
4:32 This one right here, a tiny bit of spinning
4:34 as it's loading up stuff, but it's much, much better
4:38 and that's on a really fast gigabit connection.
4:40 If we were on crummy 3G or Edge Cellular
4:44 you would really appreciate the fact
4:46 that this app goes super fast now. And that's it.
4:49 That's how we use our data access bit to cache things.
4:53 Now there's a few other things about, say
4:55 logging out and whatnot, we got to be a little bit
4:57 careful about, so, we're going to have a function
5:00 called logout, and when we do that, we want to just
5:03 clear out the measurements here.
5:06 So we'll say user equals none.
5:09 The other thing we want to be careful about
5:11 is once we add a measurement, how does this get updated?
5:15 Remember, this function was going to get more interesting?
5:19 So down here we're going to say, that's equal
5:22 to an empty list as well.
5:25 If we're going to change the measurements
5:27 we're going to clear the ones we've got
5:28 and so the next time someone asks for them
5:30 we go back to the database.
5:32 And let's just make sure that that actually works.
5:35 And everything should be good.
5:36 Oh, we're not calling the, one other thing.
5:39 We want to make sure we're call logout down here somewhere.
5:43 link_logout so let's say data_access.logout.
5:48 That should actually log out the user
5:50 and I think we'll be good this time.
5:52 Let's give it a shot.
5:54 So we're over here, let's try to logout.
5:56 Yeah, everything worked on that.
6:00 Log back in, that looks like that works
6:03 and yeah, we're actually getting the measurements
6:05 logged in again.
6:06 Now notice we're using the cached measurements.
6:08 Let's add one more, go back in time.
6:11 Add go, 181, say 73, 74, something like that.
6:17 Notice there's a little spin right there
6:18 as it took just a moment to add that measurement
6:21 and now how many do we have?
6:23 We have four. Right, 'cause when we went to the Home page
6:25 we had to go back and reload it
6:27 but again now, we're using our four cached measurements.
6:32 I think we have this data access and caching thing nailed.