Sunday, 6 December 2015

SQL Server Management Studio Shortcuts

SQL Server Management Studio Shortcuts
Here’s a list of shortcuts available from SQL Server Management Studio (SSMS).  This includes the function keys and what it does in SQL Server Management Studio.  Some of these shortcuts are the same as the ones used in other applications such Microsoft Word like the ones in the Basic Editing Shortcuts.

Basic Editing Shortcuts
Ctrl+X
Cut
Ctrl+C
Copy
Ctrl+V
Paste
Ctrl+A
Select All
Ctrl+Z
Undo
Ctrl+Y
Redo
Ctrl+F
Quick Find
Ctrl+H
Quick Replace
Ctrl+Shift+H
Replace in Files
Ctrl+G
Go To Line

File Shortcuts
Ctrl+N
New Query with Current Connection
Ctrl+Shift+N
New Project
Ctrl+O
Open File
Ctrl+Shift+O
Open Project/Solution
Ctrl+S
Save
Ctrl+Shift+S
Save All
Ctrl+P
Print

View Shortcuts
Ctrl+Alt+G
Registered Servers
Ctrl+Alt+T
Template Explorer
Ctrl+Alt+L
Solution Explorer
Ctrl+K, Ctrl+W
Bookmark Window
Ctrl+Alt+X
Toolbox
Ctrl+\, Ctrl+E
Error List
Ctrl+Alt+R
Web Browser
Ctrl+Alt+K
Task List
Ctrl+Alt+O
Output
Shift+Alt+Enter
Full Screen
Ctrl+-
Navigate Backward

Query Shortcuts
Ctrl+Shift+M
Specify Values for Template Parameters
Ctrl+L
Display Estimated Execution Plan
Ctrl+Alt+P
Trace Query in SQL Server Profiler
Ctrl+Shift+Q
Design Query in Editor
Ctrl+M
Include Actual Execution Plan
Shift+Alt_S
Include Client Statistics
Ctrl+T
Results to Text
Ctrl+D
Results to Grid
Ctrl+Shift+F
Results to File

Window Shortcuts
Ctrl+R
Hide/Show Results Pane

Advanced Editing Shortcuts
Ctrl+Shift+U
Make Uppercase
Ctrl+Shift+L
Make Lowercase
Ctrl+K, Ctrl+\
Delete Horizontal White Space
Ctrl+I
Incremental Search
Ctrl+K, Ctrl+C
Comment Selection
Ctrl+K, Ctrl+U
Uncomment Selection

Bookmarks Shortcuts
Ctrl+K, Ctrl+K
Toggle Bookmark
Ctrl+K, Ctrl+P
Previous Bookmark
Ctrl+K, Ctrl+N
Next Bookmark
Ctrl+K, Ctrl+L
Clear Bookmarks
Ctrl+Shift+K, Ctrl+Shift+P
Previous Bookmark in Folder
Ctrl+Shift+K, Ctrl+Shift+N
Next Bookmark in Folder
Function Keys
F1
Help
Ctrl+F1
Help – How Do I
Ctrl+Alt+F1
Help Contents
Ctrl+Alt+F2
Help Index
F3
Find Next
Ctrl+Alt+F3
Help Search
F4
View Properties Window
F5
Refresh / Execute
Alt-F5
Start Debugging
Ctrl+F5
Parse
Ctrl+Alt+F5
Help Tutorial
F6
Window-Next Pane
Shift+F6
Window-Previous Pane
F7
View Object Explorer Details
F8
View Object Explorer
Alt+F8
Open Server in Object Explorer
F9
Debug-Toggle Break Point
F10
Debug-Step Over
F11
Debug-Step Into

Intellisense Shortcuts
Ctrl+J
List Members
Ctrl+Shift+Space
Parameter Info
Ctrl+K, Ctrl+I
Quick Info
Alt+Right Arrow
Complete Word
Ctrl+Shift+R
Refresh Local Cache

Using SQL Server Configuration Manager

Applies to Windows Vista, Windows 7, and Windows Server 2008
The following procedures configure the Windows Firewall by using the Windows Firewall with Advanced Security Microsoft Management Console (MMC) snap-in. The Windows Firewall with Advanced Security only configures the current profile. For more information about the Windows Firewall with Advanced Security, see Configure the Windows Firewall to Allow SQL Server Access

To open a port in the Windows firewall for TCP access

  1. On the Start menu, click Run, type WF.msc, and then click OK.
  2. In the Windows Firewall with Advanced Security, in the left pane, right-click Inbound Rules, and then click New Rule in the action pane.
  3. In the Rule Type dialog box, select Port, and then click Next.
  4. In the Protocol and Ports dialog box, select TCP. Select Specific local ports, and then type the port number of the instance of the Database Engine, such as 1433 for the default instance. Click Next.
  5. In the Action dialog box, select Allow the connection, and then click Next.
  6. In the Profile dialog box, select any profiles that describe the computer connection environment when you want to connect to the Database Engine, and then click Next.
  7. In the Name dialog box, type a name and description for this rule, and then click Finish.

To open access to SQL Server when using dynamic ports

  1. On the Start menu, click Run, type WF.msc, and then click OK.
  2. In the Windows Firewall with Advanced Security, in the left pane, right-click Inbound Rules, and then click New Rule in the action pane.
  3. In the Rule Type dialog box, select Program, and then click Next.
  4. In the Program dialog box, select This program path. Click Browse, and navigate to the instance of SQL Server that you want to access through the firewall, and then click Open. By default, SQL Server is at C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Binn\Sqlservr.exe. Click Next.
  5. In the Action dialog box, select Allow the connection, and then click Next.
  6. In the Profile dialog box, select any profiles that describe the computer connection environment when you want to connect to the Database Engine, and then click Next.
  7. In the Name dialog box, type a name and description for this rule, and then click Finish.

Thursday, 3 December 2015

SQL Server – Horizontally partitioning a SQL Server Database Table

////http://www.sqlshack.com/database-table-partitioning-sql-server/

Partitioning was introduced in SQL Server 2005, it allows you to different database files, which can be located on different disks to improve performance. In earlier versions this was achieved by creating separate tables on different file groups of database, and then creating a view by UNIONing these tables to make it look like a single table.
First, let us create a Test database for partitioning with more than one file groups. I have created [PartitionDB] with two file groups – [PRIMARY] and [FileGroup2], both of these groups contain a data file:
CREATE DATABASE [PartitionDB] ON
PRIMARY
      (     NAME = N'PartitionDB',
            FILENAME = N'C:\PartitionDB\PartitionDB.mdf' ,
            SIZE = 3072KB , FILEGROWTH = 1024KB
      ),
FILEGROUP [FileGroup2]
      (     NAME = N'PartitionDB_2',
            FILENAME = N'C:\PartitionDB\PartitionDB_2.ndf' ,
            SIZE = 3072KB , FILEGROWTH = 1024KB
      )
LOG ON
      (     NAME = N'PartitionDB_log',
            FILENAME = N'C:\PartitionDB\PartitionDB_log.ldf' ,
            SIZE = 9216KB , FILEGROWTH = 10%
      )
image
Note: I have created both files on the same disk here, it is recommended to create files on separate disks for optimal performance.
Now we have a database to work with, let’s start with table partitioning, Partitioning a Table involves three steps:

Step 1: Creating a Partition Function – This is the first step in creating a partitioned table, a partition function tells SQL Server “how” to split the data. A partition function is not related to any particular table, it is a “technique” that SQL Server will use to split the data rows.
A partition function defines the range of values for a partition. For example, customer order data can be partitioned according to order date. I.e. data for current year will be in [PRIMARY] file group, while older data will be in [FileGroup2] file group. To achieve this we can create partition function as:
— Step 1. Creating a Partition Function

CREATE PARTITION FUNCTION ordersPartFunc (DATE)
AS RANGE LEFT
FOR VALUES ('2010-01-01')

The VALUES(<list of values>) define a list of boundary values for each partition, By specifying VALUES (2010) we have created two partitions.
1. For Values < 2010 and,
2. For Values > 2010
RANGE defines to which partition the boundary values will go. It can either be specified as RANGE LEFT or RANGE RIGHT, depending on this the placement of boundary values is decided. Here, we have used RANGE LEFT that means ‘2010’ will go to LEFT partition, so the partitions will contain below data:
1. Values <= 2010 and
2. Values > 2010

Step 2: Creating a Partition Scheme – Once the partition function is created, SQL Server knows “how” to split data, but it does not know where to put this partitioned data, this is defined by partition scheme. Partition scheme need to be linked to a partition function, to specify where each partition will be stored. We can create a partition scheme linked to ordersPartFunc as:
— Step 2. Creating a Partition Scheme

CREATE PARTITION SCHEME ordersPartScheme
AS PARTITION ordersPartFunc
TO ([FileGroup2], [PRIMARY])

Note: A Partition scheme can only be created for file groups, and not individual data files.
Each file group defined in TO(<list of file groups>) corresponds to VALUES(<list of values>) in partition function. This scheme defines that:
1. [FileGroup2] will contain data for Values <= 2010 and,
2. [PRIMARY] will contain data for Values > 2010
We have now created the partition function and partition scheme, but we haven’t linked these to any tables yet. This can be done while creating the Table. A partition scheme can be applied to more than one table.

Step 3: Creating a Partitioned Table – Once he partition scheme is defined, a table can be created using the partition scheme. It is done by specifying the partition scheme in the “ON” clause while creating the table. Only partition scheme need to be specified, you don not need to specify a partition function as this is already defined in partition scheme:
— © 2011 – Vishal (http://SqlAndMe.com)

— Step 3. Creating a Partitioned Table

CREATE TABLE Table_Orders
(
      OrderID     INT,
      Name        VARCHAR(20),
      OrderDate   DATE
)
ON ordersPartScheme (OrderDate)
That’s all folks, data inserted to ‘Table_Orders’ will be partitioned according to ordersPartFunc, and will be stored according to ordersPartScheme.
Let’s insert some data to check if the partitions are created successfully.
INSERT INTO Table_Orders (OrderID, Name, OrderDate)
VALUES (1, 'Vishal', '2011-07-22')
— Will go to Partition 2 -> [PRIMARY] File Group

INSERT INTO Table_Orders (OrderID, Name, OrderDate)
VALUES (2, 'Vishal', '2010-06-22')
— Will go to Partition 1 -> [FileGroup2] File Group

INSERT INTO Table_Orders (OrderID, Name, OrderDate)
VALUES (3, 'Vishal', '2009-05-22')
— Will go to Partition1 -> [FileGroup2] File Group

SELECT      OrderID, Name, OrderDate
FROM        Table_Orders
Result Set:
OrderID     Name                 OrderDate
———– ——————– ———-
3           Vishal               2009-05-22
1           Vishal               2011-07-22
2           Vishal               2010-06-22

(3 row(s) affected)
You can use sys.partitions catalog view to check number of partitions and rows for a table as below:
SELECT      partition_id, object_id, partition_number, rows
FROM        sys.partitions
WHERE       object_id = OBJECT_ID('Table_Orders')
Result Set:
partition_id         object_id   partition_number rows
——————– ———– —————- ——————–
72057594038910976    5575058     1                1
72057594038976512    5575058     2                2

(2 row(s) affected)
sys.partitions can tell how many rows are present in each partition, you can use $PARTITION function to identify a partition for a row. $PARTITION can be used $PARTITION.partition_function_name(expression):
SELECT      OrderID, Name, OrderDate,
            $PARTITION.ordersPartFunc(OrderDate)
            AS 'Partition'
FROM  Table_Orders
Result Set:
OrderID     Name                 OrderDate  Partition
———– ——————– ———- ———–
3           Vishal               2009-05-22 1
1           Vishal               2011-07-22 2
2           Vishal               2010-06-22 2

(3 row(s) affected)

Wednesday, 2 December 2015

Why Use Both Partitioned Tables and Indexes?

SQL Server partitioned tables are a way to spread a single table over multiple partitions, and while doing so each partition can be on a separate filegroup. Following are several reasons for doing this:
  • Faster and easier data loading: If your database has a large amount of data to load, you might want to consider using a partitioned table. “A large amount of data,” doesn’t mean a specific amount of data, but any case in which the load operation takes longer than is acceptable in the production cycle. A partitioned table enables you to load the data to an empty table that’s not in use by the “live” data, so it has less impact on concurrent live operations. Clearly, there will be an impact on the I/O subsystem, but if you also have separate filegroups on different physical disks, even this has a minimal impact on overall system performance. After the data is loaded to the new table, you can perform a switch to add the new table to the live data. This switch is a simple metadata change that quickly executes, which is why partitioned tables are a great way to load large amounts of data with limited impact to users who touch the rest of the data in the table.
  • Faster and easier data deletion or archival: For the same reasons, partitioned tables also help you to delete or archive data. If your data is partitioned on boundaries that are also the natural boundaries on which you add or remove data, the data is considered to be aligned. When your data is aligned, deleting or archiving data is as simple as switching a table out of the current partition, after which you can unload or archive it at your leisure. There is a bit of a catch to this part: With archiving, you often want to move the old data to slower or different storage. The switch operation is so fast because all it does is change metadata. It doesn’t move any data around, so to actually move the data from the filegroup where it lived to the old, slow disk archival filegroup, you need to move the data, but you move it when the partition isn’t attached to the existing partitioned table. Therefore, although this may take quite some time, it can have a minimal impact on any queries executing against the live data.
  • Faster queries: You are probably interested in an opportunity to get faster queries. When querying a partitioned table, the query optimizer can eliminate searching through partitions that it knows won’t hold any results. This is referred to as partition elimination. This works only if the data in the partitioned table or index is aligned with the query. That is, the data must be distributed among the partitions in a way that matches the search clause on the query. You learn more details about this as you consider how to create a partitioned table. SQL Server 2008 offers some improvements for parallel query processing enhancements on partitioned tables and indexes.
  • Sliding windows: A sliding window is basically what was referred to earlier in the discussion about adding new data and then deleting or archiving old data. What you did was fill a new table, switch it into the live table, and then switch an existing partition out of the live table for archival or deletion. It’s kind of like sliding a window of new data into the current partitioned table, and then sliding an old window of data out of the partitioned table.
Creating Partitioned Tables
Table partitioning requires SQL Server 2012 Enterprise Edition. There are also some expectations about the hardware in use, in particular the storage system; although these are implicit expectations, and you can store the data anywhere you want. You just won’t get the same performance benefits you would get if you had a larger enterprise storage system with multiple disk groups dedicated to different partitions.
SQL Server 2012 supports up to 15,000 partitions by default and is fully supported in 64-bit systems. In 32-bit systems, it is possible to create more than 1,000 table or index partitions, but it is not fully supported.
To create a partitioned table or index, perform the following steps:
1.   Specify how the table or index is partitioned by the partitioning column, and the range of values included for each partition. Only one partitioning column can be specified. For example, to create four partitions based on a DateKey column, you execute the following command:
Either LEFT or RIGHT boundaries can be specified in the partition function. If no partition boundary is specified LEFT is used as default. Table 14-3 describes the partitions created by the preceding partition function.
Table 14-3: Partition Results for DateKeyRange Function.
Partition No.
Description
1
All records with DateKey <= 20021231
2
Records between Datekey>20021231 and Datekey<=20031231
3
Records between Datekey>20031231 and Datekey<=20041231
4
All records with DateKey > 20041231
2.   To determine the partition number where a record will be placed based on the DateKey column value, use the $PARTITION function as follows:

3.   Now create a partition scheme. For example, create a partition scheme with four filegroups that can be used to hold the four partitions defined in the DateKeyRange_PF partition function as follows: