Tuesday, November 24, 2009

Optimizing a query with Forced Parameterization

Enabling Forced Parameterization

-- enable forced mode
ALTER DATABASE YOUR_DATABASE SET PARAMETERIZATION FORCED

-- enable simple mode
ALTER DATABASE YOUR_DATABASE SET PARAMETERIZATION SIMPLE


You can also use the Management Console and the properties option
under the relevant database to switch the setting over manually.
When switching from one mode to another I made sure to clear the
current cache by running the following:
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE


To check the queries that were currently cached and the
type of method being engaged.

SELECT *
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
INNER JOIN sys.dm_exec_cached_plans as cp
on
qs.plan_handle=cp.plan_handle
WHERE cp.plan_handle=qs.plan_handle
AND (ObjType = 'Adhoc' OR ObjType='Prepared')


Enabling Forced Parameterization for a single query

There maybe situations where you want to enable this
feature for one or more queries rather than set it
on for the whole database. You can do this by
setting up a plan guide for the query in question.
An easy way to do this is to run the following SQL
making sure to replace the first parameter value with
the SQL you want to parameterize.


DECLARE @stmt nvarchar(max);
DECLARE @params nvarchar(max);
EXEC sp_get_query_template
N'SELECT * FROM JOBS WHERE Row between 1 AND 20;',
@stmt OUTPUT,
@params OUTPUT;
EXEC sp_create_plan_guide
N'MyPlanGuide_JobResults',
@stmt,
N'TEMPLATE',
NULL,
@params,
N'OPTION(PARAMETERIZATION FORCED)';


Friday, November 13, 2009

VB - Drag and Drop within grid (Reorder Rows)

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'Populate the grid.
Me.DataGridView1.Rows.Add("Jan", "1")
Me.DataGridView1.Rows.Add("Feb", "2")
Me.DataGridView1.Rows.Add("Mar", "3")
Me.DataGridView1.Rows.Add("Apr", "4")
End Sub
Private Sub DataGridView1_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
Dim hti = Me.DataGridView1.HitTest(e.X, e.Y)
If hti.Type = DataGridViewHitTestType.RowHeader Then
Me.DataGridView1.DoDragDrop(Me.DataGridView1.Rows(hti.RowIndex), DragDropEffects.Move)
End If
End Sub
Private Sub DataGridView1_DragEnter(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
If Me.CanDrop(sender, e) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub DataGridView1_DragOver(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver
If Me.CanDrop(sender, e) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub DataGridView1_DragDrop(ByVal sender As Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
If Me.CanDrop(sender, e) Then
Dim rowToMove = DirectCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
Dim location = Me.DataGridView1.PointToClient(New Point(e.X, e.Y))
Dim rowBounds As Rectangle
Dim rows = Me.DataGridView1.Rows
For rowIndex = Me.DataGridView1.RowCount - 1 To 0 Step -1
If Not rows(rowIndex).IsNewRow Then
rowBounds = Me.DataGridView1.GetRowDisplayRectangle(rowIndex, False)
If location.Y >= rowBounds.Y Then
If rowIndex <> rowToMove.Index Then
Dim newIndex = rowIndex
If rowToMove.Index <>
If location.Y > rowBounds.Y + rowBounds.Height \ 2 Then
newIndex += 1
End If
'Move the row to the new position.
rows.Remove(rowToMove)
rows.Insert(newIndex, rowToMove)
End If
Exit For
End If
End If
Next
End If
End Sub
Private Function CanDrop(ByVal sender As Object, ByVal e As DragEventArgs) As Boolean
Dim data = e.Data
Dim dataType = GetType(DataGridViewRow)
'Data can only be dropped if it is a row from the same DataGridView.
Return data.GetDataPresent(dataType) AndAlso _
DirectCast(data.GetData(dataType), DataGridViewRow).DataGridView Is sender
End Function
End Class

Wednesday, November 11, 2009

Find Name of The SQL Server Instance

SELECT @@SERVERNAME AS 'Server Name'