Вы находитесь на странице: 1из 2

As for the RANGE window frame extent, according to standard SQL, it allows you t o define delimiters based on logical

offsets from the current row s sort key. Remember that ROWS defines the delimiters based on physical offsets in terms of number of rows from the current row. However, SQL Server 2012 has a very limited implementation of the RANGE opt ion, supporting only UNBOUNDED PRECEDING or FOLLOWING and CURRENT ROW as delimiters. One subtle difference between ROWS and RANGE when using the same delimiters is t hat the former doesn t include peers (tied rows in terms of the sort key) and the latter d oes. IMPORTANT ROWS vs. RANGE In SQL Server 2012, the ROWS option usually gets optimized much better than RANG E when using the same delimiters. If you define a window with a window order claus e but without a window frame clause, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Therefore, unless you are after the special behavior you get fr om RANGE that includes peers, make sure you explicitly use the ROWS option. Window Ranking Functions With window ranking functions, you can rank rows within a partition based on spe cified ordering. As with the other window functions, if you don t indicate a window parti tion clause, the entire underlying query result is considered one partition. The window order clause is mandatory. Window ranking functions do not support a window frame clause. T-SQL supports four window ranking functions: ROW_NUMBER, RANK, DENSE_RANK, and NTILE. The following query demonstrates the use of these functions. SELECT custid, orderid, val, ROW_NUMBER() OVER(ORDER BY val) AS rownum, RANK() OVER(ORDER BY val) AS rnk, DENSE_RANK() OVER(ORDER BY val) AS densernk, NTILE(100) OVER(ORDER BY val) AS ntile100 FROM Sales.OrderValues; This query generates the following output (shown here in abbreviated form). custid orderid val rownum rnk densernk ntile100 ------- -------- ------ ------- ---- --------- --------12 10782 12.50 1 1 1 1 27 10807 18.40 2 2 2 1 66 10586 23.80 3 3 3 1 76 10767 28.00 4 4 4 1 54 10898 30.00 5 5 5 1 88 10900 33.75 6 6 6 1 48 10883 36.00 7 7 7 1 41 11051 36.00 8 7 7 1 71 10815 40.00 9 9 8 1 38 10674 45.00 10 10 9 2 53 11057 45.00 11 10 9 2 75 10271 48.00 12 12 10 2 XML Indexes The XML data type is actually a large object type. There can be up to 2 gigabyte s (GB) of data in every single column value. Scanning through the XML data sequentially is not a very

efficient way of retrieving a simple scalar value. With relational data, you can create an index on a filtered column, allowing an index seek operation instead of a table scan. Similarly, you can index XML columns with specialized XML indexes. The first index you crea te on an XML column is the primary XML index. This index contains a shredded persisted re presentation of the XML values. For each XML value in the column, the index creates several r ows of data. The number of rows in the index is approximately the number of nodes in the XML value. Such an index alone can speed up searches for a specific element by using the exist() method. After creating the primary XML index, you can create up to three other t ypes of secondary XML indexes: PATH This secondary XML index is especially useful if your queries specify path e xpressions. It speeds up the exist() method better than the Primary XML index. Such an index also speeds up queries that use value() for a fully specified path. VALUE This secondary XML index is useful if queries are value-based and the path is not fully specified or it includes a wildcard. PR OPERTY This secondary XML index is very useful for queries that retrieve one o r more values from individual XML instances by using the value() method. The primary XML index has to be created first. It can be created only on tables with a clustered primary key. Practice Using XML Data Type Methods In this practice, you use XML data type methods. If you encounter a problem completing an exercise, you can install the completed projects from the Solution folder that is provided with the companion content for this ch apter and lesson. Exercise 1 Use the value() and exist() Methods In this exercise, you use the value() and exist() XML data type methods. 1. If you closed SSMS, start it and connect to your SQL Server instance. Open a new query window by clicking the New Query button. 2. Connect to your TSQL2012 database.

Вам также может понравиться