יום שלישי, 26 בינואר 2010

Troubleshooting Performance Problems in SQL Server 2008




It’s not uncommon to experience the occasional slowdown of a database running the Microsoft SQL Server database software. The reasons can range from a poorly designed database to a system that is improperly configured for the workload. As an administrator, you want to proactively prevent or minimize problems; if they occur, you want to diagnose the cause and take corrective actions to fix the problem whenever possible. This white paper provides step-by-step guidelines for diagnosing and troubleshooting common performance problems by using publicly available tools such as:

* SQL Server Profiler
* System Monitor (in the Windows Server 2003 operating system) or Performance Monitor (in the Windows Vista operating system and Windows Server 2008), also known as Perfmon
* Dynamic management views (sometimes referred to as DMVs)
* SQL Server Extended Events (Extended Events) and the data collector, which are new in SQL Server 2008.

We have limited the scope of this white paper to the problems commonly seen by Microsoft Customer Service and Support (CSS), because an exhaustive analysis of all possible problems is not feasible.


http://download.microsoft.com/download/D/B/D/DBDE7972-1EB9-470A-BA18-58849DB3EB3B/TShootPerfProbs2008.docx

יום שלישי, 19 בינואר 2010

SQL Server 2008 R2 gets an official date


בוקר טוב,
חברת מייקרוסופט הודיעה כי גרסה רשמית של פלטפורמת ה- sql server הבאה - SQL Server 2008 R2 תיהיה זמינה במאי 2010.

יכולות הפלטפורמה החדשה הינם רבות וישנם יכולות חדשות בתחום business intelligence ושרידות בסיס הנתונים.
מייקרוסופט מציינת כי מאז שיצאה גרסת הבטא של המוצר - CTP באוגוסט 2009 היו מעל ל- 150,000 הורדות.

לפני מספר שבועות פירסמתי טיפ אשר פורסם גם באתר מייקרוסופט ובו סקירה אודות היכולות החדשות של הפלטפורמה החדשה:
http://itaibinyamin.blogspot.com/2009/12/sql-server-2008-r2.html

להלן קישור בנוגע להודעה של MC בנוגע לזמינות הגרסה:
http://blogs.technet.com/dataplatforminsider/archive/2010/01/19/sql-server-2008-r2-gets-an-official-date.aspx

בהצלחה.

יום רביעי, 6 בינואר 2010

Trigger with insert select

שלום רב,

עלתה דרישה אצל אחד מהלקוחות שלי שאחרי כל הכנסת לקוח בטבלת הלקוחות תרוץ פרוצדורה שמקבלת את מספר הלקוח ושולחת לו מייל עם פרטי ההרשמה.

במבט ראשון, הפתרון הינו פשוט :

נקים טריגר על הטבלה , שאחרי כל Insert לטבלת הלקוחות , הטריגר יקבל את מספר הלקוח מטבלת המערכת - inserted שבטריגר ויעביר אותה לפרוצדורה.

בדרך כלל הטריגר יראה כך:

CREATE TRIGGER [dbo].[tri_TRIGGER_test_insert]
ON [dbo].[t_test_trigger]
AFTER INSERT
AS
declare @id int
select @id = id from inserted
exec p_send_mail @id
GO

הטריגר יעבוד מצויין להכנסה בודדת של לקוחות לטבלה, אך שימו לב מה יקרה כאשר אני אבצע הכנסה של מספר רשומות בו זמנית:

ראשית ניצור טבלה:
CREATE table t_test_trigger
( id int)
go

להלן טריגר אשר אחרי כל הכנסת רשומה ידפיס את המספר הלקוח שהוכנס:
alter TRIGGER [dbo].[tri_TRIGGER_test_insert]
ON [dbo].[t_test_trigger]
AFTER INSERT
AS
declare @id int
select @id = id from inserted
select @id
GO

במידה ונבצע הכנסת נתונים בדרך הבאה, הכל יעבוד כשורה והטריגר יקפוץ 3 פעמים:
insert into t_test_trigger values (1)
insert into t_test_trigger values (2)
insert into t_test_trigger values (3)

אך שימו לב שאם נבצע הכנסת נתונים בו זמנית, הטריגר יקפוץ פעם אחד בלבד:
insert into t_test_trigger
select *
from t_test_trigger

במקרה שכזה הפרוצדורה תרוץ רק פעם אחת, ורק לקוח אחד יקבל מייל...

פתרון:
במקרים בהם אנו יודעים שאופי הכנסת הנתונים מול הטבלה הינה גם על ידי
insert and select
הפתרון הינו לשים לולאה בתוך הטריגר אשר תרוץ על נתוני טבלת ה- inserted וכך הפרוצדורה שלנו תרוץ על כל הרשומות.

לדוגמא:
alter TRIGGER [dbo].[tri_TRIGGER_test_insert]
ON [dbo].[t_test_trigger]
AFTER INSERT
AS

DECLARE @id int
DECLARE uniques_cursor CURSOR FOR

select id from inserted

OPEN uniques_cursor
FETCH NEXT
FROM uniques_cursor
INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN

select @id

FETCH NEXT
FROM uniques_cursor
INTO @id
END
CLOSE uniques_cursor
DEALLOCATE uniques_cursor

GO


מוסר השכל - לפני כתיבת הקוד ויישום פתרון בבסיס הנתונים ובכלל חשוב לבצע מיפוי ואפיון לעומק על מנת להבין ולמצוא גם את מקרי הקצה - וזאת על מנת למנוע באגים !

בהצלחה.