Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option...

32
569 Index A acyclic graphs. See directed acyclic graph (DAG) ad-hoc paging for row numbers, 243–244 aggregate binding, 39 aggregate bitwise operations AND operator, 363–364 OR operator, 362–363 XOR operator, 364 aggregations. See also pivoting aggregate bitwise AND operator, 363–364 aggregate bitwise operations specialized solution, 360–362 aggregate bitwise OR operator, 362–363 aggregate bitwise XOR operator, 364 aggregate product specialized solution, 358–360 aggregate product using pivoting, 346–347 aggregate string concatenation specialized solution, 358 C# code for UDA, 348 calculating using OVER clause, 315–319 CLR code in databases, 347–348 creating assemblies in Visual Studio 2005, 353–357 CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting, 345 enabling CLR and querying catalog views, 357 grouping factor. See grouping factor histograms. See histograms implementing UDA, 348 median value, 364–367 OVER clause overview, 315–316 overview, 315 ROLLUP option. See ROLLUP option running, 321–323 sliding, 328–329 specialized solutions overview, 358 string concatenation using pivoting, 345–346 testing UDA, 357–358 tiebreakers, 319–321 UDA overview, 347 Visual Basic .NET code for UDA, 351 Year-To-Date (YTD), 330–331 algebrizer aggregate binding, 39 grouping binding, 39–40 name resolution, 38–39 operator flattening, 38 overview, 37–38 type derivation, 39 algorithms, join forcing a strategy, 295 hash, 294–295 loop, 291–292 merge, 292–293 overview, 291 analytical ranking functions ad-hoc paging for row numbers, 243–244 cursor-based solution for row numbers, 234–235 dense rank overview, 246 DENSE_RANK function overview, 246 IDENTITY-based solution for row numbers, 235–237 multipage access for row numbers, 244–245 nonunique sort column with tiebreaker for row numbers, 229–230 nonunique sort column without tiebreaker for row numbers, 230–233 NTILE function in SQL Server 2005, 247–249

Transcript of Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option...

Page 1: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

569

Index

Aacyclic graphs. See directed acyclic graph (DAG)ad-hoc paging for row numbers, 243–244aggregate binding, 39aggregate bitwise operations

AND operator, 363–364OR operator, 362–363XOR operator, 364

aggregations. See also pivotingaggregate bitwise AND operator, 363–364aggregate bitwise operations specialized

solution, 360–362aggregate bitwise OR operator, 362–363aggregate bitwise XOR operator, 364aggregate product specialized solution,

358–360aggregate product using pivoting, 346–347aggregate string concatenation specialized

solution, 358C# code for UDA, 348calculating using OVER clause, 315–319CLR code in databases, 347–348creating assemblies in Visual Studio 2005,

353–357CUBE option. See CUBE optioncumulative, 323–328custom aggregations overview, 344–345custom aggregations using pivoting, 345enabling CLR and querying catalog views, 357grouping factor. See grouping factorhistograms. See histogramsimplementing UDA, 348median value, 364–367OVER clause overview, 315–316overview, 315ROLLUP option. See ROLLUP option

running, 321–323sliding, 328–329specialized solutions overview, 358string concatenation using pivoting, 345–346testing UDA, 357–358tiebreakers, 319–321UDA overview, 347Visual Basic .NET code for UDA, 351Year-To-Date (YTD), 330–331

algebrizeraggregate binding, 39grouping binding, 39–40name resolution, 38–39operator flattening, 38overview, 37–38type derivation, 39

algorithms, joinforcing a strategy, 295hash, 294–295loop, 291–292merge, 292–293overview, 291

analytical ranking functionsad-hoc paging for row numbers, 243–244cursor-based solution for row numbers,

234–235dense rank overview, 246DENSE_RANK function overview, 246IDENTITY-based solution for row numbers,

235–237multipage access for row numbers, 244–245nonunique sort column with tiebreaker for row

numbers, 229–230nonunique sort column without tiebreaker for

row numbers, 230–233NTILE function in SQL Server 2005, 247–249

Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM

Microsoft Press
Note
Inside Microsoft® SQL Server™ 2005: T-SQL Querying (ISBN 0-7356-2313-9) by Itzik Ben-Gan (Solid Quality Learning), Lubor Kollar, Dejan Sarka. Published by Microsoft Press. Copyright © 2006 by Itzik Ben-Gan.
Page 2: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

570

analytical ranking functions, continuedNTILE function overview, 247NTILE function set-based solutions, 249–252overview, 222–224paging overview for row numbers, 243partitioning, 233–234performance comparisons for row numbers,

237–242RANK function overview, 246rank overview, 246row number overview, 224, 228–229ROW_NUMBER function determinism,

226–227ROW_NUMBER function overview, 224–226ROW_NUMBER function partitioning, 227set-based solutions for rank and dense rank,

247set-based technique for row numbers, 227

analyzing execution plansgraphical plans, 108–115overview, 107–108textual showplans, 116–117XML showplans, 117–119

analyzing trace data, 90–103analyzing waits at instance level, 71–79ancestors

compared to subordinates function, 485compared to subtrees, 515creating fn_managers function, 484–485creating management chain (CTE solution),

486creating management chain with two levels

(CTE solution), 486–487limiting levels (CTE solution), 487overview, 484testing fn_managers function, 485–486

AND operatoraggregate bitwise, 363–364flattening with algebrizer, 38TOP option and APPLY table operator

solutions, 405–408

APPLY table operatorCROSS keyword, 389–390first page solution, 403–404matching current and previous occurrences

solution 1, 398–399matching current and previous occurrences

solution 2, 399–400matching current and previous occurrences

solution 3, 400–401matching current and previous occurrences

solution 4, 401–402matching current and previous occurrences

solution overview, 397–398median value solution, 413–415n rows for each group solution 1, 392–394n rows for each group solution 2, 394n rows for each group solution 3, 395n rows for each group solution 4, 395–396n rows for each group solution 5, 396–397n rows for each group solution overview,

391–392next page solution, 404–409AND operator, 405–408OUTER keyword, 390overview, 20–22, 381, 388paging solution overview, 402–403passing column reference parameter, 390–391previous page solution, 409–411random rows solution, 411–412TOP option WITH TIES, 389

argumentscommon table expression (CTE), 215derived tables, 213

assemblies, creating in Visual Studio 2005, 353–357

assigning left and right values in nested setscreating relationships (CTE solution), 521–522creating script for fn_empsnestedsets function

(UDF solution), 523illustration of model, 518–519

analyzing execution plans

Z02I623139.fm Page 570 Monday, March 6, 2006 2:15 PM

Page 3: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

571

materializing nested sets relationships in tables (CTE or UDF solution), 526

overview, 518producing binary sort paths (CTE solution),

519–522testing script for fn_empsnestedsets function

(UDF solution), 525assignment SELECT statements, 450–452assignment UPDATE statements, 452–454asynchronous sequence generation, 433–434attributes

compared to dimensions, 374pivoting, 331–335

audience for this book, 4auxiliary table of numbers

creating and populating, 253–256overview, 252returning, 256

Bbalanced trees, 124–128bill of materials (BOM) scenario

creating script for fn_BOMTC (UDF solution), 533–534

generating all paths in BOM, 534–535isolating shortest paths (CTE solution),

535–537overview, 464–468running code for transitive closure, 531–533transitive closure overview, 531

binding, 36bitwise operations

AND operator, 363–364OR operator, 362–363XOR operator, 364

block of sequence values, 431–433BOM scenario. See bill of materials (BOM)

scenario

CCartesian product (cross join)

example, complex, 4example, simple, 3–4improving performance of queries, 5–7overview, 265performing, 6–7

clearing cache, 105CLR (common language runtime), 347–348clustered index seek + ordered partial scans

index access methods, 148–150index optimization scale, 161–162

clustered indexes, 124–128code samples, downloading, xxivcommon language runtime (CLR), 347–348common table expression (CTE)

ancestors, creating management chain, 486ancestors, creating management chain with two

levels, 486–487ancestors, limiting levels, 487arguments, 215container objects, 218–219cycles, avoiding, 503–504cycles, detecting, 502–503cycles, isolating paths, 504–505isolating shortest paths in BOM, 535–537modifying data, 217–218multiple, 216multiple references, 216–217nested sets, creating relationships, 521–522nested sets, materializing relationships in

tables, 526nested sets, producing binary sort paths,

519–522overview, 214–215, 219–222, 472result column aliases, 215sorting, returning all employees sorted by

empname, 499–500

common table expression

Z02I623139.fm Page 571 Monday, March 6, 2006 2:15 PM

Page 4: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

572

common table expression (CTE), continuedsorting, returning all employees sorted by

salary, 500–501subgraph/subtree with path enumeration,

490–491subordinates, creating fn_partsexplosion

function, 476–477subordinates, creating fn_subordinates2

function, 480–481subordinates, creating fn_subordinates2

function with two levels, 482subordinates, limiting levels, 480subordinates, limiting levels using filters, 483subordinates, limiting levels using

MAXRECURSION, 482–483subordinates, parts explosion, 478subordinates, parts explosion with aggregate

parts, 479subordinates, testing fn_partsexplosion

function, 478subordinates, testing fn_subordinates2

function, 481compilation

aggregate binding with algebrizer, 39algebrizer overview, 37–38Assert operator in update plans, 59batch main steps, 35batch overview, 35binding, 36capturing showplans with SQL trace, 55–57compared to execution, 35cost-based query optimizer, 40–41, 42–43cost strategies in update plans, 60counters of optimizer event, 47creating clustered index in update plans, 62–63data manipulation language (DML), 37dynamic management view (DMV), 44execution plans overview, 35extracting showplans from procedure cache,

57–58

formats for showplans, 47–48graphical format for showplans, 51–53grouping binding with algebrizer, 39–40Halloween spool in update plans, 61, 63maintaining indexes in update plans, 59name resolution with algebrizer, 38–39operator flattening with algebrizer, 38optimization overview, 36, 40optimization phases, 42–43outer join simplifications, 41–42overview, 31, 35parsing, 36per-row and per-index update plans, 60–61performance in update plans, 60procedure cache using

sys.dm_exec_query_optimizer_info, 44query plans overview, 47run-time information in showplans overview,

53script for batch from

sys.dm_exec_query_optimizer_info, 44–47SET STATISTICS PROFILE in showplans,

54–55SET STATISTICS XML ON|OFF in showplans,

53–54SHOWPLAN_ALL, 50showplans overview, 47SHOWPLAN_TEXT, 48–50simplifications, 41–42stored procedure overview, 35text format for showplans, 48–50trivial plan optimization, 41type derivation with algebrizer, 39update plans overview, 59update plans stages, 59XML format for showplans, 50–51

connected graphs, 460container objects, 218–219

compilation

Z02I623139.fm Page 572 Monday, March 6, 2006 2:15 PM

Page 5: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

573

correlated subqueriesEXISTS, 199–207overview, 195tiebreaker, 196–199

correlating waits with queues, 80–81covering nonclustered index seek + ordered

partial scansindex access methods, 150–152index optimization scale, 162

cross join (Cartesian product)example, complex, 4example, simple, 3–4overview, 265performance, 5–7performing, 6–7

CTE. See common table expression (CTE)CUBE option. See also ROLLUP option

applying, 13attributes vs. dimensions, 374#Cube, 377–378GROUPING function, 378NULL placeholder described, 375NULL placeholder in the empid column, 378overview, 4, 374–375

cumulative aggregations, 323–328cursor-based solutions for row numbers,

234–235custom aggregations

aggregate bitwise AND operator, 363–364aggregate bitwise OR operator, 362–363aggregate bitwise XOR operator, 364aggregate product using pivoting, 346–347C# code for UDA, 348CLR code in databases, 347–348creating assemblies in Visual Studio 2005,

353–357enabling CLR and querying catalog views, 357implementing UDA, 348median, 364–367

overview, 344–345pivoting, 345specialized solutions for aggregate bitwise

operations, 360–362specialized solutions for aggregate product,

358–360specialized solutions for aggregate string

concatenation, 358specialized solutions overview, 358string concatenation using pivoting, 345–346testing UDA, 357–358UDA overview, 347Visual Basic .NET code for UDA, 351

custom sequencesasynchronous sequence generation, 433–434block of sequence values, 431–433overview, 429single sequence value, 430synchronous sequence generation overview,

429–430cycles

avoiding (CTE solution), 503–504detecting (CTE solution), 502–503isolating paths (CTE solution), 504–505overview, 502

DDAG. See directed acyclic graph (DAG)data manipulation language (DML) statements,

37data modifications

Assert operator in update plans, 59assignment SELECT statements, 450–452assignment UPDATE statements, 452–454asynchronous sequence generation, 433–434block of sequence values, 431–433common table expression (CTE), 217–218cost strategies in update plans, 60

data modifications

Z02I623139.fm Page 573 Monday, March 6, 2006 2:15 PM

Page 6: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

574

data modifications, continuedcreating clustered index in update plans, 62–63custom sequences overview, 429DELETE statements using joins, 438–441DELETE statements with OUTPUT clause,

441–443DELETE vs. TRUNCATE TABLE statements,

435deleting data overview, 435execution plan for update plans, 61–62globally unique identifier (GUID), 434–435Halloween spool in update plans, 61, 63identity columns in sequence mechanisms, 429INSERT EXEC statement, 417–423INSERT statement with OUTPUT clause,

426–428inserting data overview, 417inserting new rows, 423–426maintaining indexes, 59overview, 417per-row and per-index update plans, 60–61performance, 60, 454–457removing rows with duplicate data, 435–438SELECT INTO statement, 417–419SELECT statement assignments overview, 450sequence mechanisms overview, 428single sequence value, 430synchronous sequence generation, 429–433TRUNCATE TABLE vs. DELETE statements,

435update plans overview, 59update plans stages, 59UPDATE statement assignments overview, 450UPDATE statements using joins, 443–447UPDATE statements with OUTPUT clause,

447–449updating data overview, 443

Database Engine Tuning Advisor (DTA), 121

DELETE statements. See also deleting datacompared to TRUNCATE TABLE statement,

435cost strategies, 60Halloween spool, 61, 63joins, 438–441maintaining indexes, 59OUTPUT clause, 441–443per-row and per-index plans, 60–61performance, 60read and write stages, 59TOP queries, 385–388

deleting data. See also DELETE statementsoverview, 435performance considerations, 454–457removing rows with duplicate data, 435–438

dense rankDENSE_RANK function overview, 246overview, 246set-based solutions, 247

derived tablesarguments, 213multiple references, 214nesting, 213overview, 211–212result column aliases, 212–213

determinismROW_NUMBER function, 226–227TOP option, 383–384

digraph, 460dimensions vs. attributes, 374directed acyclic graph (DAG)

BOM scenario, 464–468compared to trees, 462creating script for fn_BOMTC (UDF solution),

533–534generating all paths in BOM, 534–535isolating shortest paths in BOM (CTE

solution), 535–537

Database Engine Tuning Advisor

Z02I623139.fm Page 574 Monday, March 6, 2006 2:15 PM

Page 7: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

575

overview, 460running code for transitive closure, 531–533topological sort, 491transitive closure overview, 531undirected graphs. See undirected graphs

directed graphs, 460DISTINCT clause

applying, 15overview, 4

DML (data manipulation language) statements, 37

DMV (dynamic management view), 44drilling down

analyzing trace data, 90–103database or file level, 82–84process level, 84–85trace performance workload, 85–89

DTA (Database Engine Tuning Advisor), 121dynamic management objects, 106dynamic management view (DMV), 44, 71

Eemployee organizational chart scenario, 462–464enumerated path

overview, 487–488subgraph/subtree, creating fn_subordinates3

function, 488subgraph/subtree (CTE solution), 490–491subgraph/subtree, hierarchical relationships,

490subgraph/subtree, testing fn_subordinates3

function, 489EXCEPT set operation

EXCEPT ALL set operation, 306–307EXCEPT DISTINCT set operation, 305overview, 305

execution plansanalysis overview, 107–108graphical showplans, 51–53, 108–115

textual showplans, 48–50, 116–117XML showplans, 50–51, 117–119

existing rangescol1 vs. row number, 262grouping factors, 260–261overview, 256–257row number vs. col1, 262row numbers based on col1 order, 261–262

EXISTS predicatecompared to IN predicate, 200–201example, 199–200minimum missing value, 203–206NOT EXISTS predicate vs. NOT IN predicate,

201–203overview, 199reverse logic applied to relational division,

206–207extents, 456

Fforcing a join strategy, 295forests, 461fragmentation, index, 168–169FROM clause

overview, 4performing Cartesian product (cross join), 6–7

Ggaps

next pairs, 259overview, 256–257points before, 258solutions, 257starting points, 258–259

globally unique identifier (GUID), 434–435graphical showplans, 51–53, 108–115

graphical showplans

Z02I623139.fm Page 575 Monday, March 6, 2006 2:15 PM

Page 8: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

576

graphsacyclic graphs overview, 460BOM scenario, 464–468connected, 460cycles. See cyclesdirected acyclic graph (DAG). See directed

acyclic graph (DAG)directed graphs overview, 460iteration. See iterationoverview, 459–460resource for formal definitions, 460road system scenario, 468–471transitive closure. See transitive closuretrees. See treesundirected graphs overview, 460

GROUP BY clause. See also grouping factorCUBE option. See CUBE optiongrouping, 12–13overview, 4ROLLUP option. See ROLLUP option

grouping binding, 39–40grouping factor. See also GROUP BY clause

calculating, 260–261creating and populating a Stocks table,

372–374difference between col1 and row number, 262overview, 260, 371row numbers based on col1 order, 261

GUID (globally unique identifier), 434–435

HHalloween spool, 61, 63hash joins, 294–295HAVING filter

applying, 13–14overview, 4

heaps, 123, 128–130

hierarchiesemployee organizational chart scenario,

462–464overview, 459–460, 461resource for formal definitions, 460

hints, 119–121histograms

altering implementation of fn_histsteps function, 371

generating steps, 368–369overview, 367–368returning with ten steps, 369–370returning with ten steps and empty steps, 370returning with three steps, 369testing, 369

history of SQL, 2–3horizontal vs. vertical operations, 303

IIDENTITY-based solution for row numbers,

235–237identity columns in sequence mechanisms, 429IN predicate

compared to EXISTS predicate, 200–201NOT IN vs. NOT EXISTS predicate, 201–203

index access methodsclustered index seek + ordered partial scans,

148–150covering nonclustered index seek + ordered

partial scans, 150–152index intersections, 152–153indexed views, 153–155nonclustered index seek + ordered partial scan

+ lookups, 140–144ordered clustered index scans, 136–137ordered covering nonclustered index scans,

137–140overview, 132table scans, 132–134

graphs

Z02I623139.fm Page 576 Monday, March 6, 2006 2:15 PM

Page 9: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

577

unordered clustered index scans, 132–134unordered covering nonclustered index scans,

134–136unordered nonclustered index scan + lookups,

144–148index fragmentations, 168–169index intersections, 152–153index optimization scale

analysis, 162–167clustered index seek + ordered partial scans,

161–162covering nonclustered index seek + ordered

partial scans, 162nonclustered index seek + ordered partial scan

+ lookups, 158–161overview, 155–156selectivity point, 159–161summary, 162–167table scans (unordered clustered index scans),

156unordered covering nonclustered index scans,

157unordered nonclustered index scan + lookups,

158index partitioning, 170index scans

clustered index seek + ordered partial scans, 148–150, 161–162

covering nonclustered index seek + ordered partial scans, 150–152, 162

nonclustered index seek + ordered partial scan + lookups, 140–144, 158–161

ordered clustered index scans, 136–137ordered covering nonclustered index scans,

137–140unordered clustered, 132–134unordered covering nonclustered, 157unordered covering nonclustered index scans,

134–136unordered nonclustered index scan + lookups,

144–148, 158

index structuresbalanced trees, 124–128clustered indexes, 124–128extents, 456heaps, 123nonclustered indexes on clustered tables,

130–131nonclustered indexes on heaps, 128–130overview, 122pages, 122–123

index tuning. See also query tuningbalanced trees, 124–128clustered index seek + ordered partial scans,

148–150, 161–162clustered indexes, 124–128covering nonclustered index seek + ordered

partial scans, 150–152, 162extents, 123fragmentation, 168–169heaps, 123index access methods overview, 132index intersections, 152–153index optimization scale analysis, 162–167index optimization scale overview, 155–156index optimization scale summary, 162–167index structures overview, 122indexed views, 153–155nonclustered index seek + ordered partial scan

+ lookups, 140–144, 158–161nonclustered indexes on clustered tables,

130–131nonclustered indexes on heaps, 128–130ordered clustered index scans, 136–137ordered covering nonclustered index scans,

137–140overview, 103–105, 122pages, 122–123partitioning, 170selectivity point, 159–161table scans, 132–134

index tuning

Z02I623139.fm Page 577 Monday, March 6, 2006 2:15 PM

Page 10: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

578

index tuning, continuedtable scans (unordered clustered index scans),

156table structures overview, 122unordered clustered index scans, 132–134unordered covering nonclustered index scans,

134–136, 157unordered nonclustered index scan + lookups,

144–148, 158indexed views, 153–155inner joins

example, 271–272overview, 270performance, 270

inner queries. See subqueriesinput expressions for TOP queries, 385INSERT EXEC statement, 417–423INSERT statements

Assert operator, 59cost strategies, 60Halloween spool, 61, 63maintaining indexes, 59OUTPUT clause, 426–428per-row and per-index plans, 60–61performance, 60read and write stages, 59TOP queries, 385–388

inserting dataasynchronous sequence generation, 433–434block of sequence values, 431–433custom sequences overview, 429globally unique identifier (GUID), 434–435identity columns in sequence mechanisms, 429INSERT EXEC statement, 417–423INSERT statements with OUTPUT clause,

426–428inserting new rows, 423–426overview, 417performance considerations, 454–457SELECT INTO statements, 417–419

sequence mechanisms overview, 428single sequence value, 430synchronous sequence generation, 429–433

inserting new rows, 423–426INTERSECT set operation

INTERSECT ALL set operation, 308–309INTERSECT DISTINCT set operation, 308

INTO clause, 310overview, 307–308introduction, 1islands

col1 vs. row number, 262grouping factors, 260–261overview, 256–257row number vs. col1, 262row numbers based on col1 order, 261–262

iterationadvantages of, 471ancestors, creating fn_managers function,

484–485ancestors, creating management chain (CTE

solution), 486ancestors, creating management chain with two

levels (CTE solution), 486–487ancestors function vs. subordinates function,

485ancestors, limiting levels (CTE solution), 487ancestors overview, 484ancestors, testing fn_managers function,

485–486compared to materialized paths, 505cycles, avoiding (CTE solution), 503–504cycles, detecting (CTE solution), 502–503cycles, isolating paths (CTE solution), 504–505cycles overview, 502enumerated path overview, 487–488overview, 471sorting, calculating integer sort values based on

sortpath order, 496sorting, constructing binary sort paths for each

employee, 495

indexed views

Z02I623139.fm Page 578 Monday, March 6, 2006 2:15 PM

Page 11: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

579

sorting, creating script for usp_sortsubs procedure, 492–495

sorting, generating identity values for employees in each level, 495

sorting, limiting levels with sort based on empname, 497

sorting overview, 491–492sorting, returning all employees sorted by

empname (CTE solution), 499–500sorting, returning all employees sorted by

salary (CTE solution), 500–501sorting, returning attributes other than

employee ID, 497–498sorting, returning employees sorted by salary,

498–499sorting, testing specifying empname, 496–497subgraph/subtree, creating fn_subordinates3

function, 488subgraph/subtree, hierarchical relationships,

490subgraph/subtree overview, 487subgraph/subtree, path enumeration (CTE

solution), 490–491subgraph/subtree, testing fn_subordinates3

function, 489subordinates, creating fn_partsexplosion

function (CTE solution), 476–477subordinates, creating fn_subordinates1

function (UDF solution), 472–474subordinates, creating fn_subordinates2

function (CTE solution), 480–481subordinates, creating fn_subordinates2

function with two levels (CTE solution), 482subordinates function vs. ancestors function,

485subordinates, getting other attributes (UDF

solution), 474–475subordinates, limiting levels (CTE solution),

480

subordinates, limiting levels using filters (CTE solution), 483

subordinates, limiting levels using MAXRECURSION (CTE solution), 482–483

subordinates overview, 472subordinates, parts explosion (CTE solution),

478subordinates, parts explosion with aggregate

parts (CTE solution), 479subordinates, subtree of given root (CTE

solution), 475–476subordinates, testing fn_partsexplosion

function (CTE solution), 478subordinates, testing fn_subordinates1

function (UDF solution), 474subordinates, testing fn_subordinates2

function (CTE solution), 481transitive closure. See transitive closure

Jjoins

algorithms overview, 291ANSI SQL, 264cross. See cross join (Cartesian product)DELETE statements, 438–441forcing a strategy, 295fundamental types, 264–265hash, 294–295inner. See inner joinsloop, 291–292merge, 292–293multiple, 279–285new style vs. old style, 263–264nonequijoins, 277–279nonsupported types, 276old style vs. new style, 263–264outer. See outer joinsoverview, 263

joins

Z02I623139.fm Page 579 Monday, March 6, 2006 2:15 PM

Page 12: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

580

joins, continuedself, 276–277semi joins, 285–287separating elements problem, 296–297separating elements solution output, 302–303separating elements solution step 1, 297–298separating elements solution step 2, 298–299separating elements solution step 3, 299–300separating elements solution step 4, 300–302sliding total of previous year exercise, 287–291UPDATE statements, 443–447

Llogic puzzles

Alternating Lamp States. See logic puzzles, Flipping Lamp Switches

Alternating Lamp States solution, 562Arithmetic Maximum Calculation, 554Arithmetic Maximum Calculation solution, 561Basic Arithmetic, 557Basic Arithmetic solution, 565Cards Facing Up, 555–557Cards Facing Up solution, 565Chocolate Bar, 552Chocolate Bar solution, 558Covering a Chessboard with Domino Tiles, 554Covering a Chessboard with Domino Tiles

solution, 561Cutting a Stick to Make a Triangle, 555Cutting a Stick to Make a Triangle solution, 562On the Dot, 553On the Dot solution, 559Find the Pattern in the Sequence, 558Find the Pattern in the Sequence solution, 567Flipping Lamp Switches, 555Flipping Lamp Switches solution. See logic

puzzles, Alternating Lamp States solutionHiking a Mountain, 557Hiking a Mountain solution, 566

Measuring Time by Burning Ropes, 553Measuring Time by Burning Ropes solution,

561Medication Tablets, 551–552Medication Tablets solution, 558The Missing Buck, 555The Missing Buck solution, 562Monty Hall Problem, 556Monty Hall Problem solution, 563–565overview, 551Piece of Cake, 556Piece of Cake solution, 565Rectangle within a Circle, 555Rectangle within a Circle solution, 563Rectangles in a Square, 553Rectangles in a Square solution, 559–561Self-Replicating Code (Quine), 557Self-Replicating Code (Quine) solution, 566To a T, 552–553To a T solution, 558–559

logical query processingadding outer rows, 10APPLY table operator overview, 20–22applying CUBE option, 13applying DISTINCT clause, 15applying HAVING filter, 13–14applying ON filter (join condition), 8–10applying ORDER BY clause, 15–17applying ROLLUP option, 13applying TOP option, 18–19applying WHERE filter, 11–12FROM clause overview, 4CUBE option overview, 4DISTINCT clause overview, 4ON filter overview, 4GROUP BY clause overview, 4grouping, 12–13HAVING filter overview, 4new phases in SQL Server 2005, 19ORDER BY clause overview, 4

logic puzzles

Z02I623139.fm Page 580 Monday, March 6, 2006 2:15 PM

Page 13: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

581

OUTER keyword overview, 4OVER clause overview, 27–29overview, 3–4performing Cartesian product (cross join), 6–7PIVOT table operator overview, 22–24processing SELECT list, 14–15ROLLUP option overview, 4sample query, 4–6SELECT list overview, 4set operations overview, 29–30steps described, 3–4table operators overview, 19–20TOP option overview, 4UNPIVOT table operator overview, 24–27WHERE filter overview, 4

loop joins, 291–292

Mmaintaining data

adding employees who manage no one (leaves), 506–508

moving subtrees, 508–511overview, 506removing subtrees, 511–512

matching current and previous occurrencesTOP option and APPLY table operator solution

1, 398–399TOP option and APPLY table operator solution

2, 399–400TOP option and APPLY table operator solution

3, 400–401TOP option and APPLY table operator solution

4, 401–402TOP option and APPLY table operator solution

overview, 397–398materialized paths

adding employees who manage no one (leaves), 506–508

compared to iteration/recursion, 505creating and populating auxiliary table of

numbers, 516

creating script for fn_splitpath function, 516excluding root of subtrees, 513joining tables, 517limiting levels when returning subtrees with

given root, 514maintaining data overview, 506moving subtrees, 508–511overview, 504–506performance, subtrees vs. ancestors, 515querying overview, 512removing subtrees, 511–512returning leaf nodes under given root, 514returning management chain of given node,

515returning nodes exactly n levels under given

root, 514–515returning subtrees with given root, 513testing fn_splitpath function, 517

measuring run time of queries, 106–107median value

custom aggregations solutions, 364–367TOP option and APPLY table operator

solutions, 413–415merge joins, 292–293methodology for query tuning

analyzing trace data, 90–103analyzing waits at instance level, 71–79correlating waits with queues, 80–81determining course of action, 81drilling down to database or file level, 82–84drilling down to process level, 84–85overview, 69–71trace performance workload, 85–89

misbehaving subqueries, 208–209missing ranges

next pairs, 259overview, 256–257points before, 258solutions, 257starting points, 258

missing value for EXISTS predicate, 203–206

missing value for EXISTS predicate

Z02I623139.fm Page 581 Monday, March 6, 2006 2:15 PM

Page 14: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

582

modifying dataAssert operator in update plans, 59assignment SELECT statements, 450–452assignment UPDATE statements, 452–454asynchronous sequence generation, 433–434block of sequence values, 431–433common table expression (CTE), 217–218cost strategies in update plans, 60creating clustered index in update plans, 62–63custom sequences overview, 429DELETE statements using joins, 438–441DELETE statements with OUTPUT clause,

441–443DELETE vs. TRUNCATE TABLE statements,

435deleting data overview, 435globally unique identifier (GUID), 434–435Halloween spool in update plans, 61, 63identity columns in sequence mechanisms, 429INSERT EXEC statement, 417–423INSERT statement with OUTPUT clause,

426–428inserting data overview, 417inserting new rows, 423–426maintaining indexes, 59overview, 417per-row and per-index update plans, 60–61performance, 60, 454–457removing rows with duplicate data, 435–438SELECT INTO statement, 417–419SELECT statement assignments overview, 450sequence mechanisms overview, 428single sequence value, 430synchronous sequence generation, 429–433TRUNCATE TABLE vs. DELETE statements,

435update plans overview, 59update plans stages, 59UPDATE statement assignments overview, 450UPDATE statements using joins, 443–447

UPDATE statements with OUTPUT clause, 447–449

updating data overview, 443multipage access for row numbers, 244–245multiple common table expression (CTE), 216multiple joins

controlling logical join evaluation order, 282–285

controlling physical join evaluation order, 279–282

overview, 279multiple references

common table expression (CTE), 216–217derived tables, 214

Nn rows for each group

TOP option and APPLY table operator solution 1, 392–394

TOP option and APPLY table operator solution 2, 394

TOP option and APPLY table operator solution 3, 395

TOP option and APPLY table operator solution 4, 395–396

TOP option and APPLY table operator solution 5, 396–397

TOP option and APPLY table operator solution overview, 391–392

name resolution, 38–39National Institute of Standards and Technology

(NIST), 460nested derived tables, 213nested sets

assigning left and right values overview, 518creating relationships (CTE solution), 521–522creating script for fn_empsnestedsets function

(UDF solution), 523illustration of model, 518–519

modifying data

Z02I623139.fm Page 582 Monday, March 6, 2006 2:15 PM

Page 15: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

583

limiting levels when returning subtrees with given root, 528

materializing relationships in tables (CTE or UDF solution), 526

overview, 517–518producing binary sort paths (CTE solution),

519–522querying overview, 527returning all ancestors of given node, 530returning count of subordinates of given node,

529–530returning leaf nodes under given root, 529returning subtrees of given root, 527–528testing script for fn_empsnestedsets function

(UDF solution), 525NEWID function, 434–435NIST (National Institute of Standards and

Technology), 460nonclustered indexes

clustered tables, 130–131heaps, 128–130seek + ordered partial scan + lookups, 140–144,

158–161nonequijoins, 277–279nonpartitioned IDENTITY-based solution for

row numbers, 236nonsupported join types, 276nonunique sort column

with tiebreaker, 229–230without tiebreaker, 230–233

NOT EXISTS predicate vs. NOT IN predicate, 201–203

NTILE functionoverview, 247set-based solutions, 249–252SQL Server 2005, 247–249

NULL placeholderempid column, 378

placeholder for CUBE and ROLLUP options, 375

Nums table. See auxiliary table of numbers

OON filter

applying, 8–10overview, 4

operator flattening, 38optimization

cost-based, 40–41, 42–43counters of optimizer event, 47data manipulation language (DML), 37dynamic management view (DMV), 44outer join simplifications, 41–42overview, 36, 40phases, 42–43procedure cache using

sys.dm_exec_query_optimizer_info, 44script for batch from

sys.dm_exec_query_optimizer_info, 44–47simplifications, 41–42trivial plan, 41

optimizer. See query optimizerOR operator

aggregate bitwise, 362–363flattening with algebrizer, 38TOP option and APPLY table operator

solutions, 405–408ORDER BY clause

applying, 15–17overview, 4TOP option, 383–384

ordered clustered index scans, 136–137ordered covering nonclustered index scans,

137–140organization of this book, xxiii

organization of this book

Z02I623139.fm Page 583 Monday, March 6, 2006 2:15 PM

Page 16: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

584

outer joinsexample, 272–276overview, 4, 272simplifications, 41–42

outer rows, adding, 10OUTPUT clause

DELETE statements, 441–443UPDATE statements, 447–449

OVER clausecalculating aggregates, 315–319logical query processing phases, 27–28ORDER BY phase, 28overview, 27, 315–316SELECT phase, 28

overview, 1

Ppages, 122–123paging

ad-hoc paging for row numbers, 243–244logical transformations, 405–408multipage access for row numbers, 244–245AND operator, 405–408OR operator, 405–408overview for row numbers, 243TOP option and APPLY table operator first page

solution, 403–404TOP option and APPLY table operator next

page solution, 404–409TOP option and APPLY table operator previous

page solution, 409–411TOP option and APPLY table operator solution

overview, 402–403parse tree, 37parsing, 36partitioning

IDENTITY-based solution for row numbers, 236–237

index, 170

ROW_NUMBER function, 227set-based technique for row numbers, 233–234

path enumerationoverview, 487–488subgraph/subtree, creating fn_subordinates3

function, 488subgraph/subtree (CTE solution), 490–491subgraph/subtree, hierarchical relationships,

490subgraph/subtree, testing fn_subordinates3

function, 489paths, materialized

adding employees who manage no one (leaves), 506–508

compared to iteration/recursion, 505creating and populating auxiliary table of

numbers, 516creating script for fn_splitpath function, 516excluding root of subtrees, 513joining tables, 517limiting levels when returning subtrees with

given root, 514maintaining data overview, 506moving subtrees, 508–511overview, 504–506performance, subtrees vs. ancestors, 515querying overview, 512removing subtrees, 511–512returning leaf nodes under given root, 514returning management chain of given node,

515returning nodes exactly n levels under given

root, 514–515returning subtrees with given root, 513testing fn_splitpath function, 517

performanceancestors vs. subtrees, 515cross joins, 5–7data modifications, 60, 454–457DELETE statements, 60

outer joins

Z02I623139.fm Page 584 Monday, March 6, 2006 2:15 PM

Page 17: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

585

index tuning. See index tuninginner joins, 270INSERT statements, 60query tuning. See query tuningrow number calculation techniques, 237–242subtrees vs. ancestors, 515update plans, 60

phases, logical query processingadding outer rows, 10APPLY table operator overview, 20–22applying CUBE option, 13applying DISTINCT clause, 15applying HAVING filter, 13–14applying ON filter (join condition), 8–10applying ORDER BY clause, 15–17applying ROLLUP option, 13applying TOP option, 18–19applying WHERE filter, 11–12FROM clause overview, 4CUBE option overview, 4DISTINCT clause overview, 4ON filter overview, 4GROUP BY overview, 4grouping, 12–13HAVING filter overview, 4new phases in SQL Server 2005, 19ORDER BY clause overview, 4OUTER keyword overview, 4OVER clause overview, 27–29overview, 3–4performing Cartesian product (cross join), 6–7PIVOT table operator overview, 22–24processing SELECT list, 14–15ROLLUP option overview, 4sample query, 4–6SELECT overview, 4set operations overview, 29–30steps described, 3–4TOP option overview, 4

UNPIVOT table operator overview, 24–27WHERE filter overview, 4

physical query processingalgebrizer. See algebrizercompilation. See compilationflow of data, 32–35optimization. See optimizationoverview, 31query compilation, 63query execution, 63query optimizer. See query optimizerquery plans. See query plansupdate plans. See update plans

PIVOT, 22–24pivoting. See also aggregations

aggregate product, 346–347aggregating data, 337–341attributes, 331–335compared to unpivoting, 341–343custom aggregations, 345histograms. See histogramsoverview, 315, 331relational division, 331–337string concatenation, 345–346

precedence of set operations, 309–310Profiler, 55, 121pronunciation of SQL, 2puzzles

Alternating Lamp States. See puzzles, Flipping Lamp Switches

Alternating Lamp States solution, 562Arithmetic Maximum Calculation, 554Arithmetic Maximum Calculation solution, 561Basic Arithmetic, 557Basic Arithmetic solution, 565Cards Facing Up, 555–557Cards Facing Up solution, 565Chocolate Bar, 552Chocolate Bar solution, 558

puzzles

Z02I623139.fm Page 585 Monday, March 6, 2006 2:15 PM

Page 18: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

586

puzzles, continuedCovering a Chessboard with Domino Tiles, 554Covering a Chessboard with Domino Tiles

solution, 561Cutting a Stick to Make a Triangle, 555Cutting a Stick to Make a Triangle solution, 562On the Dot, 553On the Dot solution, 559Find the Pattern in the Sequence, 558Find the Pattern in the Sequence solution, 567Flipping Lamp Switches, 555Flipping Lamp Switches solution. See puzzles,

Alternating Lamp States solutionHiking a Mountain, 557Hiking a Mountain solution, 566Measuring Time by Burning Ropes, 553Measuring Time by Burning Ropes solution,

561Medication Tablets, 551–552Medication Tablets solution, 558The Missing Buck, 555The Missing Buck solution, 562Monty Hall Problem, 556Monty Hall Problem solution, 563–565overview, 551Piece of Cake, 556Piece of Cake solution, 565Rectangle within a Circle, 555Rectangle within a Circle solution, 563Rectangles in a Square, 553Rectangles in a Square solution, 559–561Self-Replicating Code (Quine), 557Self-Replicating Code (Quine) solution, 566To a T, 552–553To a T solution, 558–559

Qquery compilation, 63. See also compilationquery execution, 63

query optimizercost-based, 40–41, 42–43counters of optimizer event, 47dynamic management view (DMV), 44outer join simplifications, 41–42overview, 3, 40phases, 42–43procedure cache using

sys.dm_exec_query_optimizer_info, 44script for batch from

sys.dm_exec_query_optimizer_info, 44–47simplifications, 41–42trivial plan, 41

query plans, 31capturing showplans with SQL trace, 55–57extracting showplans from procedure cache,

57–58formats for showplans, 47–48graphical format for showplans, 51–53overview, 47run-time information in showplans overview,

53SET STATISTICS PROFILE in showplans,

54–55SET STATISTICS XML ON|OFF in showplans,

53–54SHOWPLAN_ALL, 50showplans overview, 47SHOWPLAN_TEXT, 48–50summary, 63text format for showplans, 48–50XML format for showplans, 50–51

query processinglogical. See logical query processingphysical. See physical query processing

query processor tree, 37query tuning. See also index tuning

additional resources, 187–189analyzing trace data, 90–103analyzing waits at instance level, 71–79

query compilation

Z02I623139.fm Page 586 Monday, March 6, 2006 2:15 PM

Page 19: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

587

correlating waits with queues, 80–81determining course of action, 81drilling down to database or file level, 82–84drilling down to process level, 84–85exercise based on code revisions, 181–182exercise using cursor-based solution, 182–183exercise using set-based solution, 183–187methodology overview, 69–71overview, 65sample data, 66–69sample data preparation for BigSessions table,

173–177sample data preparation for Sessions table,

171–173sample data preparation overview, 170–171sample data using TABLESAMPLE clause,

177–180set-based vs. iterative or procedural

approaches, 180–181tools. See tools for query tuningtrace performance workload, 85–89

querying, materialized pathscreating and populating auxiliary table of

numbers, 516creating script for fn_splitpath function, 516excluding root of subtrees, 513joining tables, 517limiting levels when returning subtrees with

given root, 514overview, 512performance, subtrees vs. ancestors, 515returning leaf nodes under given root, 514returning management chain of given node,

515returning nodes exactly n levels under given

root, 514–515returning subtrees with given root, 513testing fn_splitpath function, 517

querying, nested setslimiting levels when returning subtrees with

given root, 528overview, 527returning all ancestors of given node, 530returning count of subordinates of given node,

529–530returning leaf nodes under given root, 529returning subtrees of given root, 527–528

Rrandom rows with TOP option and APPLY table

operator, 411–412rank

overview, 246RANK function overview, 246set-based solutions, 247

recursionadvantages of iterative solutions, 471ancestors, creating fn_managers function,

484–485ancestors, creating management chain (CTE

solution), 486ancestors function vs. subordinates function,

485ancestors, creating management chain with two levels (CTE solution), 486–487

ancestors, limiting levels (CTE solution), 487ancestors overview, 484ancestors, testing fn_managers function,

485–486compared to materialized paths, 505cycles, avoiding (CTE solution), 503–504cycles, detecting (CTE solution), 502–503cycles, isolating paths (CTE solution), 504–505cycles overview, 502enumerated path overview, 487–488overview, 471

recursion

Z02I623139.fm Page 587 Monday, March 6, 2006 2:15 PM

Page 20: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

588

recursion, continuedsorting, calculating integer sort values based on

sortpath order, 496sorting, constructing binary sort paths for each

employee, 495sorting, creating script for usp_sortsubs

procedure, 492–495sorting, generating identity values for

employees in each level, 495sorting, limiting levels with sort based on

empname, 497sorting overview, 491–492sorting, returning all employees sorted by

empname (CTE solution), 499–500sorting, returning all employees sorted by

salary (CTE solution), 500–501sorting, returning attributes other than

employee ID, 497–498sorting, returning employees sorted by salary,

498–499sorting, testing specifying empname, 496–497subgraph/subtree, creating fn_subordinates3

function, 488subgraph/subtree, hierarchical relationships,

490subgraph/subtree overview, 487subgraph/subtree, path enumeration (CTE

solution), 490–491subgraph/subtree, testing fn_subordinates3

function, 489subordinates, creating fn_partsexplosion

function (CTE solution), 476–477subordinates, creating fn_subordinates1

function (UDF solution), 472–474subordinates, creating fn_subordinates2

function (CTE solution), 480–481subordinates, creating fn_subordinates2

function with two levels (CTE solution), 482subordinates function vs. ancestors function,

485

subordinates, getting other attributes (UDF solution), 474–475

subordinates, limiting levels (CTE solution), 480

subordinates, limiting levels using filters (CTE solution), 483

subordinates, limiting levels using MAXRECURSION (CTE solution), 482–483

subordinates overview, 472subordinates, parts explosion (CTE solution),

478subordinates, parts explosion with aggregate

parts (CTE solution), 479subordinates, subtree of a given root (CTE

solution), 475–476subordinates, testing fn_partsexplosion

function (CTE solution), 478subordinates, testing fn_subordinates1

function (UDF solution), 474subordinates, testing fn_subordinates2

function (CTE solution), 481transitive closure. See transitive closure

recursive common table expression. See common table expression (CTE)

relational divisionexample, 192–195overview, 192pivoting, 331–337reverse logic, 206–207

removing rows with duplicate data, 435–438result column aliases

common table expression (CTE), 215derived tables, 212–213

road system scenario, 468–471ROLLUP option. See also CUBE option

applying, 13example, 379–380NULL placeholder described, 375overview, 4, 374, 379

rooted trees, 461

recursive common table expression

Z02I623139.fm Page 588 Monday, March 6, 2006 2:15 PM

Page 21: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

589

row numbersad-hoc paging, 243–244cursor-based solution, 234–235IDENTITY-based solution, 235–237multipage access, 244–245nonunique sort column with tiebreaker,

229–230nonunique sort column without tiebreaker,

230–233overview, 224paging overview, 243performance comparisons for calculation

techniques, 237–242ROW_NUMBER function determinism,

226–227ROW_NUMBER function overview, 224–226ROW_NUMBER function partitioning, 227set-based technique overview, 227set-based technique partitioning, 233–234unique sort column for set-based technique,

228–229row value constructors, 444ROW_NUMBER function. See also row numbers

determinism, 226–227overview, 224–226partitioning, 227

run-time information in showplansoverview, 53SET STATISTICS PROFILE, 54–55SET STATISTICS XML ON|OFF, 53–54

running aggregationscumulative aggregations, 323–328overview, 321–323sliding aggregations, 328–329Year-To-Date (YTD), 330–331

Ssample databases, installing, xxivscalar subqueries

example, 192overview, 192

SELECT INTO statement, 417–419SELECT list processing, 4, 14–15SELECT statements, 450–452SELECT TOP option

basic example, 382DELETE statements, 385–388determinism, 383–384input expressions, 385INSERT statements, 385–388modifications, 385–388ORDER BY clause, 383–384overview, 381–382PERCENT option example, 382–383WITH TIES option, 384UPDATE statements, 385–388

selectivity point, 159–161self-contained subqueries

overview, 192relational division example, 192–195relational division overview, 192scalar subqueries example, 192scalar subqueries overview, 192

self joins, 276–277semi joins, 285–287separating elements

problem, 296–297solution output, 302–303solution step 1, 297–298solution step 2, 298–299solution step 3, 299–300solution step 4, 300–302

separating elements

Z02I623139.fm Page 589 Monday, March 6, 2006 2:15 PM

Page 22: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

590

sequence mechanismsasynchronous sequence generation, 433–434block of sequence values, 431–433custom sequences overview, 429globally unique identifier (GUID), 434–435identity columns, 429overview, 428single sequence value, 430synchronous sequence generation, 429–433

set-based solutionsdense rank, 247NTILE function, 249–252rank, 247

set-based technique for row numbersnonunique sort column with tiebreaker,

229–230nonunique sort column without tiebreaker,

230–233overview, 227partitioning, 233–234unique sort column, 228–229

set operationsINTO, 310EXCEPT, 305EXCEPT ALL, 306–307EXCEPT DISTINCT, 305horizontal vs. vertical, 303INTERSECT, 307–308INTERSECT ALL, 308–309INTERSECT DISTINCT, 308overview, 29–30, 263, 303–304precedence, 309–310UNION, 304UNION ALL, 304UNION DISTINCT, 304unsupported logical phases, 310–313vertical vs. horizontal, 303

SET ROW COUNT option, 385SHOWPLAN_ALL, 50

showplansanalyzing textual, 116–117analyzing XML, 117–119capturing with SQL trace, 55–57extracting from procedure cache, 57–58formats, 47–48graphical format, 51–53, 108–115overview, 47run-time information overview, 53SET STATISTICS PROFILE, 54–55SET STATISTICS XML ON|OFF, 53–54SHOWPLAN_ALL, 50SHOWPLAN_TEXT, 48–50text format, 48–50, 116–117XML format, 50–51, 117–119

SHOWPLAN_TEXT, 48–50simplifications, 41–42single sequence value, 430sliding aggregations, 328–329sorting

calculating integer sort values based on sortpath order, 496

constructing binary sort paths for each employee, 495

creating script for usp_sortsubs procedure, 492–495

generating identity values for employees in each level, 495

limiting levels with sort based on empname, 497

overview, 491–492returning all employees sorted by empname

(CTE solution), 499–500returning all employees sorted by salary (CTE

solution), 500–501returning attributes other than employee ID,

497–498returning employees sorted by salary, 498–499testing using empname, 496–497

sequence mechanisms

Z02I623139.fm Page 590 Monday, March 6, 2006 2:15 PM

Page 23: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

591

specialized solutions for custom aggregationsaggregate bitwise AND, 363–364aggregate bitwise operations, 360–362aggregate bitwise OR, 362–363aggregate bitwise XOR, 364aggregate product, 358–360aggregate string concatenation, 358median value, 364–367overview, 358

SQL Server Management Studio (SSMS), 32SQL Server Profiler, 55, 121SQL vs. T-SQL, 1SSMS (SQL Server Management Studio), 32STATISTICS IO session option, 106subgraph/subtree

creating fn_subordinates3 function, 488hierarchical relationships, 490overview, 487path enumeration (CTE solution), 490–491testing fn_subordinates3 function, 489

subordinatescompared to ancestors function, 485creating fn_partsexplosion function (CTE

solution), 476–477creating fn_subordinates1 function (UDF

solution), 472–474creating fn_subordinates2 function (CTE

solution), 480–481creating fn_subordinates2 function with two

levels (CTE solution), 482getting other attributes (UDF solution),

474–475limiting levels (CTE solution), 480limiting levels using filters (CTE solution), 483limiting levels using MAXRECURSION (CTE

solution), 482–483overview, 472parts explosion (CTE solution), 478

parts explosion with aggregate parts (CTE solution), 479

subtree of given root (CTE solution), 475–476testing fn_partsexplosion function (CTE

solution), 478testing fn_subordinates1 function (UDF

solution), 474testing fn_subordinates2 function (CTE

solution), 481subqueries

correlated subqueries EXISTS, 199–207correlated subqueries overview, 195correlated subqueries tiebreaker, 196–199misbehaving, 208–209overview, 191self-contained, 192–195table expressions. See table expressionsuncommon predicates, 209–211

support for this book, xxiv–xxvsynchronous sequence generation

block of sequence values, 431–433overview, 429–430single sequence value, 430

syscacheobjects, 105system requirements, xxiii

TT-SQL vs. SQL, 1table expressions

arguments in CTE, 215arguments in derived tables, 213container objects, 218–219CTE overview, 214–215derived tables overview, 211–212modifying data in CTE, 217–218multiple CTE, 216multiple references in CTE, 216–217multiple references in derived tables, 214

table expressions

Z02I623139.fm Page 591 Monday, March 6, 2006 2:15 PM

Page 24: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

592

table expressions, continuednesting in derived tables, 213overview, 211recursive CTE, 219–222result column aliases in CTE, 215result column aliases in derived tables,

212–213table operators, 19–20table scans, 132–134, 156table structures

balanced trees, 124–128clustered indexes, 124–128extents, 123heaps, 123nonclustered indexes on clustered tables,

130–131nonclustered indexes on heaps, 128–130overview, 122pages, 122–123

TABLESAMPLE clause, 177–180terminology

acyclic graphs, 460connected graphs, 460directed acyclic graph (DAG), 460directed graphs, 460forests, 461graphs, 460hierarchies, 461National Institute of Standards and Technology

(NIST), 460resource for formal definitions, 460rooted trees, 461trees, 461undirected graphs, 460

textual showplans, 48–50, 116–117tiebreakers

aggregations, 319–321APPLY table operator. See APPLY table operatorcorrelated subqueries, 196–199

nonunique sort column for row numbers, 229–230

TOP option. See TOP optiontools for query tuning

analyzing execution plans overview, 107–108clearing cache, 105Database Engine Tuning Advisor (DTA), 121dynamic management objects, 106graphical execution plans, 108–115hints, 119–121measuring run time of queries, 106–107overview, 105Profiler, 121STATISTICS IO session option, 106syscacheobjects, 105textual showplans, 116–117tracing, 121XML showplans, 117–119

TOP optionapplying, 18–19DELETE statements, 385–388determinism, 383–384first page solution, 403–404input expressions, 385INSERT statements, 385–388matching current and previous occurrences

solution 1, 398–399matching current and previous occurrences

solution 2, 399–400matching current and previous occurrences

solution 3, 400–401matching current and previous occurrences

solution 4, 401–402matching current and previous occurrences

solution overview, 397–398median value solution, 413–415modifications, 385n rows for each group solution 1, 392–394n rows for each group solution 2, 394n rows for each group solution 3, 395

table operators

Z02I623139.fm Page 592 Monday, March 6, 2006 2:15 PM

Page 25: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

593

n rows for each group solution 4, 395–396n rows for each group solution 5, 396–397n rows for each group solution overview,

391–392next page solution, 404–409OR operator, 405–408ORDER BY clause, 383–384overview, 4, 381paging solution overview, 402–403previous page solution, 409–411random rows solution, 411–412SELECT TOP option basic example, 382SELECT TOP option overview, 381–382SELECT TOP PERCENT option example,

382–383SET ROW COUNT option, 385WITH TIES option, 384UPDATE statements, 385–388

trace performance workload, 85–89tracing, 121transitive closure

creating script for fn_BOMTC (UDF solution), 533–534

creating script for fn_RoadsTC (UDF solution), 539, 545–546

generating all paths in BOM, 534–535generating closure of roads, 538isolating shortest paths in BOM (CTE

solution), 535–537loading shortest road paths into tables,

545–546overview, 530–531returning all paths and distances in roads, 541returning shortest paths in roads, 541–545running code for BOM (DAG), 531–533undirected cyclic graphs overview, 537

treescompared to DAG, 462employee organizational chart scenario,

462–464

forests, 461graphs. See graphsiteration. See iterationnested sets. See nested setsoverview, 459–460, 461recursion. See recursionresource for formal definitions, 460rooted trees, 461

trivial plan optimization, 41TRUNCATE TABLE vs. DELETE statement, 435tuning

index. See index tuningquery. See query tuning

type derivation, 39

UUDA. See user-defined aggregates (UDA)uncommon predicates, 209–211undirected graphs

creating script for fn_RoadsTC (UDF solution), 539, 546–548

generating transitive closure of roads, 538loading shortest road paths into tables,

545–546overview, 460, 537returning all paths and distances in roads, 541returning shortest paths in roads, 541–545road system scenario, 468–471

UNION operatorflattening with algebrizer, 38overview, 304UNION ALL set operator, 304UNION DISTINCT set operator, 304

unique sort column, 228–229unordered clustered index scans, 132–134unordered covering nonclustered index scans,

134–136, 157unordered nonclustered index scan + lookups,

144–148, 158

unordered nonclustered index scan + lookups

Z02I623139.fm Page 593 Monday, March 6, 2006 2:15 PM

Page 26: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

594

UNPIVOT table operator, 24–27unpivoting vs. pivoting, 341–343update plans

Assert operator, 59cost strategies, 60creating clustered index, 62–63execution plan for update plans, 61–62Halloween spool, 61, 63maintaining indexes, 59overview, 59per-row and per-index, 60–61performance, 60stages, 59

UPDATE statementsAssert operator, 59assignment UPDATE statements, 452–454assignments overview, 450cost strategies, 60creating clustered index, 62–63execution plan, 61–62Halloween spool, 61, 63joins, 443–447maintaining indexes, 59OUTPUT clause, 447–449per-row and per-index plans, 60–61performance, 60read and write stages, 59TOP option, 385–388

updates for SQL Server, xxivupdating data

assignment SELECT statements, 450–452assignment UPDATE statements, 452–454overview, 443performance considerations, 454–457SELECT statement assignments overview, 450UPDATE statement assignments overview, 450UPDATE statements using joins, 443–447UPDATE statements with OUTPUT clause,

447–449

user-defined aggregates (UDA)C# code, 348CLR code in databases, 347–348creating assemblies in Visual Studio 2005,

353–357enabling CLR and querying catalog views, 357implementing, 348overview, 347testing, 357–358Visual Basic .NET code for UDA, 351

user-defined function (UDF)ancestors, creating fn_managers function,

484–485ancestors, testing fn_managers function,

485–486nested sets, creating script for

fn_empsnestedsets function, 523nested sets, materializing relationships in

tables, 526nested sets, testing script for

fn_empsnestedsets function, 525overview, 472subordinates, creating fn_subordinates1

function, 472–474subordinates, getting other subordinate

attributes, 474–475subordinates, testing fn_subordinates1

function, 474transitive closure, creating script for

fn_BOMTC, 533–534transitive closure, creating script for

fn_RoadsTC, 539

V-Wvertical vs. horizontal operations, 303views, indexed, 153–155wait times

analyzing at instance level, 71–79correlating with queues, 80–81

UNPIVOT table operator

Z02I623139.fm Page 594 Monday, March 6, 2006 2:15 PM

Page 27: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

595

WHERE filterapplying, 11–12overview, 4

WITH TIES option, 384

X-YXML showplans, 50–51, 117–119XOR operator, 364Year-To-Date (YTD) aggregations, 330–331

Year-To-Date aggregations

Z02I623139.fm Page 595 Monday, March 6, 2006 2:15 PM

Page 28: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

Z02I623139.fm Page 596 Monday, March 6, 2006 2:15 PM

Page 29: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

Z02I623139.fm Page 597 Monday, March 6, 2006 2:15 PM

Page 30: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

Z02I623139.fm Page 598 Monday, March 6, 2006 2:15 PM

Page 31: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

Z02I623139.fm Page 599 Monday, March 6, 2006 2:15 PM

Page 32: Z02I623139.fm Page 569 Monday, March 6, 2006 2:15 PM ... · CUBE option. See CUBE option cumulative, 323–328 custom aggregations overview, 344–345 custom aggregations using pivoting,

Z02I623139.fm Page 600 Monday, March 6, 2006 2:15 PM