青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

【轉】Thinking Set-Based .... or not?

轉自 http://weblogs.sqlteam.com/jeffs/archive/2007/04/30/60192.aspx


Thinking "Set-Based"

So, I hear you're a "set-based SQL master"!

As Yoda once said, you've "unlearned what you have learned". You've trained yourself to attack your database code not from a procedural, step-by-step angle, but rather from the set-based "do it all at once" approach. It may have taken weeks, months or even years to finally obtain this enlightened state of "database zen", but it was worth it. Your SQL code is short, fast, and efficient. There is not a cursor in sight. You have reached the point where you can write a single SELECT that replaces hundreds of lines cursors, temp tables and client-side processing. Life is good.

As I read somewhere once, you don't tell SQL how to do it, you tell SQL what you want, and that's a great way of thinking about it. A procedural programmer gets bogged down with the details, and has to concentrate on breaking things down into small pieces, explicitly reading and processing one row of data at a time, and figuring out how to combine those results together at the end to make it all work. A set-based SQL programmer worries about none of those things: In the set-based world, you state your relations and join the tables together, add some grouping and criteria, and it is the database engine that worries about the specifics.

Well, maybe not ... You might not want to abandon all of the things that you learned from your procedural background. There's a danger in misunderstanding that set-based programming means "doing it all at once", and thinking that it forbids processing things "one at a time" or "in steps". Sometimes, when you get too comfortable in the set-based way of thinking, you abandon the good things that you learned as a procedural programmer. The two mindsets aren't as different as you might think!

Approaching a Problem

What if I ask you to write a somewhat complicated SELECT, something like this:

"Write a SELECT that returns, for a given @Year, the total sales by office, and also the office's top salesperson (highest total sales for the year) with their salary (as of the last day of that year), their total bonuses for that year, and their hire date."

While this isn't rocket science, what makes this request slightly complicated is that it appears there are at least 3 different transactional queries (sales by employee, sales by office, bonus totals by employee) that we need to put all together, as well some point-in-time reporting off of a history table (employee salaries) which can be difficult depending on how the table is structured.

Now, how does a "set-based" programmer attack this? The schema and the specifics are not important, it is really just the general approach that I am commenting on.

Do you start by immediately finding all of the necessary tables and put them all into 1 big SELECT by joining everything that matches? Then, from there, you may start adding columns and expressions to your GROUP BY clause, adding in criteria and CASE expressions, maybe a DISTINCT before it all? And then, if that doesn't work, maybe you add some correlated subqueries to your SELECT list, or move things in and out of derived tables? Then more GROUPING, more criteria, more JOINs, more moving things and shifting parts of the SELECT around until it "looks right" and it "seems to work"?

Well, that does seem to be the set-based approach for many, since you get so trained and so used to thinking of the "big picture", and not worrying about details, that you just assume that you can dive right in and start joining and selecting and eventually you'll get there. We've all done it. That's what you want to do, after all. We don't want to think that we need to break things down into smaller, discrete steps, or that things should be "processed" on step at a time. It goes against everything that we've been trying to train ourselves to do ever since we embraced the concept of relational database programming, right?

Wrong!

Thinking in Sets = Thinking in Steps

It is so important to understand that "thinking set-based" does not conflict with "thinking in steps" !! In fact, it is more important than ever in some ways, especially as your data and your schemas and your requirements become more complex.

In the above example, if you "dive right in" and start joining and selecting and grouping and seeing how things work, that is exactly the wrong way to do it! You need to remember that the skill you learned from your procedural world -- breaking larger problems down into smaller parts -- still applies even in when writing SQL.

Looking at the above statement, a really good SQL developer will immediately break the problem down into smaller, completely separate parts:
a SELECT that returns 1 row per Office, with each Office's total sales for a @Year
a SELECT that returns 1 row per employee, with their salary as of the last day of a given @Year
a SELECT that returns 1 row per employee, with their total bonus amount for a given @Year
a SELECT that returns 1 row per Office, with the top salesperson (Employee) and their sales amount, for a @Year
Starting with those 4 basic pieces, all of which are completely isolated from the others, is the way to begin to approach the problem. You don't focus on returning employee names, or sorting, or formatting dates -- you focus on the data, and returning it in small parts that will eventually all fit together. For each SELECT, you can test it and optimize it and verify the data, and only at the very end, when all the individual parts are working, do you put them together. This sounds familiar, doesn't it? Much like a procedural programmer who breaks their application down into smaller parts via functions or classes or whatever tools their language provides, I am suggesting that the overall approach is still valid and in fact a great idea even when writing SQL!

In fact, when writing a SELECT that requires multiple non-related transactional tables this is really the only way to go about solving this problem, since each one must be fully grouped and summarized and ready to join on matching key columns before we can begin to even think about combining the results. In this case, it is only at the very end, when all of our individual SELECTs are grouped by Office or Employee, that we join them together as derived tables.

In addition, the "step-based" approach involves understanding that things like formatting dates, deciding on how to output a name (first/last or last/first, etc), or sorting is irrelevant to the larger problem. In a complicated select with lots of calculations or point in time reporting, if you can write a select that returns 1 row per employee (determined by the employee's primary key column, let's say EmployeeID), that is all you need; if you know that the Employee table has first name, last name, hire date, and a simple relations to their Department, then don't worry about any of that until the very last step! Just focus on returning a reference to the entity (EmployeeID) and calculating the results or values that you are trying to return per entity (total sales, salary, bonus), and only when everything is accurate and correct should you dress things up with the other attributes of the entity which are trivial to obtain (employee name, hire date) through simple joins.

Putting it all Together

In the end, it really does resemble procedural programming quite a bit in that each of these little, self-contained parts, all of which are responsible for doing their job accurately and efficiently, are much like functions or classes. And our primary SELECT is like the main program that calls each of them and in the end puts them all together:

select OfficeSales.OfficeID,
OfficeSales.TotalSales as OfficeSales,
Offices.OfficeName,
TopSalesPerson.EmployeeID,
TopSalesPerson.TotalSales as EmployeeSales,
Employees.EmployeeName,
Employees.HireDate,
EmpSalaries.Salary,
EmpBonus.Bonus,
from
( .... ) OfficeSales
inner join
( .... ) TopSalesPerson on OfficeSales.OfficeID = TopSalesPerson.OfficeID
inner join
( .... ) EmpSalaries on TopSalesPerson.EmployeeID = EmpSalaries.EmployeeID
inner join
( ... ) EmpBonus on TopSalesPerson.EmployeeID = EmpBonus.EmployeeID
inner join
Employees on TopSalesPerson.EmployeeID = Employees.EmployeeID
inner join
Offices on OfficeSales.OfficeID = Offices.OfficeID

When all the code is in place, this will probably be a very large, complicated SELECT. But looking at this way, doesn't it look pretty simple? And each of those derived tables, on their own, will also be quite simple. That's the approach we want to take!

(note: In addition to using derived tables, you can use Common Table Expressions to facilitate this approach, since they work essentially the same way but are often easier to read and incorporate into your complicated SELECT statements. Views and parameterized User Defined Functions can be useful as well. The same concepts still apply -- divide and conquer!)

Only now, at the very end, do we worry if some of those joins should become LEFT OUTER JOINs, since maybe some employees might not have a bonus for a given year, and so on. Getting the employee's Name and HireDate and the name of each Office is done here, at the very end, where it is very easy and clear since we have just focused on returning the key columns for both of those entities in our derived table results.

Think again!

So, the next time you dive right into and start joining and selecting because you know that a "set-based master" doesn't worry about breaking down the details, consider instead becoming a "step-based set-based programmer", and break down your large problem into smaller, easily solvable steps. Even in T-SQL, this is the way to go and it will make your life easier, your code simpler, and often more efficient as well. Don't completely disregard your past experience as you become a relational database programmer, learn how to combine the best of both worlds.

posted on 2007-09-28 12:42 季陽 閱讀(343) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


<2011年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

導航

統計

常用鏈接

留言簿(2)

隨筆檔案(12)

搜索

最新隨筆

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久久999精品免费| 欧美在线关看| 欧美美女福利视频| 亚洲精品综合| 99热在这里有精品免费| 欧美色一级片| 欧美一区二区三区喷汁尤物| 亚洲免费一区二区| 韩国成人理伦片免费播放| 老司机午夜免费精品视频| 久久一区国产| 亚洲精品久久嫩草网站秘色| 亚洲国产精品v| 欧美日韩在线播放| 久久国产欧美日韩精品| 老色鬼精品视频在线观看播放| 亚洲精品网址在线观看| 一区二区三区四区蜜桃| 国产一在线精品一区在线观看| 久久综合伊人77777麻豆| 欧美成va人片在线观看| 国产精品99久久久久久人| 亚洲综合精品| 亚洲经典在线| 亚洲综合精品四区| 亚洲国产高清自拍| 亚洲在线观看免费| 又紧又大又爽精品一区二区| 亚洲精品一区在线| 国产视频一区二区三区在线观看| 欧美福利一区| 国产精品久久久久久影院8一贰佰| 久久久人成影片一区二区三区| 欧美成人一区二区三区| 欧美一区二区三区在线观看视频 | 欧美亚洲系列| 猛男gaygay欧美视频| 亚洲欧美中文另类| 欧美成人精品在线| 久久久久亚洲综合| 国产精品黄色| 亚洲黄色片网站| 黄色成人在线网址| 这里只有精品电影| 亚洲精品国产精品国自产在线| 亚洲综合三区| 亚洲一区二区免费看| 麻豆久久婷婷| 久久夜色精品| 国产亚洲亚洲| 午夜精品一区二区三区在线视| 日韩特黄影片| 欧美激情一区二区| 欧美不卡视频| 影院欧美亚洲| 久久久www成人免费精品| 香蕉久久国产| 国产精品久久久久7777婷婷| 亚洲精品国产精品乱码不99按摩| 激情av一区| 久久精品99国产精品日本| 欧美一区二区视频在线观看2020 | 久久精品国产一区二区三区| 亚洲一品av免费观看| 欧美大色视频| 亚洲国产另类精品专区| 亚洲欧洲偷拍精品| 理论片一区二区在线| 牛牛国产精品| 亚洲国产精品一区在线观看不卡| 久久精品国产视频| 欧美1区2区3区| 亚洲国产三级在线| 欧美1级日本1级| 亚洲国产裸拍裸体视频在线观看乱了中文 | 欧美激情二区三区| 亚洲国产日韩在线| 欧美精品一区二区三区在线播放| 欧美激情一区二区三区不卡| 亚洲欧洲精品一区二区| 欧美精品入口| 一区二区三区视频观看| 欧美一区二区三区四区在线| 国产亚洲视频在线观看| 久久夜色精品一区| 亚洲国产精品传媒在线观看| 一区二区久久| 国产欧美一区二区三区沐欲| 欧美资源在线| 亚洲激情电影中文字幕| 亚洲视频一区| 国语自产精品视频在线看| 牛牛国产精品| 在线视频欧美日韩| 久久久久成人精品| 亚洲精品久久久久久久久久久| 欧美三区美女| 久久九九国产| 99精品视频免费观看视频| 久久国产精品久久w女人spa| 亚洲激情图片小说视频| 欧美亚男人的天堂| 久久午夜视频| 亚洲一区三区视频在线观看| 乱人伦精品视频在线观看| 一区二区不卡在线视频 午夜欧美不卡'| 国产精品久久久久aaaa| 快she精品国产999| 亚洲一区亚洲| 亚洲国产高清在线| 久久国产精品99国产| 日韩视频免费在线观看| 国产一区自拍视频| 欧美日韩国产一区二区三区| 久久精品道一区二区三区| 亚洲美女毛片| 欧美成人免费在线观看| 欧美一区激情| 亚洲一区二区3| 亚洲欧洲日本在线| 激情校园亚洲| 国产精品一区二区视频| 欧美日韩国产va另类| 久久综合一区二区| 欧美亚洲在线视频| 亚洲午夜一区二区| 亚洲美女av电影| 亚洲电影观看| 美女黄毛**国产精品啪啪| 欧美一区二区视频观看视频| 一区二区三区av| 亚洲日韩欧美视频一区| 黄色工厂这里只有精品| 国产欧美亚洲视频| 国产精品一区二区三区免费观看| 欧美日本一区二区视频在线观看| 噜噜噜久久亚洲精品国产品小说| 欧美在线观看www| 午夜精品久久久久久99热软件| 一区二区三区国产| 一本色道久久综合亚洲精品不| 亚洲人成网站精品片在线观看| 欧美岛国激情| 欧美成人午夜激情视频| 欧美成人免费全部| 欧美黄色小视频| 欧美激情精品久久久久久大尺度| 欧美va亚洲va日韩∨a综合色| 久久综合九色综合久99| 美女黄网久久| 亚洲电影免费| 亚洲国产精品成人一区二区| 91久久精品日日躁夜夜躁国产| 亚洲国产精品专区久久| 最新日韩在线视频| 日韩一级在线观看| 亚洲一区二区三区777| 亚洲欧美视频在线观看| 性做久久久久久免费观看欧美| 欧美尤物巨大精品爽| 久久精品一区四区| 免费成人在线视频网站| 欧美精品日韩一本| 国产精品乱码| 好吊日精品视频| 亚洲国产视频一区| 亚洲一品av免费观看| 久久大逼视频| 欧美成人激情视频| 亚洲另类春色国产| 亚洲一区二区精品在线观看| 欧美资源在线观看| 嫩草国产精品入口| 国产精品福利av| 精品电影一区| 亚洲伦理自拍| 久久精品久久99精品久久| 美女久久一区| 一本色道久久88综合日韩精品| 亚洲欧美精品在线观看| 美女999久久久精品视频| 欧美日韩日本视频| 国内精品久久久久久久影视蜜臀 | 亚洲高清毛片| 亚洲一区二区三区四区中文| 欧美一区二区三区男人的天堂| 美女视频网站黄色亚洲| 一区二区三区久久久| 久久视频精品在线| 国产精品入口66mio| 亚洲精品在线免费观看视频| 欧美一区二区三区成人| 亚洲激精日韩激精欧美精品| 欧美一区二区视频在线观看| 欧美精品导航| 亚洲电影毛片| 久久九九99视频| 亚洲视频 欧洲视频| 欧美高清视频一二三区| 好吊视频一区二区三区四区|