Saturday 15 June 2013

sql - filter first or join first? -


text after "itemprop =" text ">

I basically just wrote a question from the customer 1. 1.query to find out the annual total order number per larger , I set the filtered outcome and the second result included it with the set which came to know the name of the customer. Interestingly, I think the filter will first produce better performance because fewer results are required to be connected. So I wrote another query before and to join the filter, which looks nyper from the first query. The result is as much as I would expect because all the time in the result is short. But I'm not sure which time is most important? Or is this a just coincidence? How to think about performance? Use [Advisor 2012] Schedule data; --1.filter First, join the second tempC selection. * Counting from the Sales.SalesOrderHeader group (CustomerID) as CustomerOrderAmt, Counting CustomerID (CustomerID) & gt; by TempP.ListName, TempP.LastName (, Selection year (OrderDate), OrderYear, CustomerID as Year (OrderDate) ; 1) as tempC joining (as selection p.FirstName, p.LastName, c.CustomerID as P. Person.Person from Sales.Customer c.PersonID = p.BusinessEntityID on c) as tempP As included tempC.CustomerID = tempP.CustomerID, order by tempC.OrderYear, tempC.CustomerID first --2.join GO, select second filter year (so.OrderDate) ORDERDATE, so.CustomerID, count (so. CustomerID) CustomerOrderAmt, p Prior Sales Sales named W, p. Join the sales list as Sales.SalesOrderHeader by name. As customer's in the form of C Customer ID = c.CustomerID joins the person. C.PersonID = Advertising on the p.BusinessEntityID group (such as orderdate), therefore .CustomerID, p.FirstName, calculate having p.LastName (so.CustomerID) & gt; 1 go

After

the query optimizer can choose to perform in any order That makes the same logical result, so if you try to filter first and then join it, unless you force it using a table variable or a temporary table, then the adapter will then be in the filter may involve.

If you really believe that the adapter is doing something stupid, then you can try things like table var or floating table, but what can be really stupid, due to which lots Be advanced.

He said, sometimes by the way you write the query , you should generally see the execution plan affecting what adapter does. If they are identical, go with the clear code. If they do not test and do not test again, and go with the best things.

No comments:

Post a Comment