SlideShare una empresa de Scribd logo
1 de 16
סודות האופטימייזר מיכאל זילברשטיין| DBArt LTD Michael@DBArt.co.il
SQL Server components תהליך Query Processing.  נא להכיר – אופטימייזר ותפקידו בכוח. שלבי האופטימיזציה. Query Execution and Memory איפה אנחנו נופלים? סיכום.
שאלת טיזר
SQL Server components
תהליך Query Processing
נא להכיר – אופטימייזר ותפקידו בכוח תפקידו של אופטימייזר – למצוא execution plan יעיל לכל שאילתה בפרוצדורה או ב-batch. Cost-based Optimizer. כלומר מחפש execution plan הכי "זול". מטרה: להחזיר את כל התוצאה כמה שיותר מהר. למצוא execution plan הכי יעיל שיש יכול לקחת זמן. לכן מחפש איזון בין תהליך האופטימיזציה לבין ביצוע.
Trivial plan איטרציה ראשונה (cost < 0.2) איטרציה שנייה (cost < 1.0) איטרציה שלישית אם (cost > 5) – לשקול parallel plans שלבי האופטימיזציה
***Trace flag 2301  מוסיף ל-Optimizer יכולות (אופציות) נוספות. על חשבון זמן אופטימיזציה! להשתמש בזהירות!!! "Went live with 2008, -T2301 killed us"  Brent Ozar שלבי האופטימיזציה
Query Execution and Memory חלק מהפעולות אינן דורשות זכרון (nested loops, merge, filter וכו'). חלק אחר דווקא כן (hash join, sort, spool) הקצאת זכרון מתבצעת בתחילת ביצוע השאילתה על-סמך חישוב שמתבסס על ה-execution plan שאופטימייזר הכין. מה קורה כאשר פתאום מגלים שצריך עוד זכרון? כותבים ל-tempdb! מה אם אין מספיק זכרון פנוי? השאילתה תמתין. RESOURCE_SEMAPHORE
איפה אנחנו נופלים?
איפה אנחנו נופלים?
ברוב רובם של המקרים אופטימייזר עושה עבודה מעולה. וגם ממשיך להשתפר מגרסה לגרסה. יש מספר מקרים בהם אופטימייזר לא מצליח למצוא execution plan טוב. חלקם בגלל מגבלותיו, חלקם בגלל באגים, חלקם בגלל התפלגות נתונים שונה ומשונה. כדאי להכיר גם את מגבלותיו של האופטימייזר אבל בעיקר – התפלגות נתונים במסד נתונים שלכם. May the (Optimizer’s) force be with you
זוכרים Query cost? ,[object Object]
מספרים קבועים לכל שורה או page אך שונים מאופרטור לאופרטור. למשל עבור Clustered Index Scan:
CPU cost עבור שורה ראשונה = 0.0001581
CPU cost עבור כל שורה משניה והלה = 0.0000011

Más contenido relacionado

Similar a 4 sql explore סודות האופטימייזר

Putting the Enterprise into the Enterprise System by Thomas H. Davenport
Putting the Enterprise intothe Enterprise System by Thomas H. DavenportPutting the Enterprise intothe Enterprise System by Thomas H. Davenport
Putting the Enterprise into the Enterprise System by Thomas H. DavenportAlon Lahav
 
Final project presentation
Final project presentationFinal project presentation
Final project presentationOded Sugarman
 
לימור רויסמן 2017
לימור רויסמן 2017לימור רויסמן 2017
לימור רויסמן 2017Limor Roisman
 
מפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFX
מפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFXמפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFX
מפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFXAutoSysfx
 
Powershell - ISUG 99 (Aviad Deri)
Powershell  - ISUG 99 (Aviad Deri)Powershell  - ISUG 99 (Aviad Deri)
Powershell - ISUG 99 (Aviad Deri)sqlserver.co.il
 
הצגת עבודה הנדסת אנוש
הצגת עבודה   הנדסת אנושהצגת עבודה   הנדסת אנוש
הצגת עבודה הנדסת אנושtalyasadon
 
מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017Igor Kleiner
 
6 sql explorer - powershell dba
6   sql explorer - powershell dba6   sql explorer - powershell dba
6 sql explorer - powershell dbasqlserver.co.il
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Ram Kedem
 
Agile, XP and Scrum
Agile, XP and ScrumAgile, XP and Scrum
Agile, XP and ScrumAlon Lahav
 
Stack overflow - by avi halif
Stack overflow - by avi halifStack overflow - by avi halif
Stack overflow - by avi halifAviHalif
 

Similar a 4 sql explore סודות האופטימייזר (11)

Putting the Enterprise into the Enterprise System by Thomas H. Davenport
Putting the Enterprise intothe Enterprise System by Thomas H. DavenportPutting the Enterprise intothe Enterprise System by Thomas H. Davenport
Putting the Enterprise into the Enterprise System by Thomas H. Davenport
 
Final project presentation
Final project presentationFinal project presentation
Final project presentation
 
לימור רויסמן 2017
לימור רויסמן 2017לימור רויסמן 2017
לימור רויסמן 2017
 
מפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFX
מפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFXמפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFX
מפגש סוחרים - הדרכה על מסחר אוטומטי מאת אלירן כהן AutoSysFX
 
Powershell - ISUG 99 (Aviad Deri)
Powershell  - ISUG 99 (Aviad Deri)Powershell  - ISUG 99 (Aviad Deri)
Powershell - ISUG 99 (Aviad Deri)
 
הצגת עבודה הנדסת אנוש
הצגת עבודה   הנדסת אנושהצגת עבודה   הנדסת אנוש
הצגת עבודה הנדסת אנוש
 
מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 2: 2017
 
6 sql explorer - powershell dba
6   sql explorer - powershell dba6   sql explorer - powershell dba
6 sql explorer - powershell dba
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)
 
Agile, XP and Scrum
Agile, XP and ScrumAgile, XP and Scrum
Agile, XP and Scrum
 
Stack overflow - by avi halif
Stack overflow - by avi halifStack overflow - by avi halif
Stack overflow - by avi halif
 

Más de sqlserver.co.il

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013sqlserver.co.il
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cachesqlserver.co.il
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013sqlserver.co.il
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserversqlserver.co.il
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012sqlserver.co.il
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum versionsqlserver.co.il
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3sqlserver.co.il
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2sqlserver.co.il
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1sqlserver.co.il
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Eventssqlserver.co.il
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoresqlserver.co.il
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACsqlserver.co.il
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatialsqlserver.co.il
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkelsqlserver.co.il
 

Más de sqlserver.co.il (20)

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cache
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013
 
DAC 2012
DAC 2012DAC 2012
DAC 2012
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserver
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum version
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStore
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DAC
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatial
 
מיכאל
מיכאלמיכאל
מיכאל
 
נועם
נועםנועם
נועם
 
עדי
עדיעדי
עדי
 
מיכאל
מיכאלמיכאל
מיכאל
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 

4 sql explore סודות האופטימייזר

  • 1. סודות האופטימייזר מיכאל זילברשטיין| DBArt LTD Michael@DBArt.co.il
  • 2. SQL Server components תהליך Query Processing. נא להכיר – אופטימייזר ותפקידו בכוח. שלבי האופטימיזציה. Query Execution and Memory איפה אנחנו נופלים? סיכום.
  • 6. נא להכיר – אופטימייזר ותפקידו בכוח תפקידו של אופטימייזר – למצוא execution plan יעיל לכל שאילתה בפרוצדורה או ב-batch. Cost-based Optimizer. כלומר מחפש execution plan הכי "זול". מטרה: להחזיר את כל התוצאה כמה שיותר מהר. למצוא execution plan הכי יעיל שיש יכול לקחת זמן. לכן מחפש איזון בין תהליך האופטימיזציה לבין ביצוע.
  • 7. Trivial plan איטרציה ראשונה (cost < 0.2) איטרציה שנייה (cost < 1.0) איטרציה שלישית אם (cost > 5) – לשקול parallel plans שלבי האופטימיזציה
  • 8. ***Trace flag 2301 מוסיף ל-Optimizer יכולות (אופציות) נוספות. על חשבון זמן אופטימיזציה! להשתמש בזהירות!!! "Went live with 2008, -T2301 killed us" Brent Ozar שלבי האופטימיזציה
  • 9. Query Execution and Memory חלק מהפעולות אינן דורשות זכרון (nested loops, merge, filter וכו'). חלק אחר דווקא כן (hash join, sort, spool) הקצאת זכרון מתבצעת בתחילת ביצוע השאילתה על-סמך חישוב שמתבסס על ה-execution plan שאופטימייזר הכין. מה קורה כאשר פתאום מגלים שצריך עוד זכרון? כותבים ל-tempdb! מה אם אין מספיק זכרון פנוי? השאילתה תמתין. RESOURCE_SEMAPHORE
  • 12. ברוב רובם של המקרים אופטימייזר עושה עבודה מעולה. וגם ממשיך להשתפר מגרסה לגרסה. יש מספר מקרים בהם אופטימייזר לא מצליח למצוא execution plan טוב. חלקם בגלל מגבלותיו, חלקם בגלל באגים, חלקם בגלל התפלגות נתונים שונה ומשונה. כדאי להכיר גם את מגבלותיו של האופטימייזר אבל בעיקר – התפלגות נתונים במסד נתונים שלכם. May the (Optimizer’s) force be with you
  • 13.
  • 14. מספרים קבועים לכל שורה או page אך שונים מאופרטור לאופרטור. למשל עבור Clustered Index Scan:
  • 15. CPU cost עבור שורה ראשונה = 0.0001581
  • 16. CPU cost עבור כל שורה משניה והלה = 0.0000011
  • 17. IO cost עבור page ראשון = 0.003125
  • 18. IO cost עבור כל page משניה והלה = 0.00074074מסכנה: אפשר לסמוך על estimated cost רק בערבון מוגבל.
  • 19. References Itzik Ben-Gan “Inside Microsoft SQL Server 2005 T-SQL Querying” http://www.benjaminnevarez.com/ http://blogs.msdn.com/b/psssql/archive/2011/04/29/why-does-this-query-consumes-so-much-cpu.aspx http://sqlblog.com/blogs/adam_machanic/archive/2011/04/13/how-queries-are-processed-a-month-of-activity-monitoring-part-13-of-30.aspx