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

【轉(zhuǎn)】Thinking Set-Based .... or not?

轉(zhuǎn)自 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 季陽(yáng) 閱讀(343) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

留言簿(2)

隨筆檔案(12)

搜索

最新隨筆

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲国产天堂久久综合| 99热精品在线观看| 欧美专区日韩专区| 亚洲一区二区三| 国模精品娜娜一二三区| 久久久久综合一区二区三区| 久久天天综合| 99国内精品| 亚洲欧美日韩国产综合| 国产在线观看精品一区二区三区| 开心色5月久久精品| 麻豆av一区二区三区| 99国内精品久久久久久久软件| 在线亚洲精品福利网址导航| 韩国av一区二区| 亚洲欧洲另类| 国产精品亚洲不卡a| 麻豆精品视频在线观看视频| 欧美日韩xxxxx| 久久精品女人天堂| 免费亚洲电影在线观看| 午夜欧美大尺度福利影院在线看 | 午夜日本精品| 亚洲在线国产日韩欧美| 亚洲电影网站| 亚洲一区二区av电影| 亚洲国产欧美久久| 亚洲一区二区综合| 91久久国产综合久久| 亚洲男同1069视频| 亚洲区一区二| 久久精品国产一区二区三区免费看 | 亚洲人成毛片在线播放女女| 国产欧美精品va在线观看| 欧美刺激午夜性久久久久久久| 国产精品日日做人人爱| 亚洲第一天堂无码专区| 国产日韩欧美一区二区三区在线观看 | 久久综合九色综合网站| 国产精品成人av性教育| 欧美国产精品久久| 国产曰批免费观看久久久| 99精品视频免费观看| 亚洲国产专区校园欧美| 欧美一区二区三区久久精品茉莉花| 一本一本久久a久久精品综合麻豆| 久久国产精品第一页| 亚洲欧美日韩综合一区| 欧美欧美天天天天操| 欧美xx69| 亚洲国产另类久久精品| 久久国产精品色婷婷| 午夜日韩在线观看| 国产精品国产a级| 一本色道88久久加勒比精品 | 亚洲日本在线视频观看| 久久野战av| 欧美sm极限捆绑bd| 亚洲国产欧美另类丝袜| 久久久久久网站| 免费观看30秒视频久久| 在线观看日韩av| 久久综合国产精品| 欧美fxxxxxx另类| 亚洲国产成人久久综合| 免费成人黄色av| 亚洲韩国青草视频| 99视频在线观看一区三区| 欧美激情精品久久久| 亚洲国产欧美一区| 9国产精品视频| 欧美日韩国产综合久久| 一本色道久久综合狠狠躁篇的优点| 亚洲视频免费看| 国产精品久久久久久久免费软件 | 欧美性大战xxxxx久久久| 99re6热只有精品免费观看| 亚洲视频碰碰| 国产日韩欧美综合精品| 久久精品夜色噜噜亚洲a∨ | 久久综合色8888| 亚洲成人在线观看视频| 欧美精品v国产精品v日韩精品| 亚洲美女福利视频网站| 亚洲一区免费| 黄色日韩网站视频| 欧美电影在线| 亚洲午夜精品一区二区三区他趣| 性xx色xx综合久久久xx| 亚洲第一色在线| 欧美日韩高清区| 欧美在线精品一区| 欧美激情一区二区久久久| 亚洲一区二区高清视频| 国产深夜精品福利| 欧美韩日一区| 亚洲欧美日韩一区二区| 欧美www视频| 性感少妇一区| 亚洲春色另类小说| 欧美午夜一区二区福利视频| 久久精品欧美日韩| 宅男精品导航| 欧美va天堂在线| 欧美伊人久久久久久午夜久久久久| 1024亚洲| 国产亚洲午夜| 欧美三级第一页| 美女露胸一区二区三区| 亚洲综合欧美| 亚洲品质自拍| 欧美电影在线观看完整版| 欧美一区二区女人| 亚洲视频免费观看| 亚洲激情在线激情| 国产在线精品一区二区夜色| 欧美日韩在线不卡| 欧美.日韩.国产.一区.二区| 香蕉乱码成人久久天堂爱免费| 亚洲精品久久久久久久久久久 | 亚洲欧美国产毛片在线| 亚洲激情成人网| 狠狠久久亚洲欧美专区| 国产精品制服诱惑| 国产精品国产三级国产aⅴ浪潮| 欧美成人精品在线观看| 久久久精品一区| 欧美一区综合| 欧美在线观看网站| 欧美一二三区在线观看| 亚洲天天影视| 一区二区三区四区蜜桃| 亚洲免费观看高清完整版在线观看| 美女精品在线观看| 久久久久久久国产| 久久久久国产一区二区三区四区| 先锋影音网一区二区| 亚洲欧美成人一区二区三区| 亚洲一区免费网站| 亚洲五月婷婷| 亚洲欧美国内爽妇网| 亚洲男人的天堂在线aⅴ视频| 亚洲一区3d动漫同人无遮挡| 在线亚洲免费视频| 一区二区高清| 亚洲主播在线| 性18欧美另类| 久久亚洲国产精品一区二区| 久久久噜噜噜| 欧美激情在线播放| 亚洲国产天堂久久综合网| 亚洲欧洲精品一区二区三区波多野1战4| 亚洲国产成人久久综合一区| 亚洲精品国产欧美| 亚洲午夜在线| 欧美一区二区三区在| 久久久福利视频| 欧美精品1区| 国产精品少妇自拍| 国内精品亚洲| 亚洲三级毛片| 午夜精品网站| 欧美成人精品| 亚洲精品久久久久久下一站 | 亚洲乱码国产乱码精品精98午夜| 99在线精品视频| 欧美影视一区| 欧美福利专区| 国产精品一香蕉国产线看观看| 精品不卡一区二区三区| 99国内精品久久| 久久国产视频网| 亚洲国产影院| 欧美亚洲在线观看| 欧美韩日一区| 国内精品**久久毛片app| 亚洲精品激情| 欧美在线视频一区| 亚洲大片在线| 午夜精品免费在线| 欧美激情麻豆| 精品福利av| 香蕉成人啪国产精品视频综合网| 男男成人高潮片免费网站| 一区二区av在线| 免费成人黄色片| 国模一区二区三区| 亚洲欧美激情诱惑| 亚洲二区视频| 久久av最新网址| 国产精品免费网站| 亚洲精品乱码久久久久久日本蜜臀 | 国内精品亚洲| 亚洲欧美日韩精品久久奇米色影视| 免费久久99精品国产自| 亚洲资源av| 欧美视频亚洲视频| 亚洲老板91色精品久久| 久久一区二区三区国产精品| 亚洲视屏在线播放|