1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu SQL Server MVP Deep Dives- P12 pptx

40 371 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề My Favorite DMVs, And Why
Trường học University of Information Technology
Chuyên ngành Database Management Systems
Thể loại Bài viết
Thành phố Ho Chi Minh City
Định dạng
Số trang 40
Dung lượng 805,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

DMV categories in SQL Server GO SELECT * FROM AB_Utility.dbo.AB_GetMissingIndexes 'Org00010001', 50 ; DMV categories in SQL Server Table 1 lists the DMV categories in both SQL Server 2

Trang 1

SELECT 'These procedures have not been executed in the past ' + RTRIM(uptime) + ' minutes (the last time SQL started)', sqlserver_start_time

FROM AB_Utility.dbo.AB_Uptime();

SELECT [name] = AB_Utility.dbo.AB_GetThreePartName (p.[object_id], DB_ID()),

p.create_date, p.modify_date FROM

sys.procedures AS p LEFT OUTER JOIN

sys.dm_exec_procedure_stats AS ps ON

p.[object_id] = ps.[object_id]

WHERE ps.[object_id] IS NULL ORDER BY

p.[Name];

END GO EXEC dbo.sp_MS_marksystemobject N'dbo.sp_AB_GetUnusedProcedures';

GO USE [your_database];

GO EXEC dbo.sp_AB_GetUnusedProcedures;

GO

WARNING Although creating objects in the master database has been a relatively

safe and well-known method for years, please proceed with the standing that you may need to change it later It is not documented,not supported, and likely to cease working in some future version ofSQL Server The main problem with using undocumented methods isthat Microsoft does not need to warn you before they change orremove the functionality; therefore, your next upgrade might end upbeing a lot more work than you thought

under-Finding inefficient and unused indexes

The following query will help you identify indexes in your database that are not used

at all, or are used more during maintenance operations than for improving query formance As with the query to find unused procedures, what you do with this infor-mation will rely heavily on how long SQL Server has been up and running If yourestarted SQL Server this morning, then these statistics may not yet represent an ade-quate sample of your workload And as with the unused procedure code, you will need

per-to create this object in each relevant database, because it returns metadata from thelocal catalog view sys.indexes (If you want to use the system object technique, thechanges to the code are similarly simple.) This code is shown in listing 7

Trang 2

BEGIN SET NOCOUNT ON;

SELECT 'These indexes have collected statistics for the past ' + RTRIM(uptime) + ' minutes (the last time SQL started)', sqlserver_start_time

FROM AB_Utility.dbo.AB_Uptime();

WITH calced AS (

SELECT [object_id], index_id, reads = user_seeks + user_scans + user_lookups, writes = user_updates,

perc = CONVERT(DECIMAL(10,2), user_updates * 100.0 / (user_seeks + user_scans + user_lookups + user_updates)) FROM

sys.dm_db_index_usage_stats WHERE

database_id = DB_ID() )

SELECT [status] = CASE WHEN reads = 0 AND writes = 0 THEN 'Consider dropping : not used at all' WHEN reads = 0 AND writes > 0 THEN 'Consider dropping : only writes' WHEN writes > reads THEN

'Consider dropping : more writes (' + RTRIM(perc) + '% of activity)' WHEN reads = writes THEN

'Reads and writes equal' END,

[table] = AB_Utility.dbo.AB_GetTwoPartName(

c.[object_id], DB_ID()), [index] = i.Name,

c.reads, c.writes FROM

calced AS c INNER JOIN sys.indexes AS i ON

c.[object_id] = i.[object_id]

Listing 7 Measuring the usefulness of indexes

Trang 3

AND c.index_id = i.index_id WHERE

c.writes >= c.reads;

END GO

Note that because the read and write metrics are per operation, not per row, a DMLoperation that affects 100 rows will only count as one user update in this view

Finding inefficient queries

The table-valued function in listing 8 will return the top n queries, ordered in

descending order by longest average CPU time, longest average elapsed time, highestaverage reads, highest logical reads, highest writes, or highest number of executions.Because this one query does not rely on database-specific catalog views, it can be cre-ated in the utility database and called from anywhere (passing database name, num-ber of rows, and ordering preference) You can also add a WHERE clause to restrict theresult set to objects matching a certain naming pattern or queries that executed at

@database_name SYSNAME, @number_of_rows INT,

@order_by VARCHAR(15) )

RETURNS TABLE AS

RETURN ( SELECT TOP (@number_of_rows) * FROM (

SELECT exec_object = AB_Utility.dbo.AB_GetTwoPartName(

est.objectid, est.[dbid]), exec_statement = AB_Utility.dbo.AB_ParseSQLText(est.[text], qs.statement_start_offset, qs.statement_end_offset ), u.sqlserver_start_time,

uptime_minutes = u.uptime, execution_count,

first_execution_time = qs.creation_time, qs.last_execution_time,

avg_cpu_time_milliseconds = qs.total_worker_time / (1000 * qs.execution_count), avg_logical_reads

= qs.total_logical_reads / qs.execution_count,

Listing 8 Finding inefficient queries

Trang 4

Some interesting applications of my favorite DMVs

avg_physical_reads = qs.total_physical_reads / qs.execution_count, avg_writes

= qs.total_logical_writes / qs.execution_count, avg_elapsed_time_milliseconds

= qs.total_elapsed_time / (1000 * qs.execution_count) FROM

sys.dm_exec_query_stats AS qs CROSS APPLY

sys.dm_exec_sql_text(qs.[sql_handle]) AS est CROSS JOIN

AB_Utility.dbo.AB_Uptime() AS u WHERE

est.[dbid] = DB_ID(@database_name) ) x

ORDER BY CASE @order_by WHEN 'cpu time' THEN avg_cpu_time_milliseconds WHEN 'logical reads' THEN avg_logical_reads WHEN 'physical reads' THEN avg_physical_reads WHEN 'writes' THEN avg_writes

WHEN 'elapsed time' THEN avg_elapsed_time_milliseconds WHEN 'executions' THEN execution_count

END DESC, exec_object );

GO USE [tempdb];

GO SELECT * FROM AB_Utility.dbo.AB_GetInefficientQueries (

'msdb', 50, 'cpu time' )

WHERE exec_object NOT LIKE '%sp_get_composite_job_info%' WHERE execution_count >= 50;

Finding missing indexes

Starting with SQL Server 2005, the database engine started keeping track of indexesthat the optimizer would have taken advantage of, if they existed The missing indexDMVs should be used only as a guide, and not as the final authority on how you shouldchange your index structures (As with other DMVs, the data does not persist betweenrestarts Also, be careful about relying on data for tables with indexes that havechanged recently, as this can also clear out missing index information.) The function

in listing 9 will return a slightly more useful output structure to help you determinewhich tables and indexes you should further investigate for fine tuning This includesinformation about how long SQL Server has been up, when the last user seek or scanwas for that specific query (because it may represent an ad hoc query outside of yournormal workload), and the CREATE INDEX DDL if you wanted to follow through with the

Trang 5

suggestion To use the function, you pass in the database name and the number ofrows you want to return.

@database_name SYSNAME, @number_of_rows INT )

RETURNS TABLE AS

RETURN ( SELECT TOP (@number_of_rows) *,

must give credit to Tibor Karazsi here:

[statement] = 'CREATE INDEX [<<index name>>]' + ' ON ' + [table] + ' ('

+ COALESCE(eq + COALESCE(', ' + iq, ''), iq) + ')' + COALESCE(' INCLUDE(' + ic + ');', ';') FROM

( SELECT [table] = AB_Utility.dbo.AB_GetTwoPartName(

d.[object_id], d.database_id),

eq = d.equality_columns,

iq = d.inequality_columns,

ic = d.included_columns, relative_benefit = (s.user_seeks + s.user_scans)

* (s.avg_total_user_cost * s.avg_user_impact), s.user_seeks,

s.user_scans, s.last_user_seek, s.last_user_scan FROM

sys.dm_db_missing_index_details AS d INNER JOIN

sys.dm_db_missing_index_groups AS g

ON d.index_handle = g.index_handle INNER JOIN

sys.dm_db_missing_index_group_stats AS s

ON g.index_group_handle = s.group_handle WHERE

d.database_id = DB_ID(@database_name) ) x

CROSS JOIN AB_Utility.dbo.AB_Uptime() ORDER BY relative_benefit DESC );

Listing 9 Finding missing indexes

Trang 6

DMV categories in SQL Server

GO SELECT * FROM AB_Utility.dbo.AB_GetMissingIndexes (

'Org00010001', 50

);

DMV categories in SQL Server

Table 1 lists the DMV categories in both SQL Server 2005 and SQL Server 2008 Table 2lists the new DMV categories in SQL Server 2008

Table 1 DMV categories in SQL Server 2005 and 2008

Common Language Runtime (CLR)

library/ms176083.aspx

library/ms178621.aspx

Trang 7

Gone are the days of running DBCC commands and system stored procedures overand over again, and keeping links to Profiler and Performance Monitor on everymachine’s desktop, when trying to peek into the usage characteristics of our SQLServer instances I hope I have provided a glimpse of how much power we have beengiven through DMVs and DMFs, and that I have inspired you to use them more oftenwhen observing usage or troubleshooting performance issues

About the author

Aaron Bertrand is the Senior Data Architect at One to OneInteractive, a global marketing agency headquartered in Boston,Massachusetts At One to One, Aaron is responsible for databasedesign and application architecture Due to his commitment tothe community, shown through blogging at http://www.sql-blog.com, peer-to-peer support on forums and newsgroups, andspeaking at user group meetings and code camps, he has beenawarded as a Microsoft MVP since 1998 Aaron recently pub-lished a technical white paper for Microsoft, detailing how to usethe new Resource Governor feature in SQL Server 2008

Table 2 New DMV categories in SQL Server 2008

Trang 8

or even all, of the rows were deleted from the table, Access wouldn’t reuse the

space It kept adding rows to the end of the table and never backfilled the holes.

Compacting the Access database file would get rid of the holes

Understanding how SQL Server automatically reuses table space

I’m not an expert in Access, and I’m certainly not knocking it I haven’t even usedAccess since v2.0 back in the 1990s This behavior may have changed since then, orperhaps I misremember, but suffice it to say that with SQL Server this is not anissue But don’t take my word for it Let’s consider an example to prove the point

To set up the example, let’s create a table with three columns and then populate

it with test data The T-SQL code for doing this is shown in listing 1

USE tempdb ; GO

create a test table CREATE TABLE dbo.Test (

col1 INT ,col2 CHAR(25) ,col3 VARCHAR(4000) ) ;

create some test data DECLARE @cnt INT ; SET @cnt = 0 ;

Listing 1 Creating and populating the dbo.Test table

Trang 9

WHILE @cnt < 1000 BEGIN

SELECT @cnt = @cnt + 1 ; INSERT

dbo.Test ( col1,col2,col3 ) VALUES (

@cnt ,'test row # ' + CAST(@cnt AS VARCHAR(10)) + 'A' ,REPLICATE('ABCD', ROUND(RAND() * @cnt, 0)) ) ;

END

The CREATE TABLE statement creates a table called Test as part of the dbo schema inthe tempdb database The table is composed of three columns The first is an integer,the second is a fixed length character string that contains 25 characters, and thethird and final column is a variable length character string that can contain up to

1900 characters

To add test data, we will use a simple INSERT statement The INSERT statement hasbeen placed inside a WHILE loop and is executed 1,000 times Note that the last col-umn in the table will vary in length from zero to a maximum of 4,000 Statistically, itshould average around 1,000 characters

Let’s view the contents of the table to make sure we have what we think we have

We can do this by running a SELECT statement to retrieve the data, as shown inlisting 2 The results of the query are shown in figure 1

view the table SELECT

* FROM dbo.Test ;

To examine the amount of space the table consumes, we’ll use a Dynamic ment View (DMV) called sys.dm_db_index_physical_stats DMVs were first intro-duced in SQL Server 2005 and have been continued in SQL Server 2008 (For more onDMVs, see chapter 29, “My favorite DMVs, and why,” by Aaron Bertrand.)

Manage-NOTE The scripts that make use of DMVs will not work in SQL Server 2000

The query in listing 3 returns a single row of data with four columns—the allocationunit type, the page count, the average page space used as a percentage, and the totalListing 2 Querying the dbo.Test table

Trang 10

Understanding how SQL Server automatically reuses table space

number of rows in the dbo.Test table The alloc_unit_type_desc column describes theallocation unit type: valid values are IN_ROW_DATA, LOB_DATA, or ROW_OVERFLOW_DATA(see Books Online for a detailed explanation of these terms) The page_count col-umn indicates the total number of data pages used by the table for IN_ROW_DATA (that

is, the allocation unit that stores the table rows in our example) The avg_page_space_used_in_percent column reports the average percentage of space used in alldata pages in the IN_ROW_DATA allocation type The record_count intuitively containsthe number of rows contained in the table

check the size of the table SELECT

alloc_unit_type_desc ,page_count

,avg_page_space_used_in_percent ,record_count

FROM sys.dm_db_index_physical_stats(

DB_ID() ,OBJECT_ID(N'dbo.Test') ,NULL

,NULL ,'Detailed') ;

Figure 2 displays the results of the DMV query As you can see on my test system, 158data pages are used to store the 1,000 rows and each data page is, on average, 82.1 per-cent full

To continue with the example, let’s delete half of the rows in our dbo.Test table andsee what happens to the space used by the table The T-SQL script in listing 4 uses themodulo operator, represented by the percent sign in T-SQL, to delete each row wherethe value in the first column, col1, is an odd number So we are deleting every otherrow in the table

delete the odd rows DELETE FROM

Test WHERE col1 % 2 = 1 view the table SELECT

*

Listing 3 Examining the space used by the dbo.Test table

Listing 4 Deleting the odd-numbered rows

Figure 2 Using a DMV to review space used

Trang 11

FROM dbo.Test ;

Figure 3 shows the results of the SELECT query You can see that the table is left withtest row #2, test row #4, and so on, for a total of 500 rows

Now that half of the rows in the table have been deleted, let’s look at the space thetable consumes by running the DMV query from listing 3 again

Figure 4 shows that the number of pages used to store the table’s data remains stant but the percentage used of each page changes It is cut in half, from 82.1 percent

con-to 42.1 percent Also notice that the number of rows reported in the record_countcolumn has been cut in half, as expected

So let’s add some new rows to the table to prove that SQL Server will automatically

reuse the newly freed space, thus filling in the holes, to go back to our Access

compari-son Using the T-SQL script found in listing 5, we can quickly add 500 rows of data tothe dbo.Test table This script is similar to the one first used to populate the table withsample data It inserts one row at a time in a WHILE loop To help differentiate the newrows from the existing rows, I change the insert statement so that the third column isfilled with WXYZ’s rather than ABCD’s as before, though it doesn’t matter for our proof

add some more test data DECLARE @cnt INT ;

SET @cnt = 0 ; WHILE @cnt < 500 BEGIN SELECT @cnt = @cnt + 1 ; INSERT

dbo.Test ( col1,col2,col3 ) VALUES (

@cnt ,'test row # ' + CAST(@cnt AS VARCHAR(10))

Listing 5 Adding new rows to the dbo.Test table

Figure 3 Deleting half the rows

in the dbo.Test table

Figure 4 Examining the space consumed by the dbo.Test table

Trang 12

Recognizing when SQL Server does not reclaim space

,REPLICATE('WXYZ', ROUND(RAND() * @cnt, 0)) ) ;

To clean up after this example, let’s execute one final statement to drop thedbo.Test table Listing 6 displays the DROP statement

clean up DROP TABLE dbo.Test ;

Recognizing when SQL Server does not reclaim space

Under certain circumstances SQL Server does not automatically reclaim space that is

no longer being used If a table definition is altered to drop one or more variablelength columns, the space consumed by those columns is not immediately made avail-able for reuse by SQL Server

To illustrate this behavior, let’s consider an example Let’s create another test tableusing the script in listing 7 The script creates the table and populates it with 1,000rows of data

USE tempdb ; GO

create the dbo.Test2 table

Listing 6 Dropping the dbo.Test table

Listing 7 Creating the dbo.Test2 table

Figure 5 Reviewing the space used

by the dbo.Test table after inserting new rows

Trang 13

CREATE TABLE dbo.Test2 (

col1 INT ,col2 CHAR(25) ,col3 VARCHAR(4000) ) ;

create some test data DECLARE @cnt INT ; SET @cnt = 0 ; WHILE @cnt < 1000 BEGIN SELECT @cnt = @cnt + 1 ; INSERT

dbo.Test2 ( col1,col2,col3) VALUES (

@cnt ,'test row # ' + CAST(@cnt AS VARCHAR(10)) ,REPLICATE('A', 4000)

) ; END

Figure 6 shows the results from the SELECT statement This table has three columns ofdata, an integer in the first column, a character string in the second column that cancontain up to 25 characters, and a variable length character string in the final columnthat contains 4,000 characters

Using the query shown in listing 3, we can see how much space our newly createddbo.Test2 table is consuming Figure 7 shows the results The newly created tabletakes up 500 data pages to store the 1,000 rows of data Each data page is, on average,99.9 percent full

Now to set up our test scenario, let’s drop the third column, the one that consumesthe most space Listing 8 contains the ALTER TABLE script to drop col3; it then executesthe DMV query to reveal the space used by the table

Figure 6 Viewing data in the dbo.Test2 table

Figure 7 Space used by the dbo.Test2 table

Trang 14

Recognizing when SQL Server does not reclaim space

drop the last column ALTER TABLE dbo.Test2 DROP COLUMN col3 ; check the space used again

SELECT alloc_unit_type_desc ,page_count

,avg_page_space_used_in_percent ,record_count

FROM sys.dm_db_index_physical_stats(

DB_ID() ,OBJECT_ID(N'dbo.Test2') ,NULL

,NULL ,'Detailed') ;

Looking at the results in figure 8, we can see that we get the same results asbefore—500 data pages, storing 1,000 rows, each 99.9 percent full, and this after drop-ping the column that consumed the most space

Why is this? When a table is altered to drop a column, SQL Server does not remove thecolumn data from the data pages Instead it updates the metadata in the system tables

so that when queried, it appears as if the column no longer exists The data is stillpresent in the data pages, but it’s not returned as a part of a result set Thus, the spacecannot be reused initially

So let’s add some additional rows to the table and see what happens The scriptshown in listing 9 inserts 500 additional rows into the dbo.Test2 table Notice that thisscript inserts only two columns of data because we’ve dropped the third column, col3

insert additional rows DECLARE @cnt INT ; SET @cnt = 0 ; WHILE @cnt < 500 BEGIN SELECT @cnt = @cnt + 1 ; INSERT

dbo.Test2 ( col1,col2 ) VALUES (

@cnt ,'test row # ' + CAST(@cnt AS VARCHAR(10)) ) ;

END

Listing 8 Dropping a varchar column in the dbo.Test table

Listing 9 Adding more rows to the dbo.Test2 table

Figure 8 Reviewing the space used after dropping a column

Trang 15

When we use the sys.dm_db_index_physical_stats DMV to examine the space used

by the dbo.Test2 table, we find that the number of data pages increases slightly, cating that the space once consumed by the dropped column was not automaticallyreused Figure 9 shows the results

indi-If the space had been automatically reused, there should have been more thanenough space to contain the additional 500 rows without having to add more space.But because the columns were only marked as no longer being part of the table in themetadata, there is no space to reuse So SQL Server added additional pages to makespace for the new rows

Using DBCC CLEANTABLE

to reclaim unused table space

Although it is comforting to know that in certain cases SQL Server will automaticallyreuse space once consumed by deleted rows, we’ve just seen that it will not automati-cally reuse space once consumed by dropped columns

Fortunately, this space is not lost forever; we can reclaim this space by issuing aDBCC command The DBCC CLEANTABLE command allows us to specify a database andtable, and it will free up any space once consumed by dropped variable length charac-ter columns

To reclaim the space in our dbo.Test2 table, run the T-SQL command found inlisting 10

reclaim the space from the table DBCC CLEANTABLE('tempdb', 'dbo.Test2') ;

If it succeeds, you should receive a message similar to the following as an output sage in the query window

mes-DBCC execution completed If mes-DBCC printed error messages, contact your system administrator.

DBCC CLEANTABLE reclaims space from variable length columns that no longer exist aspart of the table definition In this context a variable length column can be one of thefollowing data types: varchar, nvarchar, varchar(max), nvarchar(max), varbinary,varbinary(max), text, ntext, image, sql_variant, and xml

Now that we’ve used the DBCC command to reclaim the space, let’s return to ourDMV query and examine the space used by the table by running listing 3

Listing 10 Reclaiming space using DBCC CLEANTABLE

Figure 9 Inserting rows after dropping a column

Trang 16

Summary

Figure 10 shows the results Notice that the number of data pages consumed by thetable did not decrease; however, the average space used as a percentage decreaseddramatically to 1.4 percent from 99.7 percent

To prove that the space is truly available for reuse, let’s add another 3,000 rows to thetable by altering and running listing 9

Now checking the used space by again running the DMV query in listing 3, we seethat the number of pages used to make the table did not increase Notice, though,that the average page space used as a percentage did increase to 4.2 percent from 1.4percent Figure 11 shows the results

Summary

When rows are deleted from a table without a clustered index (an object known as aheap), SQL Server can readily reuse the space that was once consumed by deletedrows The same may hold true for clustered tables, as long as the newly inserted rows

have the required key values that would allow them be placed in a hole created by a

deleted row

There are certain circumstances, in which deleted space is not immediately released

for reuse; in particular when variable length character columns are dropped from atable By employing the DBCC CLEANTABLE command we can reclaim the space onceconsumed by the dropped columns and make better use of the disk resources at hand

About the author

Joe Webb, a Microsoft SQL Server MVP, serves as Chief ing Manager for WebbTech Solutions, a Nashville-based IT con-sulting company He has over 15 years of industry experienceand has consulted extensively with companies in the areas ofbusiness process analysis and improvements, database designand architecture, software development, and technical training

In addition to helping his consulting clients, Joe enjoys ing and speaking at technical conferences He has delivered

writ-Figure 10 The space consumed

by dbo.Test2 after running DBCC CLEANTABLE

Figure 11 Inserting rows into reclaimed space

Trang 17

over 50 sessions at conferences in Europe and North America and has authored twoother books.

Joe served for six years on the Board of Directors for the Professional Associationfor SQL Server (PASS), an international user group with 30,000 members worldwide

He culminated his tenure on the board by serving as the Executive Vice President ofFinance for the organization Joe also volunteers his time by serving on the MBA Advi-sory Board for Auburn University and the Computer Science Advisory Committee forNashville State Community College

When he’s not consulting, Joe enjoys spending time with his family and tending tothe animals and garden on his small farm in middle Tennessee He can be reached atjoew@webbtechsolutions.com

Trang 18

in table partitioning Ron Talmage

This chapter covers two practical topics in SQL Server table partitioning:

ƒ Strategies for creating partition functions

ƒ Minimizing data movementLarge SQL Server databases can become difficult to manage due to their size Usu-ally such large databases have only a couple of large tables that account for most ofthe databases’ size Table partitioning is a method for making those large tableseasier to manage, and in some cases improving query performance against thoselarge tables

Table partitioning is an involved topic and can quickly become complex;therefore, this chapter is going to have a limited scope Let’s look at table parti-tioning from a general point of view, and then zero in on the two topics men-tioned previously

Table partitioning dependencies

Let’s begin with a basic overview that’ll introduce the terminology surroundingtable partitioning Our focus will be on SQL Server 2008 and SQL Server 2005.Table partitioning was introduced with SQL Server 2005 and enhanced a bit inSQL Server 2008, but the overall architecture of table partitioning remains thesame between both versions

NOTE Table partitioning is an Enterprise Edition–only feature in SQL Server

2008 and 2005 That means you can use the Developer and EvaluationEditions to learn about table partitioning, but you can put it into pro-duction only with the Enterprise Edition

In a nutshell, a SQL Server–partitioned table is a table that’s referred to as one tablebut is subdivided into multiple segments or partitions In fact, technically all data-base tables in SQL Server 2005 and later are partitioned with one implicit partition

Trang 19

Such tables aren’t normally referred to as explicitly partitioned They can’t have more

than one partition without also having been created on a partition scheme with a valid

partition function Only when a table has this additional property does it become atruly partitioned table

The extra properties that a partitioned table needs concern two aspects:

ƒ Storage (partition scheme)—Every partitioned table requires a definition for its storage, called a partition scheme The partition scheme defines on which file-

groups the table's multiple partitions will be stored

ƒ Partition boundaries (partition function)—Every partition scheme must be bound

to a partition function that defines the partition boundaries, and therefore the

parti-Manipulating partitioned data

Once a partitioned table is defined using a partition scheme and function, data can beadded and removed from the partitioned table, one partition at a time

A single table can be loaded with external data and then swapped with an emptytable partition, using what’s called a SWITCH operation, something done by ALTERingthe table Next, by manipulating the table’s partition function, two partitions in atable can be combined (using the MERGE option) to form one single new partition,and a single partition can be SPLIT into two partitions

The end result is that you can use the SWITCH, MERGE, and SPLIT operations tomodify a partitioned table so as to load new data and remove old data

For example, to load new data into a tioned table, follow these steps:

parti-1 Load the new data into a staging table inthe same database

2 Use SPLIT to create an empty new partition

at the “front” end of the table (where youwant to load the data)

3 Swap the staging table with the empty tion, using the SWITCH operation

parti-Figure 2 shows the loading operation

Figure 1 Dependency relations of the major table partitioning components

Figure 2 An initially empty partition is

Trang 20

How the partition function works

In figure 2, partition 3 is initially empty and a staging table S is created and loadedwith data Then S is swapped with partition 3 using the SWITCH operation The result isthat partition 3 has the data and the staging table is now empty This operationinvolves metadata only, and only takes milliseconds to complete, no matter what thesize of the new table Because there’s no data transfer, no activity other than the meta-data operation is logged in the transaction log This is how table partitioning can loaddata quickly

Although the metadata operations (SWTICH, MERGE, and SPLIT) occur almostinstantaneously, to accomplish them SQL Server requires a schema modification lock

on the partitioned table If other transactions are running that prevent the schemamodification lock, the metadata operations will be blocked until the required lock isgranted

To remove old data from a partitioned table, follow these steps:

1 Create an empty staging table in the same database

2 Swap the staging table with the oldest full partition, using the SWITCH operation

3 MERGE the old empty partition with an empty neighbor to remove it

Figure 3 illustrates how data can be removed

In figure 3, partition 1 has data and an emptystaging table S is created Then S is swapped withpartition 1 using the SWITCH operation, resulting

in an empty partition 1 but a full staging table S

As before, this operation involves metadata only,and only takes milliseconds to complete Oncethe data has been swapped out to staging table S,partition 1 can be merged with partition 2 oranother empty partition, and effectively disap-pear Then the staging table data can optionally

be copied out of the database for archiving, andthe table truncated and reused later This is howtable partitioning can remove data quickly from

a partitioned table

How the partition function works

So how does this all work? As figure 1 showed, it all starts with the partition function.This is a special type of function used in table partitioning, and is unlike other T-SQLfunctions in several ways Like other functions, each partition function is scoped tothe database it’s created in Although the partition function is a user-created databaseobject, it’s not visible by ordinary means in Management Studio; you can find parti-tion functions, along with partition schemes, in the Storage node for each database.The reason is because the partition function is a special database object, not found inthe sys.sysobjects table like other user-created and system functions

Figure 3 A full partition can be swapped with an empty staging table

to remove data.

Ngày đăng: 15/12/2013, 13:15

TỪ KHÓA LIÊN QUAN

w