Welcome to this lecture number 8 on permeability and its determination. So, in the previous lecture, we were discussing about the intrinsic permeability, which is essentially a function of flow medium only and it has the dimensions of square of length and its expression it is this is the intrinsic permeability is denoted by k 0 and it is linked with the permeability or hydraulic conductivity k by the formula k is equal to k 0 into g divided by nu, where nu is the kinematic viscosity and by this we also know that by Darcy's equation we have this hydraulic conductivity or permeability which can be expressed as velocity v of the ground water divided by the hydraulic gradient that is minus dh by dl. So, that will be the hydraulic conductivity or permeability and equating that to k 0 into g by nu. So, finally after simplifying we get an expression for this intrinsic permeability also known as specific permeability as this v the product of v the ground water velocity multiplied by nu the kinematic viscosity of ground water divided by the product of the acceleration due to gravity g and the hydraulic conductivity I am sorry the hydraulic gradient that is minus dh by dl. So, this is the expression for the intrinsic permeability in terms of the ground water flow velocity the ground water kinematic viscosity the acceleration due to gravity and the hydraulic gradient. So, this intrinsic permeability k 0 is measured in terms of as we have already discussed that it has the units of square of length it is measured in terms of that is micrometer square of micrometer that is square of 10 to the power 6 meter which is 10 to the power minus 12 meter square and in case of petroleum engineering. So, another unit known as Darcy is also under use and this Darcy is related to this micrometer square by this expression 1 Darcy is equal to 0.987 micrometer square. So, you can roughly say that 1 Darcy is almost equal to say 1 micrometer square. So, this is the intrinsic permeability which essentially is a function of flow medium only having a dimensions of square of the length. Now, let us come to the we have already seen in the previous class we have seen the the storativity or storage coefficient which is the volume of ground water that an aquifer can release or absorb per unit surface area per unit change in the head. And so this is denoted by the letter s and we have also seen the hydraulic conductivity which is given by the Darcy's expression as the ground water velocity is equal to the hydraulic conductivity k into the hydraulic gradient that is minus d h by d l. So, now these two storativity as well as permeability also known as hydraulic conductivity and storativity is also known as storage coefficient along with that there is a third parameter which forms what is known as the what are known as the three formation constants of the aquifer. And that is this transmissivity also known as transmissibility and it is defined as the ground water flow rate through a unit width of aquifer under a unit hydraulic gradient. So, here we are considering a unit width of aquifer and a unit the flow is taking place under a unit hydraulic gradient. So, we know that we have an expression based on the Darcy's law that the ground water flow rate q is equal to the ground water velocity which is given by k into i and the area of flow. So, therefore q is equal to k into i into a and by this definition of transmissibility or transmissibility. So, this q will be equal to t when this hydraulic gradient i is equal to 1 as well as the area of the ground water flow is equal to the unit width multiplied by the saturated thickness of the aquifer denoted by b. So, therefore when i is equal to 1 and a is equal to b the saturated aquifer thickness. So, then q is equal to t. So, therefore this transmissibility or transmissibility t is given by the product of the hydraulic conductivity k multiplied by the saturated thickness of aquifer that is b. So, in case of unconfined aquifer. So, this b will be varying because the unconfined aquifer is not confined at the top. So, therefore b will be varying as per the water table shape of the water table shape obviously the slope also. Whereas, in case of a confined aquifer. So, the saturated aquifer thickness is constant at a particular location and so therefore, so the there the transmissibility or transmissibility t is more constant as compared to that of an unconfined aquifer. So, the hydraulic conductivity k the transmissivity also known as permeability the transmissibility t also known as transmissibility and the storage coefficient that is s also known as storativity. So, these three form what are known as the formation constants of the aquifer and here this transmissibility t it has the expression given by k times b. k is the hydraulic conductivity b is the saturated aquifer thickness. So, therefore this transmissibility t its dimensions are l t to the power minus 1 into l. So, this is l square t to the power minus 1. So, this transmissibility is expressed in terms of meter square per second or meter square per day or it can be meter square per hour depending upon the time unit in which the ground water flow is described. Now let us come to the determination of hydraulic conductivity or permeability. So, this hydraulic conductivity or permeability can be determined by either field tests, laboratory tests and in the field tests we have pumping hole test that is pumping test of course, we also have say tracer test and we also have this auger hole test. So, these form what are known as the field test in which the hydraulic conductivity or permeability k is determined in the field and in the laboratory it can also be determined by what is known as the falling head permeability test or falling head permeability test also permeability test and the other one is the constant head permeability test or permeameter. So, the instrument which is used the experimental setup which is used for determining the hydraulic conductivity or permeability in the laboratory is known as permeameter and its specific purpose is the determination of the k or the hydraulic conductivity value. So, therefore, this is both is whether it is a constant head whether it is operating at constant head in that case it is known as constant head permeameter and whether it is operating at a falling head that means, when the head is falling continuously as the ground water flow takes place. So, in that case it is known as falling head permeameter. So, these two form the laboratory test whereas, the other is one that is the pumping test, tracer test, the auger hole test they form what are known as the field test. Now, let us come to firstly the constant head permeameter or say permeability test. So, here we have in the laboratory a setup in which the ground water is made to flow through a soil or a rock sample under constant head. So, let me draw the schematic diagram here. So, there will be a supply of water. So, this is the supply, this is the continuous supply. So, this is the overflow and here so this is the constant water level and here so the ground water or this water is made to flow through a setup in which this is the sample here soil or rock sample and here to retain this soil sample. So, there are two porous plates one at the top and one at the bottom of the soil sample and then there is this. So, there will be a collecting tank. So, this is a collector in which the water after flowing through this sample which is enclosed between two porous plates one at the top and one at the bottom. So, this sample so this has a so the cross sectional that is a sample cross sectional area is A. So, which is perpendicular to the flow direction in this case the flow is from bottom to top and this is known as the constant head H. So, this is the constant head H which is essentially the level difference between the constant level in the apparatus after the flow takes place through the soil or rock sample as well as the ground water so constant water level in the supply side. So, this is the sample cross sectional area A and in this case so collector say suppose let us say the collected volume in the collector is V. Let us show this by H V and this sample cross sectional area and then this sample length so this is L. So, this sample cross sectional area so this is a cross flow direction and this L is a long flow direction. So, in this case and the collected volume is V in say time t. So, in that case the hydraulic conductivity K is given by V the volume multiplied by so divided by time t. So, this will give the discharge divided by the area that will give the velocity and divided by the hydraulic conductivity so that is H by L. So, therefore, so the hydraulic conductivity K is given by the volume V multiplied by the sample length L divided by the cross sectional area A multiplied by the time of collection of volume that is t multiplied by the constant head H. So, this is the expression for hydraulic conductivity in constant head permeability test. So, simply this hydraulic conductivity is expressed in terms of say 4 linear parameters rather 2 linear parameters 1 area parameter 1 volume parameter and 1 time parameter. So, here this is the collected volume is the V the cross sectional area is the A and then the 2 linear parameters are the sample length along flow and the constant head between the supply the constant level supply tank and the level of ground constant ground water level after it has flown through the soil or rock sample and then t is the time of collection. So, this is the constant head permeometer or which is also known as the constant head permeability test.  So, obviously through due to gravity flow. So, this head in the supply side is continuously decreasing. So, the sample will the sample and that part is almost the same. So, there is the soil sample or soil or rock sample is enclosed between 2 porous plates. So, this is the soil or rock sample through which permeability variable head permeability test is conducted and. So, here this is the collector. So, this is the collector and here we have this water and the sample it is assuming this to have a circular cross section. Let us consider the diameter of the sample as r c and here. So, this is the supply side circular tube with diameter equal to r t or rather 2 r t. So, the diameter of the circular sample and 2 r t is the diameter of this pipe which supplies water for flow through the soil or rock sample and the initial head. So, this is let this be the head h 1 and this head is measured with respect to this one. So, this is h 1 is the initial head and then let us say this is h 2. So, this is the h 1 is at time is equal to 0 and h 2. So, this is the water level. So, this is at time is equal to t. So, here in this case this is let us say this is the h 1. So, the initially the head is at this level which is above the reference level which is the constant water level maintained after flow through the soil or rock sample. So, this is the datum with which the heads are measured. So, that is h 1 and then as the water flows through the sample. So, this head decreases and at an arbitrary time t. So, this head decreases from h 1 to h 2 h 1 is at time is equal to 0 h 2 is at time is equal to t. So, now and the collectors with the collected volume. So, now we can say here that the ground water flow rate or the flow rate through the soil or rock sample is given by the cross sectional area. It is pi into RT square multiplied by dh by dt which is the rate of change of head with time. So, that is represents the velocity in the vertical direction and this q is also equal to the pi into Rc square where Rc is the radius of the circular sample through which the water flows circular soil or rock sample multiplied by the hydraulic conductivity k into h by L and this L is the sample length along flow. So, now if we equate these two expressions and integrate. So, here this is pi into RT square dh by dt is equal to pi into Rc square into k into h by L. So, let us simplify this one. So, therefore, here we can write this as a dh by h with limits say from initially limit of h 1 to the final limit of h 2 is equal to. So, dh by h and of course, if you want you can keep this Rc square by RT square which is a constant you can take it outside into k by L. So, all this will be outside the integral sign and here. So, there will be definite integral when h is equal to h 1 time is equal to 0 when h is equal to h 2 the time is equal to t and here. So, this is dt. So, after simplifying this after integrating and then simplifying we get an expression for the hydraulic conductivity through this variable head permeability test as RT square by Rc square into L by t L is a sample length 3 is a time of collection into natural log of h 1 by h 2 where h 1 is the initial head h 2 is the final head after the flow which results in the continuous decreasing of the head through this variable head permeability. So, this is the expression for k in variable head permeability test or variable head permeameter. So, while in the constant head permeability test. So, the expression for the hydraulic conductivity is simply given by the product of the collected volume multiplied by the sample length divided by the product of the sample cross sectional area and the time of collection and the constant head. Whereas, in this case in case of variable head permeability test. So, this hydraulic conductivity k is given by RT square by Rc square of course you can also write this k as the area of the tube divided by the area of the column into L by t. So, this is the A t is the tube area and A c is the sample column area into L by t into natural log of h 1 by h 2. So, therefore, so this is the expression for the hydraulic conductivity and if both the tube as well as sample which are in circular shape generally that is the case. In that case simply this k is equal to RT square by Rc square into L by t into natural log of h 1 by h 2 where h 1 is the initial head at time is equal to 0 and the h 2 is the final head after the flow of water through the soil or rock sample at time is equal to t. So, these are the two laboratory test which are used to determine the hydraulic conductivity or permeability. And in this case the laboratory test many times they do not yield they do not give the proper results because in most of these cases the sample through of soil or rock through which the hydraulic conductivity laboratory test is conducted it will be a disturbed sample. So, therefore, they may not give the actual value. So, that case we go for the field test firstly let us consider the field permeability determination test or permeability test and in this firstly let us say the tracer test. So, in this tracer test what is done is suppose there is a ground water or soil or rock sample. So, here two holes are drilled one at the upstream and another one at the downstream. So, this is the upstream hole which is A and then this is the downstream hole which is B and so in the upstream hole. So, this is tracer is added. So, this is sampling for tracer is done at the downstream hole and here in this case obviously there will be hydraulic gradient. So, this is the and the level difference between the water level in the upstream and downstream is H and say suppose this. So, this is the flow direction and this is the length between these two holes L. So, in that case the velocity of flow is given by the flow velocity the ground water flow velocity ground water it is the interstitial flow velocity. So, this is V A. So, this is given by the hydraulic conductivity K divided by the porosity N multiplied by H by L which represents the hydraulic gradient and this is also given by L by T where. So, this is the T is the travel time N is the porosity and H is the difference in the water level between the upstream hole and the downstream hole upstream hole where in the tracer is added and downstream hole where the sampling of the tracer is done. That means the time measurement as well as its concentration and. So, here by equating these two expressions. So, from this expression we get this K is equal to H I am sorry L square N into L square divided by H into T that is the hydraulic conductivity K is given by the porosity of the soil or rock sample N multiplied by the square of the distance between the two holes the upstream the one at the upstream and downstream. So, let me show the central line here for these two. So, this is obviously L is the center to center distance and H is the level difference of ground water in the upstream hole A and the downstream hole B and T is the time of travel. So, sample is added here. So, the initially and then the stopwatch is started and as soon as the sample reaches this downstream hole B this stopwatch is closed. So, that it will give the travel time T. So, therefore, so this hydraulic conductivity is given by the porosity multiplied by the square of the length divided by the H which is the difference in the water level between upstream hole and downstream hole and T the travel time. So, this is the first of the field permeability test for determining the permeability or hydraulic conductivity. Of course, there is also another technique known as the point dilution method and let us not go into that and so there is also now let us come to another field test which is known as the auger hole test. Here what is done is an auger hole is drilled in the ground consisting of soil or rock where we need the permeability or the hydraulic conductivity value and so this is Lw which is the length of the well or the auger and this auger hole or well it has a diameter of 2 into Rw and in this case so let this be the so this is the impermeable or highly permeable layer and with respect to this so this is the water table denoted by Wt and the draw down in this auger hole let this be y the level difference between the water table and the water level in the auger hole and the height of this water table above this datum of impermeable or highly permeable layer let this be h. So, here so it can be shown that the hydraulic conductivity K is given by that is C a coefficient so this is a dimensionless constant in C divided by 864 and this K so this is it directly gives the hydraulic conductivity value in meters per day and 864 comes from the this is 24 into 3600 which is the number of seconds in a day that is 86400 into dy by dt which is the the rate of change of the y with time so this dy by dt so this is measured in centimeter per second measured in centimeter per second so initially this auger hole is drilled and then so some water is removed that is water is pumped out from this auger hole so therefore there will be a draw down of y created and then so because the water table is above the so there is a draw down so what happens is so this y goes on decreasing so therefore this dy by dt represents the rise of the rate of change of this draw down with time so this K is directly given by this expression C is a constant so this is the dimensionless constant so this is the expression for the hydraulic conductivity through this auger hole test now let us come to the last anyway of course we will go into this that is the pumping test and in this pumping test so through a well in the field so the time versus draw down that means the time and is continuous measurement is done and then this consists of continuous measurement of time and draw down in a well subjected to pumping and by using the appropriate expressions so the hydraulic conductivity K value is determined so these are some of the methods for the determination of the hydraulic conductivity initially we discussed about the laboratory test wherein we started with the constant head permeability permeameter or constant head permeability test and we also discussed the variable head permeability test or variable head permeameter then we moved on to the field test which will give a better result for simple reason that the sample is undisturbed in case of field test as compared to the laboratory sample of soil or rock which is generally disturbed so which consisted of there are three types of that one is the tracer test the auger hole test and lastly the pumping test and of course the actual pumping the one we will see the subsequent chapters. Now let us come to the heterogeneity and anisotropy so here in all these we made an assumption that the soil or rock layer through which the groundwater flows so is a homogeneous and isotropic that means it is the hydraulic conductivity is same in all directions in all the three directions that is Kx, Ky, Kz whereas in actuality it is not the case now let us consider the some of the anisotropic aquifers wherein this let us consider say separate layers wherein the let us say this is the wherein the flow is taking place firstly let us consider that is the groundwater flow along the layers or strata and in this case let us consider so this is the so these are the three layers the first layer let us consider the permeability value as K1 and let us consider the width as the thickness of this aquifer as B1 and let us consider the second as a K2 the second layer having hydraulic conductivity K2 and a thickness B2 and then let us also consider let us consider an aquifer having a permeability Ki having a thickness of Bi and let this be the last the lowermost aquifer having a thick nth aquifer having a permeability or hydraulic conductivity Kn and thickness Bn and in all these let the flow be along the layers or along the strata so in this case so this is case of heterogeneous aquifer of course here in this case we are not considering the anisotropy and we are considering only flow in one direction so in this case the equivalent permeability that is Ke so this is expressed as summation from 1 to n Ki into Bi divided by summation Bi from 1 to n so and this is the expression for the equivalent this permeability and the expression for transmissibility or transmissivity is simply given by Ke into summation Bi that is 1 to n which is simply summation 1 to n that is Ki into Bi so this is the expression for transmissivity and this is the expression for the equivalent permeability so the ground water flow is along the layers or strata sometimes it may so happen that in a heterogeneous aquifer the ground water flow is taking place across the stratum or layers so this is the ground water flow across the heterogeneous strata so here let us consider say this is the impervious strata and this is the other impervious strata may be top or bottom you can say and then suppose this is the ground water flow and let us consider the strata the flow are perpendicular to the flow direction each having length of say L1 and hydraulic conductivity K1, L2 hydraulic conductivity K2 and so on say suppose and here let us say this is Li the hydraulic conductivity Ki and lastly let us say this is Ln with hydraulic conductivity Kn so in this case the ground water flow is perpendicular or across the heterogeneous layers so in this case so the total flow length is given by this L is equal to summation Li 1 to n and the equivalent permeability that is Ke is given by summation Li 1 to n divided by summation 1 to n of Li by Ki so basically this is the length and Li by Ki represents the travel time so this is the expression for the equivalent permeability in case of a heterogeneous aquifer where in the ground water flow is across the heterogeneous flow stratum so we will stop here and we will continue in the next class in the next lecture on the and move on to this further articles in this. Thank you.


Mlamli Mankala Discharged From Hospital After COVID-19 Battle After spending several weeks in hospital, including four or five days on oxygen in an intensive care unit, former The Queen actor Mlamli Mankala is back home and in high spirits. The star was diagnosed with COVID-19 last month after struggling to breathe and when he was unable to walk. He underwent treatment and was told he had developed bilateral pneumonia, in addition to COVID-19. He said he had lost more than 27 kilograms because of the illness. The star has since been on the road to recovery, sharing his journey with fans on Facebook, and he returned home this past weekend.  The star has been holding discussions with several experts to raise awareness about the coronavirus, and opened up about his own treatment. He said the best thing people could do is boost their immune system until they show symptoms that could be treated. The star was flooded with messages of support and prayer after he posted a video of himself on oxygen last month. He pleaded with fans to take the virus seriously, and said at one point he feared the worst. I thought I am gone, this will be it. I am not going to get out of this one.


Carnegie Mellon University's Advanced Database Systems course is filmed in front of a live studio audience. Actually, I have one of the chips in my office for some reason. They took me on a boat on Hawaii, and they're like, here's a burnt-out chip from Spark that has, you know, oracle compression built inside of it. I'm like, okay, I have it, I don't know what I'd do with it. All right, anyway, sorry, enough, we can go on forever. Databases, query optimization. All right, so last class, we didn't get through everything. We'll go over this again. Again, I'll go a bit slower than maybe I did last time. Just walk through what Cascade is doing, and then we'll finish up with randomized search before we jump into what today's paper is about. But again, the things we discussed last class was going through this progression of how to do more sophisticated things with the query optimizers. The query optimizers are starting from just if-and-else rules, looking for patterns that then do some kind of rewrites, which everyone uses when they first start out. And then we saw how System R in the 1970s introduced this cost-based joint search, and that's sort of the backbone of how the stratified search and unified search came along. And then randomized search will be just the variation of this. So the distinction we were talking about last time was, again, the stratified search and the unified search. And as I was saying, the lines were kind of blurry. We're like, OK, Cascades in SQL Server is technically a unified search, but the way they invoke some of the transformation rules, as we'll see in a second, it's basically doing it without a cost-based search. They're doing transformations that you always want to do first, and then they do something that is more exploratory search that you would see in Cascades. And so the thing that maybe matters the most is that we want to be able to define the transformation rules, ideally in the same dialect or DSL, whatever you want to call it, that allow us to then be able to decide, do I want to move stuff to be always in this front phase that I always want to run or the second phase where I do more exploration stuff? So the stratified search is having two separate phases. One with just heuristics, sorry, one using the rules, but without a cost model, the second one is doing cost-based search. And then the unified search is the idea is that you just do everything all at once to avoid a bunch of expensive transformations or getting stuck in infinite loops. We saw again how the memo table is going to help with that. So less so about maybe the stratified search for the unified search, the thing that really matters when we understand is the distinction between top-down versus bottom-up. So the Cascades approach is top-down, meaning I start with the outcome that I want. This is the final output I want for my query. I don't know how I got there in my query plan, but I'm going to go down the tree and assemble the pieces, assemble the operators I need to then feed me into my final output. Whereas in the bottom optimization, which is what the system R guys started with, is that you start with nothing and you then add the operators you need to get you up to the goal that you want. And again, me standing up here and making hand gestures like this and this maybe doesn't really sink in and mean anything, but there are certain optimizations you can apply more easily in one versus another. At a high level they are composable, they're commutative, they could use one versus another, but there are some optimizations you can do in top-down one that maybe you can't do in bottom-up and likewise in bottom-up you can't do in top-down. Yes? Is stratified always bottom-up? No, his question is stratified always bottom-up.  And in that call-space search it's doing top-down, but the transformation rules are sort of operating them in the same way. They're generating these logical plans in the top-down manner too. But that would be technically that's a stratified search because you have something you always do and then you have a call-space thing that you can be dynamic about. Right. So most of the open-source systems are going to be bottoms-up. Even the guy that invented Cascades said the way you probably want to do create optimization now is start with Cascades in a sort of stratified manner, like run some initial rules. Maybe not necessarily in the full call-space search. It wasn't clear. It's this sort of off-hand comedy made at a conference. And then you want to do the bottom-up optimization to pick join ordering. All right. So again, today's class I just want to spend the first half going over again what we missed and rushed through at the last class of the unified search in Cascades. And then we'll see how we then apply that randomized search. And then I want to go quickly over some real-world examples of what some close-source and open-source query optimizers actually look like. The main is it's going to be SQL Server, CalCyc, Orca, and CalCycDB. And then we'll finish up with going over at a high level the unnesting sub-query paper you guys are starting to reading. Hopefully, that wasn't too dense on the relation algebra, but it's one of the things where I think everyone should know it. We probably should teach it in the intro class. Very few systems actually implement it entirely. As far as I know, only DuckDB, Umbra, and Hyper. My former student who took 701, she's at Databricks. She implemented almost all of it in Databricks, but not everything. But we'll walk through an example there. Unfortunately, let's take a time. We're not going to be able to cover how Hyper does their dynamic program approach or DP approach for finding joiners, but we can cover that offline if you want separately. All right, so again, this is just a repeat of what we talked about in the last class, where the Cascades optimizer is going to be the third version or third generation of a query optimizer this guy, GertzGraphy, built. Exodus came first, then Volcano. And it's the same Volcano when we describe the Volcano model, but in that system or in that paper, he also describes how to do parallel queries with the exchange operator. And then he has this optimizer generator that he built with Volcano. And then after that, he then built Cascades. But as far as I know, he just wrote the paper on Cascades. It's very heavy-handed on object-oriented programming because that was the hot thing in the late 80s, early 90s. And then as far as I know, nobody, he didn't actually implement it except for being involved in this master's thesis a few years later at Portland State. But then Microsoft hired him to go build their new query optimizer because they were rewriting everything from their fork of Sybase. And that's why, again, Microsoft famously is using Cascades. So it's going to be a top-down approach using a branch bound search. And the key idea of what makes Cascades interesting and better than what came before was that they were doing the incremental materialization of the possible ways to represent some expression, which I'll say it in a second, some operator in the query plan. Whereas in Volcano, when you search down, you landed some node in the tree, you immediately materialized everything which would explode your search base. Even though you may have run out of time to examine everything at that operator, at that level of the tree, you still materialize everything. So your memory cost of Volcano would explode. So the four key ideas of Cascades, as we talked about last class, again, everything is going to be represented as data structures. So within a task, we'd have here's the pattern I'm looking for in my query plan, here's the transformation I want to apply, and you can have additional things like a priority that says this thing should be considered more quickly or sooner than another one. And in the paper, you can actually modify these priorities on the fly, but in practice, again, I don't, Cochrane-Schiebe does it, Microsoft does not. Then you have explicit definition of these, what properties we need our operators to have to ensure that if data needs to be sorted a certain way or data needs to come in a certain compressed form, we can make sure that anything, any operator we would generate in a query plan at some level in the tree meets the expectations of, sorry, anything below us in the tree meets the expectation of the parent we're feeding into. And then we talked about this, you can reorder things on the fly to find the best plan more quickly based on what you know the query plan has done so far. And then this last one, again, is important that within the same search engine and the rule definitions, we can do pattern matching and transformations of expressions and where clauses, having clauses or joining clauses, whatever in the same way that we would do a search for converting logical operators to physical operators. And this is nice, again, you don't have to do like one pass for the where clause to optimize that and then a separate pass for the query plans themselves, all that's done in a single engine. Like I think MySQL, they treat the expressions and the where clauses separately from the query operators. So they have sort of two optimization passes. All right, so the definitions we care about in Cascades is this notion of an expression and it's just going to be some operation that we want to do in our query plan that is going to have zero or more inputs coming into us. So it could be a leaf in the query plan or it could be some middle operator, right? And we can sort of group them together and say here's a higher level thing that we want to do and we'll represent that as an expression. So for example, if I want to join A, B and C on simple IDs, I can have a logical expression that says I'm going to join A with B, then join that with C. And then if I flip the order, then that's considered another expression as well. And then I can have a physical manifestation of that expression by actually specifying here's the scan method I'm going to use or access method I'm going to use for that given table and here's the join algorithm I want to use. And then now I'm going to combine or group these together the equivalent expressions based on what we know as the relation algebra rules into what is called a group. And the idea is here is that within one container or construct or entry in our system, in our query optimizer, we would say here's the output that I expect, A join B join C, then here's all the equivalent logical expressions, you know, permutations of the join order and then here's all the equivalent physical expressions. So within one entry in our system we can say here's everything that, here's all the different possible expressions to produce the output we would need for a single group. So the entire thing itself is the group and then these are all just the equivalent expressions. And then also embed additional information about like here's the properties that I need to have coming into the data that I want here. So then we can combine expressions even further into what's called a multi-expression and this is just basically a placeholder that says there's some expression below me in the query plan and at this point, at this group in my search tree, I don't actually know what it is, right? But I'm just going to keep track of like I know something below me, can tell me how this thing would actually be executed. So for example, if I need to join A, B and C, I could have a multi-expression that says I'm going to join A and B, I don't know how and I don't know in what order but then I'm going to join whatever the output of that is with C and then you have the various combinations of the join orders and so forth and likewise the various combinations for the what algorithm we're going to use to join it, right? So if it's a bottoms up approach, you're basically starting with like the smallest atomic multi-expression you could have, like go read A, right? And then you go add the pieces or the components to that going up towards the optimizer, towards the final output. In this case there, because we're at the top going top down, we start with this multi-expression says I want some output of A join B join C and then I'm going down and then filling in the specifications of the exact details of how to do that. Again, this is how they're going to get away with not having to materialize everything all at once when you land say in a group as you search down the tree because now you say I have a placeholder for A and B, someone below me is going to tell me how to handle that but right now I can just reason about whatever expressions I'm looking at within my group. Directly. And then likewise you can make further decisions based on those priorities as we talked about, like if I'm trying to consider should I go look at what happens when I join A and B? Should I go look at what happens when I scan C? You can decide dynamically as you're going down the tree which of these paths you want to look at first.  And in the end the database system needs physical operators because that tells you what the system is actually going to do when it exits things. And then within a rule we'd say here's the pattern that I want to match on, either in a logical expression or a physical expression and then here's the transformation I want to do to put the query plan that I'm looking at or the group that I'm looking at into a new form. So this is the example we saw before. My pattern is to identify when I have two joins across three groups and then I would have a logical plan that looks like this and note here again I have the multi-expression that says I'm joining A and B but then down below me now I'm just saying join A with B and then below that I can specify how I'm accessing the data. So I'm going to have a transformation rule that says just rotate it left to right or I could have an implementation rule that says convert all the aqua joins that I have in here into a search merge join or hash join or an asset loop join and so forth, right? And as we said before to avoid having to get stuck in infinite loops because I could go left to right and right to left and just doing this transformation and over again we're going to use the memory table to keep track of do I know something about what the outcome of this transformation would be and therefore I don't need to apply it because I've already seen it. So instead of doing that on every individual rule for every single possible state of the query plan my memo table is just going to keep track of like okay I know that for this multi-expression I have a cost for it and I don't need to go look at what happens when I make that transformation if that transformation's cost is going to be, sorry, the cost of the query plan after that transformation is worse than the best I've seen that I know I don't need to go look at it. So as you said the memo table again the different ways to implement this I think Microsoft puts this as actually inside the groups themselves I think CockroachDB maintains this as a separate hash table but it's basically again some table that's going to keep track of for a given multi-expression here's the best physical operator I've seen for it and here's this lowest cost. You also keep track of like the properties that I care about whether the data's coming up with this operator for a given multi-expression with again what physical properties and I can know that if I'm looking at something differently even though like I'm looking at a different multi if I'm looking at a different operator like in the group if it has a different physical property I can then maybe still evaluate and go further and you would do this things like if the best cost I've seen so far is a sequential scan but I know that for the given group that I'm at I really want data to be sorted now because that'll change my cost expectations then if the multi-group below me does not enforce that property or provide that property I can still go down and look for other things. So the basic idea how this is going to work not to get too theoretical is this idea of the principle of optimality and all it says is that if we have a what is the true optimal plan then any sub-plan of that optimal plan is going to be optimal. It's sort of tautology, sort of like self-defining like if I have the optimal plan then any portion of that query plan is going to be optimal because if it wasn't optimal then that wouldn't be the optimal plan. So because of that this is how we're going to be able to do branch and bound search to identify that if I'm at some level in my search tree if the cost to traverse down to my query plan as they go below is now worse than the best plan I've ever seen so far then I know I don't need to go down and look further because there's no magic way that that cost is now going to become less because as you go down the level you're adding more physical operators you're accumulating more costs, estimates and that's just a summation so you're not magically going to get faster so we can just cut things off and avoid having to search further. So this is the example that we have before again we want to join A, B and C so at the very beginning we just start at the root and we apply logical transformations to generate different logical multi-expressions and then rather than materializing a bunch of all the logical multi-expressions what is jumped down to the first one we have and see what the cost is. So now we come down here do the same thing we have the multi-expression AB we do a logical transformation to convert it into a multi-expression on A joined by a multi-expression on B but now we need to get the cost of these inner multi-expressions so we go down here and this is doing the access on A there's nothing other than just get A there's no other logical multi-expression so then we'll materialize the different physical multi-expressions so either sequential scan on A or index scan on A for brevity I'm not showing you what index I'm actually using but you can imagine for all my possible choices of indexes you would have additional you would have multiple or for each index I could use an index scan on a table I would have entry for each of those and then again you can do ranking in the priority list to say well only consider the indexes that you know rank them rank the order in which I evaluate the indexes in my multi-expression list here based on the selectivity of them or which ones cover the most columns that I need in my query there's a bunch of different rules you can do to figure out if I'm just blindly looking at all possible indexes alright so now that we have these two different physical multi-expressions we do some cost model look up and say okay well the sequential scan is going to be the fastest it has the cost of 10 so we add the multi-expression on A into our memo table and with the best physical expression we do the cost of 10 then we go back up the tree do the same thing down on B same thing we convert that to physical multi-expressions we end up with the sequential scan and next scan on B sequential scan is still faster we get a cost of 20 and then now we bounce back up to our multi-expression on AB we do further transformations to now flip the order of the joins now B is joining A but now we need again to do the same thing and get what is the cost of the different tables well when we go down to these lower nodes here we will see that we've already done this costing for us because it's in our memo table so we actually don't even do the due traversal at this point here we can just go look up the memo table and say what is the best cost for scanning B and scanning A and then we just reuse that so now again say it's only A join B and B join A so now we generate all the physical multi-expressions and then again using our cost model we then figure out that the hash joins the fastest so the cost of the hash join plus the cost of the scans on A and B produces the cost of 80 and that goes in our memo table and then we bounce back up now go on the other side of C and do the same evaluation yada yada and so forth until we then maybe exhaust the search for the different join orders we have for A, B, and C and we end up with the lowest cost of 125 so this is clear so this is what Cascades is doing again you're starting with in this case here I'm going to join A, B, and C not specified in the order not specified how I'm accessing the tables not specified what the join algorithm I'm going to use and I traverse down and use the memo table to keep track of the best cost I have yes so this is entirely cost based but you said there were a few rules it tries to apply every single time so would you first build this table and then or would it do it somehow before so like the like what I showed here like okay I have get A and then I take the logical multi-expression and convert it to physical multi-expression the rule could be if I land here I know I'm accessing a logical expression on a scan or accessing a table so let me always run the transformation rule or implementation rule without different physical multi-expression so the rule would simply say always pick sequential scan for A no no no what I'm saying is that you always generate the multi-expression that a sequential scan and all possible index scans on A that always gets fired then you do a cost based search to say or selection which one of these is the best now whether or not you materialize all possible physical multi-expression depends on the complexity of whatever it is that you're doing because that might balloon up the huge like up you know the join or up above I'm going to say like I think I should look at the join orders first yes what's the difference between this and Volcano Volcano is all still what's the difference between this and Volcano the the difference is that when you land in say at the very very beginning so back up here I generate all the logical multi-expression all the physical multi-expression and again it seems kind of obvious that you wouldn't want to do that but I don't know it was the 80s 90s right they Volcano didn't have priorities Volcano also didn't have I don't think you could evaluate the where cause expressions in the same method as well right question this you mean like this right here right what's the question yeah so right here it's saying I want to join A together but here you're not defining how to do it you then go look up the memo table and say what's the best way for me to to have done that yeah why do we say we're applying the rule before if we're doing it while we're doing all the cost estimation this is still entirely unified there isn't a set of things that you're doing before you're doing this entire thing altogether where you're applying these transformations also estimating the cost picking the best one so yeah so his question is like it's not two-stage yeah why how can I say that you could use Cascades to do a two-stage approach so like think of like the when you first show up you convert the AST from the part SQL query into some you know you have like a group like this I know I know I'm going to access A so you start populating those things right and without I don't know how Microsoft actually does it whether they can inject themselves at any point in the tree and start doing transformations or they always start the top and go down but there's like you could say I'm going to traverse the tree and only apply these rules to put me into a form that when I do a cost-based search I'm kind of pushed in the direction where I know it's going to be a better plan and the second one which is you said in volcano it won't do everything exhausted but in a diagram you made like I put dot dot dot right like whether it's powerpoint like whatever it looks at all of them it is exhaustive though no so again we talked about the termination rules it could be a timer, it could be like how many transformations I've done it could be like I haven't seen anything better volcano did not have that timer whether or not you materialize all possible things ahead of time before you even look at them volcano did not do that there's other object-oriented programming stuff which we don't care about I was going to ask what data is on limits to make these estimations with like for example like page information like the aggregate information for a page the laws you know if you're going to skip it whether or not maximum stuff would be like scans over stuff like for the cost that's next week right that comes later but like you would in a traditional database system meaning like you have statistics you've collected by running analyze or whatever like you would you would the cost model would tell you what the selectivity of you know a low level operator like this would be like how much data I think I'm going to read if it's just a straight sweatshirt scan versus like an index scan this is all data that you have to have collected you're going to touch the data when you're creating like this what do you mean touch the data there's like a difference between like running there's only certain things you can know after you run the query obviously yes but like this analytics comes from either previous runs or estimations correct statement like these costs as I'm describing here are done using summarizations or sketches or whatever you will call it of the data that you either derive from previous runs or your own analysis I would also throw a third category and you'll see on Monday next week is sometimes there's just a sample of data that's local and you could run like you know run the where calls on that sample data just to see what the estimate is going to be right SQL server does that but like all that is it abstracted away from this part is through the cost model yes why might one want to prefer to this question why do you want to do bottom up versus top down yeah so good question so there's some weird things like you may not be able to do the do all the branch bounding stuff that we want to do because I got to get to the bottom first meaning like I can't say okay well I am at this point here and I know that maybe I'm farther up in the tree like think of like doing a lot of joins like so I'm up here and I have I can't do any pruning of the search tree below me until something gets to the bottom and produces what I think is the best possible plan so you always have to generate the full plan then you can start pruning other things and sometimes like you can't you do have to do the materialization of the logical to physical because I don't know I can't do cost estimates I can't get the the can't get the lower bound if it's just a logical expression I got to convert it to the physical operator to know what the true cost is going to be there's some tricks you can play around like like doing estimates of like you know worst-case scenario things but it's in the if you're going bottom to the top like along each step I have a physical operator I can cost things right there starting from the bottom in some way gives you more options and it also allows you to materialize physical as you go materialize what sorry materialize physical operators as you go instead of converting them there's other optimizations you can do which we don't have time to cover from like the hyper guys of like grouping things together on hyper graphs and like just costing those things separately from other pieces where this just looks at everything all at once yeah I can it's like 2006 paper it's in the reading list I might mention I don't have time to cover it but like they show that like they in practice they find the they find the optimal drawing algorithm faster than drawing ordering faster than cascades you know Microsoft has put a ton of effort and money into it yes yeah so the question is how does like the data properties come to play in any of this would be like say that say that I've picked here I'm going to merge join say not so not so I assume that my inputs have to be sorted so as I traverse down then I can say don't give me anything that that's not sorted right and then I can record that actually in the in my memo table to say okay for the scan on B here's the lowest cost with with with these set of properties and here's the lowest cost so there's other set of properties so then I can decide like if I need those properties I can check the memo tiplessly whether that cost whether that operator providing input yeah memo table I'm not showing here because it's PowerPoint but like you would keep track of like what properties you know a given multi-expression and a any the best expression for it provides and then you can decide whether that's going to give you what you need or not sorting is the most common one you could say like compression I'm not showing like we're not showing projections obviously here but like I need these columns stuff like that and then that can change based on what it's like a column store or a row store because row store could always shove it up at a column store if it's like disaggregated across different nodes it becomes more complicated would you have to store every single set of like subset of properties for every single combination of tables? The question is would you have to sort of so if you just think you're like sorting than this obviously wouldn't do it that's one or zero but you have like five no no no it would be sorting me like sort of on what column oh yeah like that as well yeah so you have to keep track of like I'm accessing this data and it's coming into me it's going to sort of in this column or I can then do a transformation to say all right well I'm looking at you know a merge join here and then have a transformation where we can then say okay populate the thing below me to make sure I'm looking at things being sorted or not or you can pass hints down and say okay at this point here maybe I don't want things sorted so don't consider it So is this memo table generated let's say maybe combinations of tables that we may want to exclude from the memo table at the very beginning? The question is are there combinations of tables we may want to exclude from the memo table at the beginning? Like for example like if I want to sort on like if I have a table with like five columns I only want to sort on one column Yeah so Microsoft does this Microsoft will pre-populate the memo table in this first stage and then sort of seed it with things that it knows it should probably consider first and then that way when you do this search you may prefer things that I already know about Yeah Yes Can I say that we all we need to do bottom up we want to do adaptivity I know we're not there for the next week's topic but like if you want to adaptivity you definitely need to do bottom up This question is if you want to do adaptivity you have to do bottoms up adaptivity when? Like while the query is running? Yeah Why? Why would you have to do it Why would you have to go bottoms up? Because if your bottom is up you start with like a sequential scan and that's your adaptivity In that case if you're doing the bottom up wouldn't it be slightly more easy to control what's happening as opposed to having to go explore all the way from the top His statement is if you want to be adaptive it wouldn't be easier to do bottoms up versus tops down Is your point like that at some point at some point we say oh like maybe Let's punt this next week to see how they do it One of the approaches we'll see to do adaptive query execution is you just inject a new physical operator See that's bad We probably won't do that Why is that bad? Because if you're injecting if you're continuously adding more costs If you're injecting another operator in the middle of the day you're adding costs to what goes above to the total The physical operator is like a I don't want to use the word sentinel it's basically a gate that checks is the data coming out what I expected it to look like and then if not you then make a decision It's like a trigger in the query plan It has no substantial cost to the query plan because it literally is like is my selectivity what I think it's going to be Is that really adaptive? Yeah that's next class That's Monday He's jumping ahead It depends on the implementation You could Whether or not you inject the activity pieces why you're doing this generating the query plan or you just say I got my physical plan you embed now in the operators your cost estimates and then you say well here's my leaf node scan I'll now inject that trigger check right above it and if I see that the data is coming out doesn't match what was in my scan below me then I'll do something else You can imagine a scenario you could include in your cost estimates to say this is going to be more expensive to do but I'll add this trigger check to make sure that I don't this is going to be really expensive to do if I if I get it wrong so I'm going to add my trigger check to make sure that I don't get it wrong and then choose the thing that's more expensive so you can play games like that let's punt on that until next week most of the systems that do adaptivity will be simple push downs of certain operations so Snowflake will push down an aggregation to a remote node above a join if it sees certain things other triggers will be like this query plan is just terrible stop throw everything away and go back to the query optimizer and then the other ones can be more clever about switching plans on the fly but nobody does that in practice as far as I know they'll do simple movements now okay I have a question so there's a bunch of implementations of Cascades Cascades is the name of the paper and as far as I know there was no implementation of it like Volcano as far as I know there was a system and code based called Volcano that we were using the actual implementations that were out there in the early 90s was this thing called Op++ out of Wisconsin I think Gignesh's office mate worked on this when he was Wisconsin because he did his PhD there and then Portland State was again with the Columbia system again that was Gertz-Graffy was involved in that Gertz-Graffy's PhD advisor was also Gignesh's PhD advisor like this guy Dave DeWitt who vented a lot early parallel Shibadeva's stuff Green Plum built this thing called Orca which we'll cover in a second that's sort of like CalCite where it's like a standalone optimized as a service similar to what we're trying to do for OpD and then here's a bunch of the other ones that are implemented in actual full systems so let's come back to Cascades other implementations actually hold on let me do more Cascades before we go to randomized algorithms so SQL Server the Cascades paper is like 94 Microsoft hires Gertz-Graffy to start building this in SQL Server in like 95 the the there is a single code base there was a single code base for the Cascades optimizer but my understanding from talking about Microsoft is that they forked it many many times and basically all the the major database products that Microsoft sells on-prem and in the cloud like what are Synapse we'll cover later Cosmos DB they're all using scopes in the line they're all using some derivation of the of the original Cascades optimizer everything is written in C++ there's no DSL and then to do transformations of the where clause expressions and else clauses and not rules running in the search engine so as we said before what their Cascades is going to do is that they're going to have separate stages that are going to find here's the rules I actually want to consider at each stage and the idea is that you start the ones that you know you always want to run predicate push down you always want to run that 1 equals 2 converts to false and things like that and then so you always do these these transformations at the very beginning and then at some point you get to this point where I want to do the cost-based search and that you can tweak and specify here's the priority for some of these transformations based on what I saw in the earlier stages that I evaluated so the very very beginning you just do basic simplification and normalization so this will be doing logical tree to logical tree transformations so the sub query stuff we'll talk about in a second rules to do that outer joins, inner joins, predicate push down and then empty result pruning like select star from table where false you can throw those things out then you get to what they call pre-exploration and this is doing not the actual cost-based search but this is applying rules to populate the memo table with things you know you think you want to we want to look at as you go down right because the idea again you don't want to blindly search in the beginning you can see the memo table like here's the things that are going to be interesting and guide the search towards those so trivial plan shortcut select one or select star from table limit zero, things like that protection normalization how to identify the stats protection normalization again cleaning up what's in the select output they do an interesting thing where they identify at this point here that they don't have all the stats they would need to in their cost model to give good estimates they will actually stop the query, stop the query planning go tell the system to go run analyze now, collect the data that it's actually missing, imagine the very beginning if you bulk load a table you need something and then when that's available then they can come back and start doing some initial current estimates and then join collapsing when possible then they get to the cost-based search and this can still be multi-stage where they're going to have the first group of transformations that they're allowed to consider and then over time is the number of transformations that they've applied because that's how they're keeping track of how long the search isn't going but then expand the set of transformations they're looking at to do more complicated things so in the very beginning it's for dealing with things like trivial plans like look-ups on a single table where there's a primary key index and stuff like that and then if you still don't find the optimal plan then you can expand that out to a quick estimation on how to do a parallel parallel plan, again now you may be looking at joining multiple tables it's more than looking at a single table and then if there's still more time in the clock as you go along then you can open up to do a larger search for the full plan and then in the last step and then this is still within the Cascade Optimizer Rules for all the different database engines or systems that they're trying to run and support through Cascades they would then have engine-specific transformations that they can apply so for example I think Synapse is like the distributed data warehouse based on SQL Server we'll cover it at the end of the semester you want to do distributed joins or broadcast joins and so forth all those things get applied separately in this last stage here because that's the building upon the plan you built up so far, yes This question is how do you pre-populate the memo table to see the search ahead of time so like it would be populating the memo table but also populating the groups you've sort of generated for this multi-expression on this joining three tables if I know something in these early stages I always want to have this be the outer table and the inner table so I'll seed that transformation ahead of time and then if I have the estimate at this point getting past this step here then I could put that cost in the memo table right away so as I said before the amounts are always going to be based on the number of transformations not the wall clock time and then what's nice about this as they bring up is that as I said no matter what hardware you're running on you could always generate the same query plan given the same database and then if the system gets overloaded because again in this environment it's not a traditional SQL Server it's like a single system where the optimizer runs in the same box in the same hardware as the execution engine so if the system gets overloaded then the time it takes to optimize the query is going to take longer because the threads are burning up running queries or doing whatever and so if you base it on wall clock if I run the query today and the system is overloaded I run the same query tomorrow I may get a different plan if it's overloaded if I'm looking at wall clock time because everything is so much slower so this guarantees that no matter what I always see I always get the best plan or the same plan and then as we said before you want to pre-populate the memo table you will join orderings again this is just your rules to make sure that things land that you're seeding the search in a way that you find the best join earlier and as this is very similar to the Oracle one the order that they appear in the SQL query is the order that will get seeded into the memo table which sometimes makes sense as Oracle did for a long time CalSight and Orca I don't want to talk too much about but I think everyone here is aware of them these are separate these are standalone query optimizers as a service CalSight is used way more than Orca it's written in Java so that limits how many people actually want to use this but the it's CalSight is an interesting piece because I think they have their own they have connections to be able to run queries and things like that it's more than just a query optimizer but mostly for the SQL parser and then the query optimizer but you basically have to define it comes with a bunch of existing rules but if you want to extend it to whatever system you want to support you can then define through their Java code here's the rules I want to transform things here's how to do cost model estimates and so forth this originally came out of a European system called LucidDB this went under and then they took the pieces out of this and then that became CalSight and again it's used for a lot of systems Green Plum is the equivalent to CalSight but maybe the less it's less pluggable with CalSight you can say here's my snowflake dialect, here's my Postgres dialect it supports a bunch of different dialects going in I think a bunch of different dialects going out Orca is more focused on its new query optimizations so this is originally written at I lose track of who bought what Green Plum got bought by EMC EMC got bought by Dell or VMware and then VMware no no Green Plum got by EMC and then VMware bought another company called Pivotal and so EMC had a database products and they didn't know what to do with them so they merged the two they spun out those two divisions of these two companies and then they formed Pivotal and then Pivotal eventually got bought by VMware Gignesh was out there for a while because his startup got bought by them so they were supporting Green Plum which still is there today it's widely used, it's a fork of Postgres, like distributed and run all that queries and then they had this thing called Hawk which is their version of Hive which is SQL on top of Hadoop but then rather than building a separate query optimizer for Hawk and a separate query optimizer for Green Plum they decided let's build a single optimizer as a service and have it support both Hawk and Green Plum in whatever system you want to hook up to it Hawk is still around and nobody actually uses it Green Plum is still around and so that's the primary user of Oracle but it's like CalCite where again you have this API where you have to implement here's what my catalog looks like here's the rules I want you to support and you plug all that in and you can use it we looked at this almost 10 years ago to use it in the system we were building at a time they had like zero documentation you send a bunch of XML files and it would spit things out and we decided not to pursue it but it's still actively maintained today but again I don't think it's used anywhere outside of Green Plum CalCite is more common so there's a paper that they wrote on Orca and they talk about a couple interesting things that haven't been a relevant through some of the discussions we've had maybe this is less of an issue if you're running on the cloud because you control the machines you control everything you can see everything but at the time for on-prem this was tricky so if someone's running your software on their hardware if your optimizer crashes or produces a bad query plan how do you actually then try to debug that and improve things so if someone sends a support ticket and it's slow they can give you the SQL query but if you don't have the right environment if you don't have the right data and you don't know what choices the optimizer made when it was doing the search it's hard to debug this so they had the ability to have the optimizer spit out a complete state of its search for a given query and then send that back to the home base for the developers to generate the query plan they can use that to figure out why it made certain choices over another again if you're running in the cloud you control everything you can figure things out and you don't have to do this but another interesting thing to do is make sure that the cost model estimates are accurate they would do this thing where they would run take a SQL query run it through the optimizer and they would keep track of the best plan and the second best plan or maybe the top ten best plans and then in the background they would run all of them and then see that the relative ordering of what the cost model thought was the best plans actually matches up with reality when you actually run the queries and they would use that to then tweak things and improve things that's like the MongoDB thing right yes but for like the MongoDB didn't have how does this MongoDB didn't have a cost model it literally was just like here they are run them and then whatever they come back the cost model is predicting the right you know physical costs they adjusted if it's off right so yes at a high level it's the same as Mongo but Mongo is not trying to verify whether the cost model estimates are correct they're just running it again I should go double check this is still what Mongo does today I guess you'll find out it looks like they might be trying to use the actual cost model okay when I talked to them before the pandemic they didn't have like any logical physical operators it's just like here's the JSON stuff all right copyrighted GP rewrote or sort of wrote from scratch their query optimizer just like everyone else they started with something that was based on heuristics and then they wrote one that based on Cascades everything's written in Go and they they're more pure to the Cascades model than maybe Microsoft was they would have a DSL that specify here's the rules and the transformations and in some cases where you can't do the transformation entirely through their DSL you could escape into Go code so here's basically what their rules sort of look like right so you have like the matching that you want this DSL and then other things you may want to apply and then this then gets transpiled into Go that they then run into the system all right we're out of time again let me run to rush through randomized really quick because again this exists Postgres does this so rather than doing top down or bottom up what do you if you just did a random walk you got to start with some query plan and that one you can just do a straight conversion of a of the AST from a logical plan into a physical plan and just permute that but the idea is that you look at a bunch of different possible query plans pick where everyone has the best cost keep track of that throw away the ones that are the lowest cost do some kind of randomization or permutation on it to then change things and then do another round and check all over again so you're sort of randomly walking to try to see whether you stumble upon the best query plan so there's an early paper in 1987 that does this with simulated and kneeling again the idea is that you you just swapping operators and like you know the join ordering of two tables randomly if the plan makes it worse then you flip it back flip away the coin and see whether you should just keep pursuing that down that path otherwise you flip it back now there's a bunch of rules you got to write to make sure that you don't do things like if it's a left outer join where you have to care about one being joined before the other you don't you know you don't put things in the wrong order so you have to check to see whether the random change is actually still a correct plan but then if it doesn't get violated then you can flip things around nobody does this what Postgres does do is use a genetic algorithm I think this was introduced in the mid 2000s so they're going to use a genetic algorithm where they're going to have these different generations of the different query plans then you pick which one is the best throw away the ones that are worse then permute the best ones to try to try to find the traits or the genes of the query plans that make it a good query plan and then hope that you sort of stumble upon the best one right so Postgres does this but you only get it when you have you give it a query that has more than 12 tables that you try to join in a single query there's a flag you specify what that cutoff is but by default you don't get this unless it's a 13-way join or higher so it looks like this say we get some random query some combinations of join orders and join algorithms first you cost all of them keep track of the one with the lowest costs up in the corner which is the best one I've ever seen throw away the one that has the worst cost and then permute some portion of the query plans are the ones you keep around and then the next generation you populate those do the same thing check which one has the lowest cost this one does so that's not near our best cost throw away the the weakest one permute them and so forth these are estimated costs why would they be realized I don't like they take these random queries and then run each random one this is all within the query optimizer only Mongo does that like runs everyone this is like using the cost one I'm supposed to talk about next week so in this case how does it come with the first generation this question is how do you come up with the first generation it's random yeah after that it's specifically picking things yes I know I have to join RS and T so let me different join orders different physical operators and then just try it out so I know in the case of Postgres they make sure that the random permutations is deterministic because you don't want to be if you throw the same query at it again you want to end up and you use it running this way you would end up with the same permutation right yes seems like a good idea why it's a good idea because if you have 30 in joins that's a lot of them so going from a random rather than approaching it from almost exhausted free top down view it's just too slow and you're unlikely to get the right join for every single one of them and you're so if you go from a random is that not true am I just spittpunning it? from the first intuition it seems like this should work there's an engineering cost to making sure that you don't permute things incorrectly so now you're maintaining code to do that and that's again that's pretty much FNL's rules for very very large tables large number of tables you know the a greedy search probably would be better again think of like joining 30 tables 40 tables yeah doing randomize seems so much better for that because you saw from around there's so many possibilities so yeah so this is a talk from the guy that actually works on this from Postgres a few years ago he basically said that the one in Postgres at least is broken it's not truly randomized as much as it should be or they're not I don't think they're they're not like incorporating the traits that are best for one drainage in the next I think that I don't say it's a hack but I think that the you don't have time to cover the hybrid one the hybrid one is the better way to do this if you they have basically adaptive algorithm that can figure out like if above a certain number joins it's a greedy based search is actually better we've covered this in the in the reading group we can cover it again I'll mention that next class I just don't have slides for it so again nobody actually does this alright with 20 minutes ago any questions before we switch over to the paper reading yes how do you do it over the logical plan first and then insert the physical components or like this question is like how are you populating this so you have a logical plan and then you you can you could just use the order in the tables they appear in the SQL query that's the order that's the initial ordering and then like you're just flipping things around but a logical plan already has like structures yes even though you have like a different you can get something completely different right you state it is like the logical plan has some structure yes and again depending on if it's interjoined those things are commutative you can swap them anywhere but like if it's an outer join or one of these correlated subqueries like that you gotta be careful about so you gotta make sure you don't violate that but like if it's if things are commutative then you can take you know the logical plan is that you join you know join R, join S, join T and you just commute that no the very beginning it is like you have to generate this first generation right but like once you like so what do you mean all the all these changes are like you're changing one part of the the tree correct yes because the idea is that like like in this case here I somehow figured out the this hash join on the hash join where S followed I R where S is the outer R is the inner you don't know whether that's the why this you know made this one the best so you want to carry that over to the next one in this case up here so that like and then you promote maybe other things about it so that if it truly is the reason why your costs are lower then that trait will get carried over over time okay so let's see how far we can get through um subqueries okay there should be no surprise anyone subqueries are important in SQL you can put them anywhere in your SQL query um I wasn't able to I tried a lesson I wasn't able to get it into a nested query into my order by clause well no but I don't think it actually does anything right I was trying to like have it return a string that the name of the table I want to look up and it runs it but I think it's just getting converted to true so you can do you can put an order by clause in the select statement in the order by you can put a select statement inside the order by clause in some cases but I don't think it actually works right and the way to think of a nested query or subquery is that it's basically like a function that like I have some outer query I'm going to invoke into it and maybe pass on some information or not about what the outer query is the tool I'm looking at in the outer query uh and then I'm going to produce some result that I can then use for for my other query and this is important because this allows people to write more expensive things in a single SQL query rather than have to run multiple queries stage it in temp tables or whatever and then and then put it all together at the end so there's this key distinction that's going to matter a lot between uncorrelated and correlated subqueries and the TLDR is that uncorrelated ones are easy most data systems will be able to handle those it's the correlated ones that are going to cause problems the uncorrelated ones basically means that the whatever my subquery is doesn't depend on anything on the outer query I'm not using any information any attributes any tuple data from the outer query to run that interquery so I only need to logically execute this interquery once whether or not the data system is smart enough to do that depends on the implementation but the ones we can we'll look at we can talk about we'll handle this right so in this example here I want to get the all the students that have the finding the student the name of the student that has the highest grade in some class or across all the students so this interquery on the select max score the students doesn't rely on anything from the outer query on students so I can just run this once materialized result and be able to substitute that in for every tuple I'm looking at on the outer query again I think for most systems they should be able to handle all cases for this actually that's not true there are some cases where they can't hit on correlated subquery the basic idea what you're trying to do is basically you want to move this up and get this to be joined up above yes which one yeah it does not thank you yes have a fix correlated subqueries are the ones we care about again this is where the the subquery is going to reference something in the outer query here so now if we do modify a query to say give me all the students that have the highest grade in their major across all the students in the same major then the basic idea is that for this I'm going to have a for loop that's going to be the going over every single tuple on the outer query and then for each of those tuples I need to do a complete sequential scan or scan on the inner subquery's results right so let's say if I start my outer query the first one I'll look at is the first tuple which is a major so then now when I invoke the inner query I'm going to scan through and now do that join where I take the outer query's major match it against the inner query's major and then get the max score so I'm going to run this entirely and produce a max score of 90 and I'm going to populate this as my output result here next for the next one again for RISC also in CompSign do the same thing start from beginning scan again get max score of 90 that doesn't match what the RISC score is RISC score so therefore that does not produce the output for ODB he's the only one majoring streets so again start from beginning scan through it matches produce my output like that right so what I just showed here is like the worst thing you could possibly do because for every single tuple in the in the outer table I'm rerunning that that join query over again for in the inner table yes the question is like can you have this inner query in a where clause again anywhere right I can have it as a from clause I could have it the projection output correct yes I can put like again you can have it in limits you can have it in you can have nest queries in limits actually I don't know if you can reference to the outer query I don't know if you can limit clauses I think having can do it like anywhere lateral join right it's basically the same thing all right so the paper you guys read we'll see how they do in a second the goal is again basically we want to lift up this this inner query to be at the same level of the outer query because then we can then convert it into a join and we know how to run those officially we know how to optimize those right and we know how to give me what we talked about last two classes we know how to pick the right best join order for these things as well so for this query here ideally we want to move the we want to move the inner query to be now be in the join clause in the from clause against the the outer query here but now we got to be mindful that yes we need the score of of the students that should not be s2 give it a bat sorry but now we want to group by major because that's how we're going to do our join down here because we want to see is for my what's the best score for my outer tuple for a given major and is it my score right so for this example yeah we can look at this and say okay we could write something reasonably easily to do this kind of manipulation you know they're saying since these nest queries can appear anywhere and in this case here it's a straight equality predicate but you can imagine like equals any, exists less than, greater than, anti-joins semi-joins like you can have all different kind of combinations and it's very difficult to to write the rules to capture everything but this is what basically people have been doing for the last 30 years yeah quick question so in the paper when they specify join they don't actually use a join to work is that just the difference in time? the question is in the paper they don't specify join if they're equivalent they just use join you could just have this be your comma and then it's a where clause from the that's that normalization step that I talked about in in like SQL server they'll figure out what's in the where clause and move what's in the where clause that should be part of the join and move that to be part of the join clause they're equivalent alright so again for the last the first paper on nested queries is like 2384 I think the SQL standard ended at I think it's SQL 99 but since then people have been writing much of rules to handle all these different combinations that they're aware of right so this is from the SQL server paper from 2001 that roughly defines the rules that they apply not going to go through all of them but this is how they decide what to when they can decorrelate nested queries in SQL server you look at the documentation they basically have up to 22 rules it's bunch of this like if it does this and this and this and this right this is defines how they're going to do unnesting but again all of these are going to be based on heuristics and rules so on the plus side is these are somewhat easily to somewhat easy to write if you know the pattern that you're looking for and because you can have complete control of like when things get moved and how they get moved it's saying like it handle all possible educations that you've got to deal with all possible ware clauses and any kind of combinations of these nested queries lateral joins is another thing that's complicated then writing these rules by hand is simply too ineffective so this is where the Germans show up in this paper in 2015 so there's actually two papers in the reading list this is the first one there's a fallout one in 2017 that covers more about how they handle external joins or other things but I just wanted to focus on this one because it's an easy read ish on how to do these correlated subqueries but they actually provide as far as they know the first general purpose method to be able to take all correlated subqueries and rewrite them into regular queries without the nesting and the goal is basically to convert all of these subqueries into joins regular like inner joins outer joins whatever it's supposed to be because again we know how to optimize those and the best case scenario we can go from doing something really stupid like running that nested query for every single 2.0 in the outer query which could be in square costs in a nested loop join we can now convert this into an hash join lookup so let me quickly go through one example again we can cover more of the hyper stuff next class so here's that same query that we have before we want to find all the students and their major if they have the if they have the highest grade for their major so the key idea that they're going to have in this paper they're going to introduce this logical concept or a logical operator called a dependent join and it's basically cross product join or cross join except that it's a there's a demarcation or it's a marker to say this thing is specifically being used because it's I'm doing a correlated sub query and the whole goal the process thing we're going to do is try to get rid of that dependent join convert it to regular joins and then optimize it like it normally would so in this case here just the outer query itself is a projection with a filter and a scan but inside this filter clause we have this the sub query inside of this so if we take this and expand this we can convert it into a dependent join because now we have on the the right hand side is the the left hand side the left hand side is the look up on the outer table and then now on the other side the right hand side this is actually the inner query and it's going to be referencing in its filter for its where clause predicate it's going to be referencing the thing on the other side that's what makes it correlated so as I said this dependent join isn't actually a new physical operator it's not something we actually implement in the system it's just an extension to relational algebra in our query plans that allow us to reason about we know we're doing certain transformations in our query optimizer because it's a correlated sub query again so you could just convert this to a cross join and add a little flag and say hey by the way I'm using this for keep track of dependencies but to keep it clean they define as a separate operator so again all you're doing is that for every single tuple in the left hand side you're going to rerun whatever it is on the right hand side and populate the output and so forth so again it's just like a cross product so what they're going to want to try to do now is try to push down the dependent join to the right hand side of the query plan where we have our interquery and eventually you want to try to get it to the bottom and then convert it into a regular join and then how you actually do this is going to depend on the semantics of the query and of the interquery to determine how you actually do these transformations so in this case here I have again the scan on the left hand side and the interquery on the right hand side so I can move the dependent join down one and now just do a a regular join on the outer query and then whatever's coming up to me from the right hand side here so for this particular query to make this work I have to introduce an additional scan that's basically going to do duplicate elimination think of like a select clause where the all the projection output list is also my group I so there's guarantees that I'm always going to have distinct set of attributes or set of values coming up for all the attributes for a given tuple right so now with this this eliminating all the duplicates on this side when I do now a dependent join on this side then this guarantees that I'm only spitting up now the the output that I need to do the join without duplicates as if I was running the right hand side query once per tuple on the on the outer table because again I'm what am I doing I'm doing a max on the student score looking up by major so all I really want is for every major what's the max score so to avoid duplicates of like there's two people in computer science and I don't want to have two entries with the same values of computer science and then score the duplicate elimination scan when I do my join will remove all that for me again this is just a logical this is keeping track of what the dependencies are from the right hand side to the left hand side as I'm going down so I want to keep pushing down the dependent join so at the next stage I can get rid of the going back here I want to get up below the aggregation so I'm going to move my duplicate elimination scan down with me but then put the aggregation above me and again at this point nothing has changed sort of what the output is going to be like everything is still the same because what am I doing here I'm getting all the unique students by score major and then I want to join it with the with the scan on the students table here so this is going to produce you know for every single student every single major here's the highest score and then when I feed that out of my dependent join thinking that this is a cross product when I now do my group by here on the major I'm going to get for one major I'm going to get the max score for it yes yes you have to identify to because you have logic say okay I know that I want to get the I only want to get the single score per major so the equivalent thing you would do here by putting the aggregation above well I need to make sure that I only get one student per major and then you add the group by to make that happen yes there's logic we have to be a reason about what the what the interquery actually wants when you do the join up above to make sure you don't have duplicates you know is there a way to standardize that or could that seem like a complicated question is the way to standardize that it's in the original paper and then it varies based on what the where clause is right this is like something equals something if it's less than or greater than nulls complicates things too right this makes everything harder we're well over time so you have to go please go we can do what we did last time we can pick up on this last class and beginning of next class alright so we push the aggregation above again we want to go further we want to get this dependent join now below this filter well that's easy to do right because there isn't actually any changes we need to make because whether or not we put the filter before after the join that's the same thing if it was a regular query right like you can do a join stupidly without a where clause and there's just a cartesian product and then above that then you do the filter same thing here right so in this case here again it's just a cartesian product for every unique student every unique major I'm going to join against every unique major and a score I'm going to join against all the students table it's going to produce all the possible combinations and then this is going to filter out that I only get where the student from this side is equals the major of the student or the major on that side alright so now at this point here my dependent join is as far as it can go I can't go below these guys because it doesn't make any sense because these are the leaf nodes I'm scanning the tables right so now I want to actually do further optimizations to actually put it into a physical form sorry put it into a form that I know how to optimize like any other query so again we're all still at the logical level we're not doing any none of these are physical operators right so I can convert this dependent join into what it really is which is just a cross join a cartesian product right so there's no where clause it's just you know everything combined with everything on the other side um but then now I have as I was saying before I have a filter above a join that's just an inner join so now I can collapse these two to a single join operator and just move these guys up I can go either even further and recognize that well all you know this is basically just for every single major uh for every single student get their major that's all this is doing here right because my group is going to handle the removing the duplications so I can then I can then convert this now into just a scan on the table s by itself but now to make sure that I had to rewrite my join that I had up here because I was referencing the deduplication scan on the major I need to get rid of that so I can also have a filter above that too so I can just combine that now into a single join where it's the major on this side join the major on that side and the score on this side equals the max score that I produce as my output here because I have the group by the major clause I'm guaranteeing that for every single major I have one score so this is the easy case and again there's some details that I'm glossing over for example he was asking how do I know that I need to push up the group by again it depends on the examination of the where clause in the expression trees so this can then be extended for all possible combinations of correlated queries um and you can convert everything into a joins okay well over time as I was saying only hyper umbra and duck db can do this fully data bricks can do some of it I don't know about other systems I haven't seen whether they make these things as well so like this is like if you're going to build a new system today this is the way to do it again we will cover the other some more cases in the next class we'll cover also then how they're handling joining picking join orders but the other thing we already alluded to today is like okay all the things I've talked about is like hey you have a cost what if your cost estimates are wrong or you just don't know because you haven't even looked at the data before in a lake house environment some new file should have an s3 you don't know what's in them what do you do so that's what we'll cover on next class how to do adaptive query optimization and the TLDR is going to be I've got to generate something I've got to generate some query plans I've got to run something but again I can put in hooks and we'll see how to get feedback from when we scan the table feed that back into our cross models and see whether that helped or not enjoy the weekend so about three in the freezer so about the killer


Hello, this is LaptopMedia, and today, we will show you how to open the Lenovo IdeaPad Gaming 315 and what's inside of it. Getting inside of this machine is certainly not very difficult. After you undo all of the 10 Philips head screws, you need to pry the bottom panel away with a plastic tool. That's it. Despite bearing a lot of similarities to last year's model, we see that the cooling has gone through some change. Now, the heat pipes are equal in size and one of them goes all the way around the fans to attach to a separate tiny heat sink. Additionally, from the visual perspective, the fans' housings are now painted black, which doesn't make them perform better, but certainly gives Lenovo some points for looks. Now, let's take a look at the upgradeability. The IdeaPad Gaming 315 supports dual-channel memory via its two RAM Sodom slots. Lenovo states that there are configurations equipped with up to 16GB of DDR4 memory, but we are pretty positive it can work with 32GB in total. Storage-wise, there is a single 2.5-inch SATA drive bay, additional screws and ribbon cable are found inside the packaging, and an M.2 PCI-EX4 drive slot. Furthermore, there is one M.2 slot, which can also be used for storage, but you have to give away the 2.5-inch drive. In terms of battery, you will get a 45Wh unit. If you'd like to see more disassembly videos of the latest notebooks we would highly appreciate if you hit the like button and subscribe to the channel.