Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.

Similar presentations


Presentation on theme: "Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers."— Presentation transcript:

1 Module 11: Programming Across Multiple Servers

2 Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers Using Partitioned Views

3 Lesson: Introducing Distributed Queries What Are Distributed Queries? Ad Hoc Query Execution on Remote Data Source

4 What Are Distributed Queries? Remote Data Access  Ad hoc query  Linked server query Specify Where to Process Distributed Queries  Local computer running SQL Server  Remote OLE DB data source (pass-through query) Verify Connection Settings

5 Ad Hoc Query Execution on Remote Data Source Use the OPENROWSET Function When You Do Not Expect to Use the Data Source Repeatedly Use the OPENROWSET Function to Access Remote Data Without Setting Up a Linked Server SELECT a.* FROM OPENROWSET('SQLOLEDB', 'LONDON1'; 'newcustomer';'mypassword', 'SELECT ProductID, UnitPrice FROM Northwind.dbo.Products ORDER BY UnitPrice') AS a SELECT a.* FROM OPENROWSET('SQLOLEDB', 'LONDON1'; 'newcustomer';'mypassword', 'SELECT ProductID, UnitPrice FROM Northwind.dbo.Products ORDER BY UnitPrice') AS a

6 Lesson: Setting Up a Linked Server Environment What Is a Linked Server? How Links Are Established Security Considerations Configuration Options Linked Server Information

7 What Is a Linked Server? Other Data SourcesRemote SQL Server SQL Server allows access to other data sources Remote servers must be linked to the local computer running SQL Server Local Computer Running SQL Server Remote Computer Running SQL Server

8 How Links Are Established Connecting to a Remote Computer Running SQL Server Connecting to an OLE DB Data Source EXEC sp_addlinkedserver @server = 'AccountingServer', @svrproduct = 'SQL Server' EXEC sp_addlinkedserver @server = 'AccountingServer', @svrproduct = 'SQL Server' EXEC sp_addlinkedserver @server = 'OracleFinance', @svrproduct = 'Oracle', @provider = 'MSDAORA', @datasrc = 'OracleDB' EXEC sp_addlinkedserver @server = 'OracleFinance', @svrproduct = 'Oracle', @provider = 'MSDAORA', @datasrc = 'OracleDB'

9 Security Considerations Local Server Must Log On to Remote Server on Behalf of User If User’s Logon Account Exists on Both Servers, It Can Be Used to Log On to Remote Server Use sp_addlinkedsrvlogin to Map Logon Accounts Use Security Account Delegation to Connect to Multiple Servers with One Authentication

10 Configuration Options Collation Compatible USE master EXEC sp_serveroption 'AccountingServer', 'collation compatible', true USE master EXEC sp_serveroption 'AccountingServer', 'collation compatible', true Collation Name and Use Remote Collation Data Access RPC and RPC out Lazy Schema Validation

11 System stored procedure Returns sp_linkedservers A list of linked servers defined on the local server sp_catalogs A list of catalogs and descriptions for a specific linked server sp_indexes Index information for the specified remote table sp_primarykeys The primary key columns, one row per key column, for the specified table sp_foreignkeys The foreign keys defined on the specified remote table sp_tables_ex Table information on the tables from the specified linked server sp_columns_ex The column information, for all columns or a specified column, for linked table Linked Server Information

12 Lesson: Working with Linked Servers Linked Server Restrictions Examples of Query Execution Pass-Through Query Execution Stored Procedure Execution Distributed Transaction Management Data Modification Best Practices

13 Linked Server Restrictions Referring to Objects on Linked Servers Allowed Transact-SQL Statements  SELECT, INSERT, UPDATE, DELETE Disallowed Transact-SQL Statements  CREATE, ALTER, DROP  ORDER BY on remote tables containing large objects  READTEXT, WRITETEXT, UPDATETEXT Remote Query Optimization

14 Examples of Query Execution Use Fully Qualified Names to Reference Objects on Linked Servers SELECT CompanyName FROM AccountingServer.NorthwindRemote.dbo.Suppliers SELECT CompanyName FROM AccountingServer.NorthwindRemote.dbo.Suppliers SELECT CompanyName, Phone INTO PhoneList FROM AccountingServer.NorthwindRemote.dbo.Suppliers SELECT CompanyName, Phone INTO PhoneList FROM AccountingServer.NorthwindRemote.dbo.Suppliers SELECT ProductName, CompanyName FROM Products p JOIN AccountingServer.NorthwindRemote.dbo.Suppliers ON p.supplierid = s.supplierid SELECT ProductName, CompanyName FROM Products p JOIN AccountingServer.NorthwindRemote.dbo.Suppliers ON p.supplierid = s.supplierid Example 1 Example 2 Example 3

15 Pass-Through Query Execution Use the OPENQUERY Function to Execute Pass-Through Queries on a Linked Server Use the OPENQUERY Function in a SELECT Statement in Place of a Table Name Use the Result of an OPENQUERY Function as the Target Table of an INSERT, UPDATE, or DELETE Statement SELECT * FROM OPENQUERY (AsiaServer, 'SELECT ProductID, Royalty FROM Northwind.dbo.ProductInfo')

16 Stored Procedure Execution User Local ServerLinked Server Stored Procedure Processing EXEC accounting.master.dbo.sp_helpntgroup Stored Procedure Call Parameters and Output

17 Distributed Transaction Management Managing Distributed Transactions by Using MS DTC Managing Distributed Transactions by Using Component Services

18 Data Modification Distribute Transactions by:  Executing BEGIN DISTRIBUTED TRANSACTION -OR-  Calling API functions from a client Consider These Facts:  BEGIN DISTRIBUTED TRANSACTION statements cannot be nested  ROLLBACK TRANSACTION rolls back entire transaction  Savepoints are not supported  Set the XACT_ABORT session option

19 Use Linked Servers for Frequent Remote Data Access Set Up Linked Servers to Execute Stored Procedures Remotely or to Execute Distributed Queries Restrict Access to Linked Resources Avoid Setting Up Duplicate Logon Accounts on Different Servers Use Ad Hoc Queries for Infrequent Remote Data Access Best Practices

20 Lesson: Using Partitioned Views Advantages of Partitioned Views Partitioned Views Implementation Steps for Partitioned Views Considerations for Partitioning Data

21 Advantages of Partitioned Views Scalability  Add more hardware to a single server  Divide workload and database across multiple independent computers Benefits of Partitioned Views  Results of separate tables can appear as one table  Data location is transparent to the application  Database is programmed as a single entity

22 Partitioned Views Member Server 2 Member Server 1 Partitioned View Table B B~~~~ ~~~~ Customer A~~~~ ~~~~ B~~~~ ~~~~ A~~~~ ~~~~ B~~~~ ~~~~ Table A A~~~~ ~~~~ CREATE VIEW Cust_View SELECT Table A UNION ALL SELECT Table B CREATE VIEW Cust_View SELECT Table A UNION ALL SELECT Table B CREATE VIEW Cust_View SELECT Table A UNION ALL SELECT Table B CREATE VIEW Cust_View SELECT Table A UNION ALL SELECT Table B

23 To Set Up Distributed Partitioned Views: Create multiple databases, each on a different member server 1 1 Create linked server definitions on each member server 3 3 Create a partitioned view on each member server by using the UNION ALL set operator 4 4 Horizontally partition the tables 2 2 Implementation Steps for Partitioned Views

24 Considerations for Partitioning Data Design Considerations  Partition data to keep related data on the same server  Minimize need to access data on other member servers  Place complete records on the same member server  Select the appropriate column to define the partition Ways to Partition Rules for Partitioning

25 Lab A: Using Distributed Data Exercise 1: Setting Up Linked Servers Exercise 2: Querying Remote Data If Time Permits: Managing Distributed Transactions


Download ppt "Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers."

Similar presentations


Ads by Google