- SET STATISTICS IO
- SET STATISTICS TIME
- Parser
- Biding
- Execution Plan Generation
- OPtimization
- Components of the Execution Plan
- Ageing of the Execution Plan
- Analysing the Execution Plan Cache
- Execution Plan Reuse
- AdHoc Workload
- Optimize for an AdHoc Workload
- Froced Parmeterization
- Benefites of Stored Procedures
- sp_executesql
- Parameter Sniffing
- Query Plan Hash and Query Hash
- Execution Plan Cache Recommendations
- Actual Vs. Estimated Execution Plans
Sunday, June 28, 2020
SQL Server Performance Tuning and Query Optimization guidelines
SQL Server Database Design guidelines
- Balance Under-and-Over normalization
- Benefiting from Entity-Integrity Constraints
- Benefiting from Domain and Referential Integrity Constraints
- Adopting Index-Design Best Practices
- Avoiding the use of the sp_Prefix fpr Stored Procedure Names
- Minimizing the use of Triggers
SQL Server Query Design guidelines
- SET NOCOUNT ON
- Explicitly Define the Owner of an Object
- Avoid Non-sargable Search Condition
- Avoid Arithmatic Expression on the WHERE Clause Column
- Avoid Optimizer Hints
- Stay away from Nesting Views
- Ensure No Implicit Data Type Conversions
- Minimize Logging Overhead
- Adopt Best Practices for Reusing Execution Plans
- Adopt Best Practices for Database Tranactions
- Eliminate or Reduce the Overhead of Database Cursors
Performance Killer in SQL Server
- Poor Indexing
- Inaccurate Statistics
- Poor Query Design
- Poor Executaion Plans (usually caused by bad parameter sniffing)
- Excessive blocking and deadlocks
- Non set based operations (usually T-SQL cursors)
- Poor Database Design
- Excessive Fragmation
- Nonreusable Execution plans
- Frequnt recompilation of queries
- Improper configuration of the database log
- Improper usages of cursor
- Excessive or improper configuration of tempdb
Tuesday, June 16, 2020
SQL Server Versions, Editions and Database size capacity
First you see which SQL Server you used
SQL Server is available in various editions. This chapter lists the multiple editions with its features.
-
Enterprise − This is the top-end edition with a full feature set.
-
Standard − This has less features than Enterprise, when there is no requirement of advanced features.
-
Workgroup − This is suitable for remote offices of a larger company.
-
Web − This is designed for web applications.
-
Developer − This is similar to Enterprise, but licensed to only one user for development, testing and demo. It can be easily upgraded to Enterprise without reinstallation.
-
Express − This is free entry level database. It can utilize only 1 CPU and 1 GB memory, the maximum size of the database is 10 GB.
-
Compact − This is free embedded database for mobile application development. The maximum size of the database is 4 GB.
-
Datacenter − The major change in new SQL Server 2008 R2 is Datacenter Edition. The Datacenter edition has no memory limitation and offers support for more than 25 instances.
-
Business Intelligence − Business Intelligence Edition is a new introduction in SQL Server 2012. This edition includes all the features in the Standard edition and support for advanced BI features such as Power View and PowerPivot, but it lacks support for advanced availability features like AlwaysOn Availability Groups and other online operations.
-
Enterprise Evaluation − The SQL Server Evaluation Edition is a great way to get a fully functional and free instance of SQL Server for learning and developing solutions. This edition has a built-in expiry of 6 months from the time that you install it.
Now see the mostly used SQL Server database size capacity as per Editions.
SQL Server Enterprise Edition - 524 PB
(PetaByte)
SQL Server Standrad Edition - 524 PB (PetaByte)
SQL Server BI Edition - 524 PB (PetaByte)
SQL Server Express Edition - 10 GB (GigaByte)
SQL Server Enterprise Evalution Version Period for - 180 days
Now see the Evaluation version Create Date and Expiry Date.
SELECT create_date AS 'Installation Date', DATEADD(DD, 180, create_date) AS 'Expiry Date'
FROM sys.server_principalsWHERE NAME = 'NT AUTHORITY\SYSTEM'Now see the Developer version Create Date and Expire Date.
DECLARE @edition SQL_VARIANT
SELECT @edition = SERVERPROPERTY('Edition')IF (@edition = 'Enterprise Evaluation Edition' OR
@edition = 'Enterprise Evaluation Edition (64-bit)')BEGIN SELECT create_date AS 'SQL Server Installation Date', DATEADD(dd, 180, create_date) AS 'Expiry Date' FROM sys.server_principals WHERE NAME = 'NT AUTHORITY\SYSTEM'
ENDELSEBEGIN print 'Now you running ' + convert(VARCHAR(100), SERVERPROPERTY('Edition')) + ' which won''t expire.'END
Wednesday, June 3, 2020
Data Mask in SQL Server
USE tempdb
GO
-- Create a sample table [dbo].[Customer] with masked columns:
CREATE TABLE dbo.Customer (
CustomerID INT IDENTITY PRIMARY KEY,
FirstName VARCHAR(250),
LastName VARCHAR(250) MASKED WITH (FUNCTION = 'default()') NULL,
PhoneNumber VARCHAR(12) MASKED WITH (FUNCTION = 'partial(1,"XXXXXXXXX",0)') NULL,
Email VARCHAR(100) MASKED WITH (FUNCTION = 'email()') NULL,
CreditCardNo VARCHAR(16) MASKED WITH (FUNCTION = 'partial(4,"XXXXXXXXXX",2)') NULL,
);
-- Now insert some test records (fictitious figures):
INSERT INTO dbo.Customer (FirstName, LastName, PhoneNumber, Email, CreditCardNo) VALUES
('Arka', 'Gupta', '7001061958', 'arkagupto@gmail.com', '4563123456789012'),
('Rahul', 'Singh', '9842359873', 'rahul@gmail.com', '123456789092'),
('Visal', 'Sharma', '6700091129', 'visal@gmail.com', '567890123469'),
('Rajib', 'Agrawal', '9800002000', 'rajib@gmail.com', '345678901224');
-- 1. Now let’s create a Test Account and just Grant Read access to [dbo].[Customer] table:
CREATE USER user1 WITHOUT LOGIN;
GO
GRANT SELECT ON dbo.Customer TO user1;
GO
EXECUTE AS USER = 'user1';
SELECT * FROM dbo.Customer;
REVERT;
GO
-- 2. Removing Masking from a column by simple ALTER TABLE/COLUMN statement:
ALTER TABLE dbo.Customer
ALTER COLUMN LastName DROP MASKED;
GO
-- Let's check the table data again:
EXECUTE AS USER = 'user1';
SELECT * FROM dbo.Customer;
REVERT;
GO
-- 3. Granting the UNMASK permission to “user1”:
GRANT UNMASK TO user1;
GO
-- Let's check the table data again:
EXECUTE AS USER = 'user1';
SELECT * FROM dbo.Customer;
REVERT;
GO
-- 4. REVOLE UNMASK TO user1:
REVOKE UNMASK TO user1;
GO
EXECUTE AS USER = 'user1';
SELECT * FROM dbo.Customer;
REVERT;
GO
-- 5. FINAL CLEANUP:
DROP TABLE dbo.Customer;
GO
DROP USER [user1]
GO
Saturday, May 23, 2020
Datetime Picker control in VB.Net DataGridView Row
Imports System
Imports System.Windows.Forms
Public Class GridDateControl
Inherits
DataGridViewColumn
Public Sub New()
MyBase.New(New CalendarCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return
MyBase.CellTemplate
End Get
Set(ByVal value As
DataGridViewCell)
' Ensure
that the cell used for the template is a CalendarCell.
If Not (value Is Nothing) AndAlso _
Not
value.GetType().IsAssignableFrom(GetType(CalendarCell))
_
Then
Throw
New InvalidCastException("Must be a CalendarCell")
End
If
MyBase.CellTemplate
= value
End Set
End Property
End Class
Public Class CalendarCell
Inherits
DataGridViewTextBoxCell
Public Sub New()
' Use the
short date format.
Me.Style.Format
= "d"
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal
initialFormattedValue As Object, _
ByVal
dataGridViewCellStyle As DataGridViewCellStyle)
' Set the
value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex,
initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl
As CalendarEditingControl = _
CType(DataGridView.EditingControl,
CalendarEditingControl)
If Not Me.Value Is DBNull.Value Then
If Not Me.Value Is Nothing Then
ctl.Value = CType(Me.Value,
DateTime)
End
If
End If
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return
the type of the editing contol that CalendarCell uses.
Return GetType(CalendarEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return
the type of the value that CalendarCell contains.
Return
GetType(DateTime)
End Get
End Property
'Public Property
CustomFocus() As Boolean
' Get
' End Get
' Set(ByVal value As Boolean)
' If CustomFocus Then
' ctl.Select()
' SendKeys.Send("%{Down}")
' End If
' End Set
'End Property
End Class
Class CalendarEditingControl
Inherits
DateTimePicker
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private
valueIsChanged As Boolean
= False
Private
rowIndexNum As Integer
Public Sub New()
Me.Format
= DateTimePickerFormat.Short
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements
IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return
Me.Value.ToShortDateString()
End Get
Set(ByVal value As Object)
If TypeOf value Is
[String] Then
Me.Value
= DateTime.Parse(CStr(value))
End
If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context _
As
DataGridViewDataErrorContexts) As Object _
Implements
IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Value.ToShortDateString()
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements
IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font
= dataGridViewCellStyle.Font
Me.CalendarForeColor =
dataGridViewCellStyle.ForeColor
Me.CalendarMonthBackground
= dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements
IDataGridViewEditingControl.EditingControlRowIndex
Get
Return
rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal
dataGridViewWantsInputKey As Boolean) As Boolean _
Implements
IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the
DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case
Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Home, Keys.End,
Keys.PageDown, Keys.PageUp
Return
True
Case
Else
Return
False
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements
IDataGridViewEditingControl.PrepareEditingControlForEdit
' No
preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return
False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements
IDataGridViewEditingControl.EditingControlDataGridView
Get
Return
dataGridViewControl
End Get
Set(ByVal value As
DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements
IDataGridViewEditingControl.EditingControlValueChanged
Get
Return
valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements
IDataGridViewEditingControl.EditingPanelCursor
Get
Return
MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the
DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class