Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Server 在企业级 环境下的应用 赵歌喃 SQL Server 资深讲师. Overview Integrating SQL Server with Your Enterprise Application Architecture Integrating SQL Server with.

Similar presentations


Presentation on theme: "SQL Server 在企业级 环境下的应用 赵歌喃 SQL Server 资深讲师. Overview Integrating SQL Server with Your Enterprise Application Architecture Integrating SQL Server with."— Presentation transcript:

1 SQL Server 在企业级 环境下的应用 赵歌喃 SQL Server 资深讲师

2 Overview Integrating SQL Server with Your Enterprise Application Architecture Integrating SQL Server with Your Enterprise Application Architecture Designing a Disaster Recovery Plan Designing a Disaster Recovery Plan

3 Integrating SQL Server with Your Enterprise Application Architecture Integrating SQL Server with Your Enterprise Application Architecture Introduction to Application Architecture Introduction to Application Architecture Query Processing and Performance Query Processing and Performance Batches and Transactions Batches and Transactions Building a Highly Available Application Building a Highly Available Application Debugging Availability Issues Debugging Availability Issues Managing Connections Managing Connections Using a Loosely Coupled Design Using a Loosely Coupled Design Clustering for Application Availability Clustering for Application Availability

4  Introduction to Application Architecture The N-Tier Application Model The N-Tier Application Model Presentation Services Presentation Services Business Logic Services Business Logic Services Data Services Data Services Cursors Cursors Cursor Best Practices Cursor Best Practices

5 The N-Tier Application Model Data Services Business Logic Services Presentation Services

6 Windows Client Applications Windows Client Applications Installed locally Installed locally Use group policy to ensure logic is always available Use group policy to ensure logic is always available Web and Intranet Applications Web and Intranet Applications Accessed using a browser Accessed using a browser Web site availability is an issue Web site availability is an issue

7 Business Logic Services Usually Implemented as Components Usually Implemented as Components Can be deployed locally or remotely Can be deployed locally or remotely Can be Implemented as Stored Procedures Can be Implemented as Stored Procedures Business logic availability becomes tied to database availability Business logic availability becomes tied to database availability

8 Data Services Database Provides Data Storage, Retrieval, and Modification Services Database Provides Data Storage, Retrieval, and Modification Services High Availability is Crucial to Most Enterprise Applications High Availability is Crucial to Most Enterprise Applications

9 Cursors Cursors Overview Cursors Overview Default Result Sets Default Result Sets Server-side Cursors Server-side Cursors Server API Cursors Server API Cursors Fast Forward Only Cursors Fast Forward Only Cursors Transact-SQL Server Cursors Transact-SQL Server Cursors API Client-side Cursors API Client-side Cursors Cursors and Locking Cursors and Locking

10 Cursor Best Practices Use Default Result Sets in Place of Cursors Use Default Result Sets in Place of Cursors Avoid the Usage of Server Cursors to Return a Few Rows at a Time Avoid the Usage of Server Cursors to Return a Few Rows at a Time Always use Optimistic Locking for Server Side Cursors Always use Optimistic Locking for Server Side Cursors Avoid Cursor Implicit Conversion Avoid Cursor Implicit Conversion Miscellaneous Miscellaneous

11  Query Processing and Performance Compiling and Executing SQL Compiling and Executing SQL Execution Plan Caching Execution Plan Caching Stored Procedures Stored Procedures Initial Processing of Stored Procedures Initial Processing of Stored Procedures Cached Stored Procedure Plans and Performance Cached Stored Procedure Plans and Performance Stored Procedure Recompilation Stored Procedure Recompilation Prepare-Execute Prepare-Execute sp_executesql sp_executesql Auto-Parameterization Auto-Parameterization

12 Compiling and Executing SQL

13 Execution Plan Caching Ad-Hoc Statements Are the First To Be Discarded from Cache Ad-Hoc Statements Are the First To Be Discarded from Cache A Query Can Reuse a Plan in Cache that it Matches Exactly A Query Can Reuse a Plan in Cache that it Matches Exactly Hash key based on SQL text and ANSI settings Hash key based on SQL text and ANSI settings The Lazywriter Ages Plans and Removes Them from Cache The Lazywriter Ages Plans and Removes Them from Cache Stored plans are managed much like data buffers Stored plans are managed much like data buffers Ad-Hoc Query Cost Is Zero, then Incremented when Reused Ad-Hoc Query Cost Is Zero, then Incremented when Reused Stored Procedure, Prepared Statement, or Auto Parameterized Statements Use Creation Cost Stored Procedure, Prepared Statement, or Auto Parameterized Statements Use Creation Cost Procedure Cache is Not Maintained Separately Procedure Cache is Not Maintained Separately

14 Stored Procedures Advantages of Stored Procedures Advantages of Stored Procedures Isolated business logic Isolated business logic Modular coding Modular coding Performance benefits to caching plans Performance benefits to caching plans Security buffer Security buffer Disadvantages of Procedures Disadvantages of Procedures Dynamic SQL Dynamic SQL Bad plans from cached procedures Bad plans from cached procedures DONE_IN_PROC Messages DONE_IN_PROC Messages

15 Initial Processing of Stored Procedures Compiled plan placed in unified cache Compilation Execution (first time or recompile) Resolution* Optimization Parsing Resolution CreationCreation sysobjects Name, type, etc. syscomments Text of object syscolumns Parameter list sysdepends Object dependencies sysobjects Name, type, etc. syscomments Text of object syscolumns Parameter list sysdepends Object dependencies

16 Cached Stored Procedure Plans and Performance Caching Plans Can Be a Performance Benefit for Stored Procedures Performing Repetitive Tasks Caching Plans Can Be a Performance Benefit for Stored Procedures Performing Repetitive Tasks Caching Plans Can Harm Performance when Inefficient Execution Results from a Cached Plan Caching Plans Can Harm Performance when Inefficient Execution Results from a Cached Plan Modular Code Is Key: Modular Code Is Key: When to recompile When to recompile When not to recompile When not to recompile

17 Stored Procedure Recompilation Manual Recompilation Manual Recompilation Automatic Recompilation Automatic Recompilation Inline Recompilation Inline Recompilation Temporary Tables and When Procedures are Recompiled Temporary Tables and When Procedures are Recompiled

18 Prepare-Execute Statements Can Be Prepared Once and Executed Many Times Statements Can Be Prepared Once and Executed Many Times A query plan is cached and a handle returned A query plan is cached and a handle returned Prepare-Execute Improved in SQL Server 2000 Prepare-Execute Improved in SQL Server 2000 One round trip to prepare and Execute One round trip to prepare and Execute One round trip for each Execute One round trip for each Execute One round trip to Unprepare, Prepare the next and Execute the next One round trip to Unprepare, Prepare the next and Execute the next Clients Must Unprepare to Avoid Harming Performance Clients Must Unprepare to Avoid Harming Performance ADO 2.6 calls sp_unprepare implicitly ADO 2.6 calls sp_unprepare implicitly

19 sp_executesql sp_executesql Plans Are Cached for Reuse by Multiple Users sp_executesql Plans Are Cached for Reuse by Multiple Users sp_executesql Is More Powerful When Using Parameter Markers sp_executesql Is More Powerful When Using Parameter Markers In ADO, Call sp_executesql As Ad-Hoc SQL to Minimize Round Trips In ADO, Call sp_executesql As Ad-Hoc SQL to Minimize Round Trips Use Execute('string') When Assigning Identifiers to Variables Use Execute('string') When Assigning Identifiers to Variables

20 Auto-Parameterization Auto-Parameterization is Attempted with SQL Language TDS Messages Auto-Parameterization is Attempted with SQL Language TDS Messages Only “ Safe ” Auto-Parameterized Plans Can Be Shared Only “ Safe ” Auto-Parameterized Plans Can Be Shared SQL Server is Conservative About Auto- Parameterization SQL Server is Conservative About Auto- Parameterization Inefficient Plans Can Result from Different Parameters Inefficient Plans Can Result from Different Parameters Detecting Auto-Parameterization Through Statistics Time and Profiler Detecting Auto-Parameterization Through Statistics Time and Profiler

21  Batches and Transactions Batches and Transactions Batches and Transactions Transactions Best Practices Transactions Best Practices

22 Batches and Transactions Autocommit Autocommit Explicit and Implicit Transaction Explicit and Implicit Transaction Transaction Nesting Transaction Nesting Statement,Transaction, and Batch Abort Statement,Transaction, and Batch Abort Aborting and Set Options Aborting and Set Options

23 Transactions Best Practices Keep Transactions Small Keep Transactions Small Transaction Isolation Level Transaction Isolation Level Don ’ t Span a Transaction to Multiple Batches Don ’ t Span a Transaction to Multiple Batches No User Interaction within transactions No User Interaction within transactions Transactions and Locking Transactions and Locking

24  Building a Highly Available Application Business Tier Error Handling Business Tier Error Handling SQL Server Error Handling SQL Server Error Handling Special Message Handling Situations Special Message Handling Situations Testing Testing

25 Business Tier Error Handling Use Error Handlers to Trap Errors Use Error Handlers to Trap Errors Log the Errors Log the Errors

26 SQL Server Error Handling SQL Server Errors and Messages SQL Server Errors and Messages User-Defined Errors User-Defined Errors Use @@ERROR and @@TRANCOUNT for Decisions Within an Application Use @@ERROR and @@TRANCOUNT for Decisions Within an Application Error Severity and @@error Error Severity and @@error Compile and Runtime Errors Compile and Runtime Errors Transaction Error Checking Transaction Error Checking

27 Special Message Handling Situations Deadlock Handling Deadlock Handling Cancel the batch and rollback trans Cancel the batch and rollback trans Resubmit the batch again Resubmit the batch again Query Timeout Handling Query Timeout Handling Rollbacks occurring in a trigger Rollbacks occurring in a trigger Lock Timeout Handling Lock Timeout Handling Constraint Violations Handling Constraint Violations Handling A Lost Connection is a Cancel Operation A Lost Connection is a Cancel Operation

28 Testing Test Throughout the Development Cycle Test Throughout the Development Cycle Load Testing Load Testing Component Testing Component Testing System Testing System Testing

29  Debugging Availability Issues Debugging Debugging Tracing Tracing

30 Debugging Reproduce the Problem Reproduce the Problem Log the Application During Failure Log the Application During Failure Compare Performance Metrics to Baseline Compare Performance Metrics to Baseline Use Built In Diagnostic Tools Use Built In Diagnostic Tools Determine the Source of the Failure Determine the Source of the Failure Create and Test a Fix Create and Test a Fix

31 Tracing Use Message Queuing to Log Traces Centrally Use Message Queuing to Log Traces Centrally Minimize the Impact of Tracing on the Application ’ s Performance Minimize the Impact of Tracing on the Application ’ s Performance Trace Before Major Activities Trace Before Major Activities

32 Lesson: Managing Connections Connection Pooling Connection Pooling COM+ Object Pooling COM+ Object Pooling

33 Connection Pooling Component Opens a Database Connection Component Opens a Database Connection When Closed, the Connection Is Placed in the Pool When Closed, the Connection Is Placed in the Pool Pooled Connections Can Be Re-used by Other Components Pooled Connections Can Be Re-used by Other Components

34 COM+ Object Pooling Object Pooling vs. Connection Pooling Object Pooling vs. Connection Pooling Speed Speed Database Connection Requests Database Connection Requests Incremental Cost Incremental Cost Manual Enlisting in a Transaction Manual Enlisting in a Transaction Turning Off Automatic Enlistment Turning Off Automatic Enlistment Disabling Automatic Connection Pooling Disabling Automatic Connection Pooling Implementing IObjectControl Implementing IObjectControl Transaction Specific Pools Transaction Specific Pools

35  Using a Loosely Coupled Design Microsoft Message Queuing Services Microsoft Message Queuing Services COM+ Queued Components COM+ Queued Components Asynchronous Scenarios Asynchronous Scenarios

36 Microsoft Message Queuing Services Data Is Passed Asynchronously Data Is Passed Asynchronously Multiple Servers Can Read from the Same Queue Multiple Servers Can Read from the Same Queue Providing load balancing and failover Providing load balancing and failover

37 COM+ Queued Components Create a Queued Component Create a Queued Component Mark COM+ application and interface as queued Mark COM+ application and interface as queued COM+ application must listen for messages COM+ application must listen for messages Call Methods on the Queued Component Call Methods on the Queued Component Use the queue moniker Use the queue moniker

38 Asynchronous Scenarios Data Update, Delete, or Insert Operations Data Update, Delete, or Insert Operations Continue order taking when database is unavailable Continue order taking when database is unavailable Not useful for data retrieval Not useful for data retrieval No Errors or Acknowledgements will be Returned No Errors or Acknowledgements will be Returned Use a response queue or COM+ event for error notification Use a response queue or COM+ event for error notification

39  Clustering for Application Availability Using a Web Farm for Presentation Services Using a Web Farm for Presentation Services Maintaining Application Server Availability Maintaining Application Server Availability Component Load Balancing Component Load Balancing Best Practices Best Practices

40 Using a Web Farm for Presentation Services Network Load Balancing

41 Maintaining Application Server Availability Use Stateless Components Use Stateless Components This allows dynamic load balancing This allows dynamic load balancing

42 Component Load Balancing Provided by Application Center Only Activations are Load Balanced Based on Response Time and Round-robin Routing Server (cluster) CoCreateInstance

43 Best Practices - 1 Avoid Using Poorly Coded Extended Stored Procedures Avoid Using Poorly Coded Extended Stored Procedures Disallow Loading COM Objects in Process with SQL Server Disallow Loading COM Objects in Process with SQL Server Always Create a Clustered Index on a Table Always Create a Clustered Index on a Table Always Use Index Tuning Wizard Extensively. Always Use Index Tuning Wizard Extensively. If Error Within a Transaction, Always Rollback the Transaction Explicitly If Error Within a Transaction, Always Rollback the Transaction Explicitly Always Stress Test at the Maximum Projected User Load Before Deployment Always Stress Test at the Maximum Projected User Load Before Deployment Avoid Using a Lot of Ad-Hoc SQL Statements Avoid Using a Lot of Ad-Hoc SQL Statements

44 Best Practices - 2 When a Client Gets Lock Timeout Error or Deadlock, Include Retry Logic in the Code When a Client Gets Lock Timeout Error or Deadlock, Include Retry Logic in the Code Set Transaction Isolation Level as Low as Possible Set Transaction Isolation Level as Low as Possible Keep Statistics Updated Keep Statistics Updated Remove Internal Page Fragmentation Remove Internal Page Fragmentation Reduce Index Fragmention Reduce Index Fragmention Minimize Use of Cursors Minimize Use of Cursors Keep Transactions Short and Have No User Interaction Within a Transaction Keep Transactions Short and Have No User Interaction Within a Transaction Let Applications Process the Result Sets as Quickly as Possible and to Completion Let Applications Process the Result Sets as Quickly as Possible and to Completion

45 Best Practices - 3 Ensure AutoClose Database Option Is NOT SET Ensure AutoClose Database Option Is NOT SET Avoid or Reduce the Need to AutoGrow the Database and Log Files Avoid or Reduce the Need to AutoGrow the Database and Log Files Use the Default sp_configure Settings Use the Default sp_configure Settings

46 Designing a Disaster Recovery Plan Designing a Disaster Recovery Plan Disaster Recovery Plan Basics Disaster Recovery Plan Basics Writing the Disaster Recovery Plan Writing the Disaster Recovery Plan Testing and Using the Disaster Recovery Plan Testing and Using the Disaster Recovery Plan

47  Disaster Recovery Plan Basics What Is a Disaster Recovery Plan? What Is a Disaster Recovery Plan? System Design Considerations for Disaster Recovery System Design Considerations for Disaster Recovery

48 What Is a Disaster Recovery Plan? Disaster Recovery Scenario Disaster Recovery Scenario Importance of Disaster Recovery Planning Importance of Disaster Recovery Planning

49 System Design Considerations for Disaster Recovery Review Design Considerations Review Design Considerations SQL Server 2000 failover clustering SQL Server 2000 failover clustering Log shipping Log shipping Replication Replication Remote disk mirroring Remote disk mirroring Snapshots Snapshots Reduce the Chance of Total System Failure Reduce the Chance of Total System Failure

50  Writing the Disaster Recovery Plan Resource Information in the Disaster Recovery Plan Resource Information in the Disaster Recovery Plan Procedural Information in the Disaster Recovery Plan Procedural Information in the Disaster Recovery Plan Backup Site Information in the Disaster Recovery Plan Backup Site Information in the Disaster Recovery Plan Scenario Information in the Disaster Recovery Plan Scenario Information in the Disaster Recovery Plan

51 Resource Information — Tools SQL Server Error Log SQL Server Error Log Windows Event Log Windows Event Log DBCC Commands DBCC Commands

52 Resource Information — Hardware and Software Operating System Operating System SQL Server SQL Server Other Software Documentation Other Software Documentation Software Locations Software Locations Transact-SQL Scripts Transact-SQL Scripts Server Hardware and Configuration Server Hardware and Configuration Access to Other Servers Access to Other Servers

53 Resource Information — Technical Staff All Necessary Contacts All Necessary Contacts Contact Information Contact Information Backup Contacts Backup Contacts

54 Procedural Information in the Disaster Recovery Plan Timelines and Data Loss Expectations Timelines and Data Loss Expectations Steps for Bringing Your System Back Online Steps for Bringing Your System Back Online Steps for Analyzing the Results of the Recovery Efforts Steps for Analyzing the Results of the Recovery Efforts

55 Backup Site Information in the Disaster Recovery Plan Hot Backup Site Hot Backup Site Warm Backup Site Warm Backup Site Cold Backup Site Cold Backup Site

56 Scenario Information — Environment Natural Disasters Natural Disasters Power Outage Power Outage Hardware Failure Hardware Failure

57 Scenario Information — SQL Server Hardware or Software Corruption Hardware or Software Corruption User-related Corruption User-related Corruption Inability to Start SQL Server Inability to Start SQL Server

58 Scenario Information — Related Systems Failure of Application, Related Server, or Network Failure of Application, Related Server, or Network Failure of Publisher, Distributor, or Subscriber Failure of Publisher, Distributor, or Subscriber Failure of a Cluster Failure of a Cluster Failure in a Distributed Environment Failure in a Distributed Environment

59  Testing and Using the Disaster Recovery Plan Testing the Disaster Recovery Plan Testing the Disaster Recovery Plan Using the Disaster Recovery Plan Using the Disaster Recovery Plan Following Up After Using the Disaster Recovery Plan Following Up After Using the Disaster Recovery Plan

60 Testing the Disaster Recovery Plan Why Test? Why Test? Develop Test Cases for Every Scenario Develop Test Cases for Every Scenario Execute the Test Plan Execute the Test Plan Validate Test Results Validate Test Results Consider Possible Redesign Based on Testing Consider Possible Redesign Based on Testing

61 Using the Disaster Recovery Plan Follow the Plan Follow the Plan Monitor Closely Monitor Closely Expect the Unexpected Expect the Unexpected Restart Recovery, If Needed Restart Recovery, If Needed

62 Following Up After Using the Disaster Recovery Plan Perform a Post Mortem after a Disaster Occurs Perform a Post Mortem after a Disaster Occurs Think About How to Prevent a Recurrence Think About How to Prevent a Recurrence Evaluate What Went Right and What Went Wrong with Your Plan Evaluate What Went Right and What Went Wrong with Your Plan Use Post Mortem Results to Improve High Availability and Your Disaster Recovery Plan Use Post Mortem Results to Improve High Availability and Your Disaster Recovery Plan

63 Review Integrating SQL Server with Your Enterprise Application Architecture Integrating SQL Server with Your Enterprise Application Architecture Designing a Disaster Recovery Plan Designing a Disaster Recovery Plan


Download ppt "SQL Server 在企业级 环境下的应用 赵歌喃 SQL Server 资深讲师. Overview Integrating SQL Server with Your Enterprise Application Architecture Integrating SQL Server with."

Similar presentations


Ads by Google