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

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

<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

導航

統(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>
            欧美h视频在线| 伊人久久久大香线蕉综合直播 | 亚洲国产欧美久久| 经典三级久久| 亚洲人被黑人高潮完整版| 亚洲第一主播视频| 亚洲欧洲三级电影| 宅男精品视频| 欧美中在线观看| 乱码第一页成人| 欧美激情一区二区久久久| 亚洲精品人人| 亚洲线精品一区二区三区八戒| 亚洲永久字幕| 玖玖玖国产精品| 欧美精品日日鲁夜夜添| 欧美日韩一区二区视频在线观看| 国产精品对白刺激久久久| 国产午夜亚洲精品不卡| 亚洲电影免费观看高清完整版| 亚洲精品在线观看免费| 欧美一区午夜精品| 欧美二区在线看| 中文精品视频一区二区在线观看| 久久福利视频导航| 欧美日韩综合精品| 国产一区二区三区成人欧美日韩在线观看 | 亚洲乱码国产乱码精品精| 亚洲一区二区视频在线观看| 久久影院午夜论| 在线综合欧美| 欧美成人激情视频免费观看| 国产精品私拍pans大尺度在线| 在线观看国产精品网站| 欧美一区二区精品| 99riav1国产精品视频| 久久免费黄色| 国产日韩欧美二区| 亚洲一级二级| 亚洲国产第一| 久久国产婷婷国产香蕉| 国产精品wwwwww| 亚洲精选中文字幕| 你懂的视频欧美| 午夜精品一区二区三区在线 | 午夜亚洲一区| 欧美日韩综合| 一区二区三区免费在线观看| 欧美成人激情视频免费观看| 性欧美xxxx大乳国产app| 欧美三级网页| aa级大片欧美三级| 亚洲精品美女免费| 欧美精品粉嫩高潮一区二区 | 国产精品午夜视频| 亚洲人体1000| 免费不卡中文字幕视频| 欧美一区2区视频在线观看| 欧美午夜免费影院| 野花国产精品入口| 亚洲国产va精品久久久不卡综合| 久久久久久伊人| 国产一区视频观看| 久久人人爽国产| 久久精品一区蜜桃臀影院| 好看不卡的中文字幕| 久久精品三级| 久久精品国产亚洲a| 红桃视频一区| 欧美成人首页| 欧美黄网免费在线观看| 一本色道久久精品| 中文精品视频一区二区在线观看| 国产精品草莓在线免费观看| 亚洲尤物在线| 亚洲一区二区三区四区五区黄| 国产情侣一区| 久久影院亚洲| 欧美国产激情二区三区| 一本久久综合亚洲鲁鲁五月天| 亚洲另类视频| 国产精品一级二级三级| 久久中文在线| 欧美激情成人在线| 亚洲在线黄色| 久久久亚洲午夜电影| 91久久国产综合久久91精品网站| 亚洲经典三级| 国产精品亚洲美女av网站| 另类图片综合电影| 欧美日韩一区二区三区高清| 久久成人精品| 欧美日本在线看| 性一交一乱一区二区洋洋av| 久久亚洲国产精品一区二区| 亚洲午夜在线观看| 久久精品成人一区二区三区| 亚洲精品欧美日韩| 亚洲免费在线视频| 亚洲人www| 欧美一级片一区| 99在线观看免费视频精品观看| 午夜精品视频网站| 日韩视频在线一区二区三区| 午夜精品影院| 中日韩高清电影网| 久久综合成人精品亚洲另类欧美| 午夜精品影院| 欧美日韩另类视频| 欧美成人蜜桃| 国内外成人免费视频| 欧美日韩免费观看一区| 欧美精品成人在线| 欧美伊人久久| 欧美精品日本| 欧美成人国产| 国产一区亚洲一区| 亚洲一二三四久久| 亚洲巨乳在线| 久久久www成人免费毛片麻豆| 亚洲在线日韩| 欧美激情中文字幕乱码免费| 久久综合伊人77777| 国产精品视频免费观看| 99精品欧美一区二区三区| 亚洲激情视频在线| 久久精品人人做人人爽| 久久高清福利视频| 国产精品自拍在线| 在线亚洲欧美视频| 亚洲永久在线观看| 欧美日韩国产精品一卡| 亚洲激情国产| 亚洲乱码精品一二三四区日韩在线 | 久久噜噜噜精品国产亚洲综合| 亚洲一区观看| 欧美日韩你懂的| 一本久久a久久精品亚洲| 亚洲免费观看高清完整版在线观看| 久久亚洲精品一区| 欧美大片一区二区| 亚洲精品一二三| 欧美日本精品在线| 99视频超级精品| 亚洲免费视频观看| 国产老女人精品毛片久久| 欧美一区二区成人| 久久在线视频| 亚洲人成人77777线观看| 欧美理论片在线观看| 日韩西西人体444www| 午夜日韩在线观看| 狠狠综合久久av一区二区小说| 久久午夜精品一区二区| 亚洲国产成人久久综合| 夜夜嗨av色一区二区不卡| 欧美日韩中文精品| 亚洲在线免费| 欧美成年人视频网站| 一区二区成人精品| 国产精品一区在线播放| 久久精品女人的天堂av| 亚洲国产精品精华液网站| 国产精品99久久久久久人| 国产精品视频一区二区三区| 久久精品123| 日韩一区二区精品| 久久国产精品久久久久久电车| 亚洲第一在线视频| 欧美视频亚洲视频| 久久精品视频va| 99热在这里有精品免费| 久久综合五月| 亚洲一区在线免费| 久久男人av资源网站| 亚洲精品在线看| 久久久久久久97| 这里只有精品电影| 在线色欧美三级视频| 国产精品老牛| 久久综合九色| 亚洲欧美日本国产有色| 亚洲国产合集| 久久综合色天天久久综合图片| 亚洲无毛电影| 亚洲激情第一页| 国产欧美日韩精品a在线观看| 另类春色校园亚洲| 欧美亚洲日本国产| 亚洲视频欧美视频| 亚洲第一在线视频| 久久影院午夜论| 亚欧成人精品| 一区二区三区 在线观看视| 在线成人激情黄色| 国产伪娘ts一区| 国产精品女人久久久久久| 欧美日韩性生活视频| 欧美成人一区二区| 久久国产精品久久久久久电车|