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

【轉(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 季陽 閱讀(338) 評論(0)  編輯 收藏 引用


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


<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

導(dǎo)航

統(tǒng)計

常用鏈接

留言簿(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>
            久久久久久久一区二区三区| 亚洲激情电影在线| 久久精品国产清自在天天线| 亚洲欧美国产日韩天堂区| 亚洲欧美在线网| 亚洲永久精品国产| 久久成人亚洲| 欧美二区在线| 国产精品国产一区二区 | 免费的成人av| 欧美成人一区二区| 欧美精品一区视频| 国产精品日韩欧美| 国产在线一区二区三区四区| 国色天香一区二区| 亚洲欧洲午夜| 午夜精彩国产免费不卡不顿大片| 久久精品九九| 亚洲人在线视频| 一本大道久久a久久综合婷婷| 亚洲午夜未删减在线观看| 欧美伊人精品成人久久综合97| 乱码第一页成人| 国产精品sm| 亚洲高清av在线| 亚洲欧美国产77777| 老司机午夜精品| 亚洲精品欧美日韩专区| 欧美在线视频a| 欧美国产乱视频| 国产一区二区在线观看免费| 亚洲精品中文字幕有码专区| 欧美一区二区视频在线| 亚洲日本国产| 久久中文字幕导航| 国产精品视频xxxx| 日韩一区二区电影网| 久久只有精品| 亚洲欧美日韩另类| 国产精品盗摄久久久| 亚洲精品久久久蜜桃| 久久蜜桃av一区精品变态类天堂| 一本久道久久久| 欧美国产91| 亚洲黄色影院| 久久最新视频| 久久精品一区二区三区四区| 国产欧美韩日| 性刺激综合网| 亚洲欧美日韩国产一区| 国产精品久久久久久福利一牛影视 | 最近中文字幕日韩精品| 99伊人成综合| 亚洲精品在线视频| 欧美一乱一性一交一视频| 久久精品1区| 一区二区免费在线播放| 久久久久国产精品人| 久久久www成人免费毛片麻豆| 欧美日一区二区在线观看 | 亚洲高清三级视频| 久久久久久久综合| 久久国产天堂福利天堂| 国产精品久久久久影院亚瑟| 一本高清dvd不卡在线观看| 亚洲精品国精品久久99热| 久久久欧美一区二区| 亚洲国产精品久久| 亚洲国产欧美一区二区三区同亚洲 | 欧美一级大片在线观看| av成人激情| 国产精品多人| 欧美在线视频网站| 久久av资源网站| 在线观看国产精品淫| 欧美~级网站不卡| 久久亚洲精品一区二区| 亚洲国内精品在线| 99精品视频免费全部在线| 国产精品h在线观看| 欧美亚洲综合网| 久久久久久亚洲综合影院红桃 | 亚洲人成7777| 亚洲乱码精品一二三四区日韩在线 | 欧美大片在线看免费观看| 久久亚洲高清| 在线视频你懂得一区| 宅男噜噜噜66国产日韩在线观看| 国产精品美女久久久久av超清| 欧美一区二区三区喷汁尤物| 久久影院午夜论| 亚洲一区二区免费视频| 久久国产一区| 亚洲第一中文字幕在线观看| 欧美一区二区视频观看视频| 久久成人精品无人区| 亚洲黄色一区| 亚洲一区黄色| 亚洲欧洲视频在线| 亚洲欧洲av一区二区| 亚洲国产一二三| 亚洲一区日韩在线| 亚洲国产日韩一级| 亚洲一区免费视频| 日韩视频在线观看免费| 欧美一区二区三区免费在线看 | 久久久人成影片一区二区三区| 欧美高清在线观看| 校园激情久久| 久久嫩草精品久久久精品一| 亚洲欧美成人一区二区在线电影 | 久久久久久久成人| 亚洲小视频在线观看| 麻豆精品网站| 亚洲欧美一区二区精品久久久| 欧美专区福利在线| 午夜精品久久久久影视| 欧美激情精品| 美日韩精品视频| 欧美三级网址| 亚洲伦理一区| 亚洲成人自拍视频| 久久久久久久999| 久久免费高清视频| 国产婷婷一区二区| 亚洲自拍电影| 欧美一区二区性| 国产欧美日本在线| 亚洲综合首页| 午夜欧美电影在线观看| 国产精品二区影院| 一本一道久久综合狠狠老精东影业| 亚洲欧洲在线免费| 欧美多人爱爱视频网站| 欧美成人一区在线| 亚洲国产精品ⅴa在线观看| 久久久久欧美精品| 欧美成人自拍| 亚洲另类视频| 欧美日韩视频| 亚洲夜间福利| 久久久精品tv| 在线国产日韩| 欧美凹凸一区二区三区视频| 欧美激情精品久久久久久蜜臀| 亚洲国产免费| 欧美日本一道本| 亚洲一区三区视频在线观看 | 亚洲系列中文字幕| 国产欧美日韩专区发布| 久久久美女艺术照精彩视频福利播放| 久热精品视频在线| 亚洲黄色有码视频| 国产精品99免费看 | 日韩亚洲国产欧美| 欧美日韩一区二区免费在线观看| 99日韩精品| 久久成人免费日本黄色| 在线看日韩欧美| 欧美日韩美女在线观看| 亚洲欧美三级伦理| 欧美激情一区二区三区在线视频观看 | 一本大道久久a久久精二百| 午夜亚洲一区| 亚洲成色精品| 国产精品久久久久久久电影| 性8sex亚洲区入口| 欧美高清在线视频观看不卡| 一本久久知道综合久久| 国产精品成人一区二区三区吃奶| 久久精品99久久香蕉国产色戒| 欧美黄在线观看| 亚洲精品人人| 国产自产女人91一区在线观看| 欧美风情在线| 久久岛国电影| 日韩一区二区精品视频| 鲁大师影院一区二区三区| 亚洲无人区一区| 狠狠色丁香婷婷综合影院| 欧美日本高清一区| 久久色在线播放| 亚洲欧美精品在线观看| 欧美激情免费在线| 久久精品成人欧美大片古装| 一区二区日韩精品| 亚洲第一主播视频| 国产一区在线视频| 欧美午夜片欧美片在线观看| 久久综合九色综合欧美狠狠| 亚洲午夜精品久久久久久浪潮| 亚洲国产日本| 欧美激情一二区| 欧美成人一区二区| 久久一二三国产| 久久久久**毛片大全| 欧美亚洲网站| 欧美一级在线亚洲天堂| 亚洲一区二区在| 亚洲一级特黄|