Search This Blog

2010-04-24

Maximum Capacity of different properties for SQL Server

The following table specifies the maximum sizes and numbers of various objects defined in SQL Server databases or referenced in Transact-SQL statements.
SQL Server Database Engine object Maximum sizes/numbers SQL Server (32-bit) Maximum sizes/numbers SQL Server (64-bit)
Batch size 165,536 * Network Packet Size65,536 * Network Packet Size
Bytes per short string column8,0008,000
Bytes per GROUP BY, ORDER BY8,0608,060
Bytes per index key2900900
Bytes per foreign key900900
Bytes per primary key900900
Bytes per row88,0608,060
Bytes in source text of a stored procedureLesser of batch size or 250 MBLesser of batch size or 250 MB
Bytes per varchar(max), varbinary(max), xml, text, or image column2^31-12^31-1
Characters per ntext or nvarchar(max) column2^30-12^30-1
Clustered indexes per table11
Columns in GROUP BY, ORDER BYLimited only by number of bytesLimited only by number of bytes
Columns or expressions in a GROUP BY WITH CUBE or WITH ROLLUP statement1010
Columns per index key71616
Columns per foreign key1616
Columns per primary key1616
Columns per nonwide table1,0241,024
Columns per wide table30,00030,000
Columns per SELECT statement4,0964,096
Columns per INSERT statement40964096
Connections per clientMaximum value of configured connectionsMaximum value of configured connections
Database size524,272 terabytes524,272 terabytes
Databases per instance of SQL Server32,76732,767
Filegroups per database32,76732,767
Files per database32,76732,767
File size (data)16 terabytes16 terabytes
File size (log)2 terabytes2 terabytes
Foreign key table references per table4253253
Identifier length (in characters)128128
Instances per computer50 instances on a stand-alone server for all SQL Server editions except for Workgroup. Workgroup supports a maximum of 16 instances per computer.SQL Server supports 25 instances on a failover cluster.50 instances on a stand-alone server.25 instances on a failover cluster.
Length of a string containing SQL statements (batch size)165,536 * Network packet size65,536 * Network packet size
Locks per connectionMaximum locks per serverMaximum locks per server
Locks per instance of SQL Server5Up to 2,147,483,647Limited only by memory
Nested stored procedure levels63232
Nested subqueries3232
Nested trigger levels3232
Nonclustered indexes per table999999
Number of distinct expressions in the GROUP BY clause when any of the following are present: CUBE, ROLLUP, GROUPING SETS, WITH CUBE, WITH ROLLUP3232
Number of grouping sets generated by operators in the GROUP BY clause4,0964,096
Parameters per stored procedure2,1002,100
Parameters per user-defined function2,1002,100
REFERENCES per table253253
Rows per tableLimited by available storageLimited by available storage
Tables per database3Limited by number of objects in a databaseLimited by number of objects in a database
Partitions per partitioned table or index1,0001,000
Statistics on non-indexed columns30,00030,000
Tables per SELECT statementLimited only by available resourcesLimited only by available resources
Triggers per table3Limited by number of objects in a databaseLimited by number of objects in a database
Columns per UPDATE statement (Wide Tables)40964096
User connections32,76732,767
XML indexes249249

1Network Packet Size is the size of the tabular data stream (TDS) packets used to communicate between applications and the relational Database Engine. The default packet size is 4 KB, and is controlled by the network packet size configuration option.2The maximum number of bytes in any index key cannot exceed 900 in SQL Server. You can define a key using variable-length columns whose maximum sizes add up to more than 900, provided no row is ever inserted with more than 900 bytes of data in those columns. In SQL Server, you can include nonkey columns in a nonclustered index to avoid the maximum index key size of 900 bytes.3Database objects include objects such as tables, views, stored procedures, user-defined functions, triggers, rules, defaults, and constraints. The sum of the number of all objects in a database cannot exceed 2,147,483,647.4Although a table can contain an unlimited number of FOREIGN KEY constraints, the recommended maximum is 253. Depending on the hardware configuration hosting SQL Server, specifying additional FOREIGN KEY constraints may be expensive for the query optimizer to process.5This value is for static lock allocation. Dynamic locks are limited only by memory.6If a stored procedure accesses more than 8 databases, or more than 2 databases in interleaving, you will receive an error.7If the table contains one or more XML indexes, the clustering key of the user table is limited to 15 columns because the XML column is added to the clustering key of the primary XML index. In SQL Server, you can include nonkey columns in a nonclustered index to avoid the limitation of a maximum of 16 key columns. For more information, see Index with Included Columns.8 SQL Server supports row-overflow storage which enables variable length columns to be pushed off-row. Only a 24-byte root is stored in the main record for variable length columns pushed out of row; because of this, the effective row limit is higher than in previous releases of SQL Server. For more information, see the "Row-Overflow Data Exceeding 8 KB" topic in SQL Server Books Online.

Document Obtain From:
http://msdn.microsoft.com/en-us/library/ms143432.aspx

Summarizing Data Using ROLLUP and CUBE

The ROLLUP operator is useful in generating reports that contain subtotals and totals.

For example, a simple table Inventory contains the following:
Item Color Quantity
-------------------- -------------------- --------------------------
Table Blue 124
Table Red 223
Chair Blue 101
Chair Red 210

This query generates a subtotal report:

SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
ELSE ISNULL(Item, 'UNKNOWN')
END AS Item,
CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
ELSE ISNULL(Color, 'UNKNOWN')
END AS Color,
SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUP

Item Color QtySum
-------------------- -------------------- --------------------------
Chair Blue 101.00
Chair Red 210.00
Chair ALL 311.00
Table Blue 124.00
Table Red 223.00
Table ALL 347.00
ALL ALL 658.00

(7 row(s) affected)

If the ROLLUP keyword in the query is changed to CUBE, the CUBE result set is the same, except these two additional rows are returned at the end:
ALL Blue 225.00
ALL Red 433.00

Following are the specific differences between CUBE and ROLLUP:

CUBE generates a result set that shows aggregates for all combinations of values in the selected columns.

ROLLUP generates a result set that shows aggregates for a hierarchy of values in the selected columns.

2010-04-13

Rank Function in SQL Server

SQL Server 2005 introduced new ranking functions. This article is an introduction to these functions, difference between them and when to use each and a few examples.

Ranking What:

The new ranking functions are new internal functions to SQL Server 2005/2008.

In simple terms, ranking functions allow you to sequentially number your result set. Your result set can be partitioned so the numbering essentially resets for each partition for example you can get the sales rank of employees partitioned by their department, or manager etc..

What's worth mentioning is that ranking functions are non-deterministic so you cannot use them in something like an indexed view.

Sample Table:

This will serve as our sample table for all our examples.

sales_employee
nameterritorysales_amount
AX100
BX200
CX200
DX300
EX400
FY300
GY300
HY500
IY600
JZ200
KZ700


Syntax and Examples:

ROW_NUMBER () OVER ( [ <partition_by_clause> ] <order_by_clause> )
WHERE

Returns the row number of the result set for each row in a partition based on the order provided in the order by clause.

RANK () OVER ( [ <partition_by_clause> ] <order_by_clause> )

Similar to Row_Number() only Rank determines the position, or lack for a better word, ranking of each row based on the Order By clause. Rank is usually used with the Partition clause to cluster your result sets. Rank also skips numbers, if 2 or more records tie in value, they will receive the same rank. The following rank would 1+ the total number of records in the same partition so for example (1, 2, 2, 2, 5, 6)

DENSE_RANK () OVER ( [ <partition_by_clause> ] <order_by_clause> )

Same as Rank() only guarantees consecutive integers (No skipping) (1, 2, 2, 2, 2, 3). If a tie occurs, it will sort arbitrarily (based on the execution plan and indexes used) and continue.

NTILE (integer_expression) OVER ( [ <partition_by_clause> ] <order_by_clause> )

Used to distribute the rows in an ordered partition into x number of groups. Each row receives the group number it belongs to.


Example: Simple ORDER BY clause

SELECT
name
,territory
,ROW_NUMBER() OVER ( ORDER BY sales_amount ) AS [row_number]
,RANK() OVER ( ORDER BY sales_amount ) AS [rank]
,DENSE_RANK() OVER ( ORDER BY sales_amount ) AS [dense_rank]
,NTILE(4) OVER ( ORDER BY sales_amount ) AS [ntile]
FROM
sales_employee


nameterritorysales_amountROW_NUMBERRANKDENSE_RANKNTILE
AX1001111
BX2002221
CX2003221
DX3005532
EX4008843
FY3006532
GY3007533
HY5009953
IY600101064
JZ2004222
KZ700111174


Example: Using PARTITION BY

SELECT
name
,territory
,ROW_NUMBER() OVER ( PARTITION BY territory ORDER BY sales_amount ) AS [row_number]
,RANK() OVER ( PARTITION BY territory ORDER BY sales_amount ) AS [rank]
,DENSE_RANK() OVER ( PARTITION BY territory ORDER BY sales_amount ) AS [dense_rank]
,NTILE(4) OVER ( PARTITION BY territory ORDER BY sales_amount ) AS [ntile]
FROM
sales_employee

nameterritorysales_amountROW_NUMBERRANKDENSE_RANKNTILE
AX1001111
BX2002221
CX2003222
DX3004433
EX4005544
FY3001111
GY3002112
HY5003323
IY6004434
JZ2001111
KZ7002222