Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up...

25
Module 11: Programming Across Multiple Servers

Transcript of Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up...

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

Module 11: Programming Across

Multiple Servers

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

Overview

Introducing Distributed Queries

Setting Up a Linked Server Environment

Working with Linked Servers

Using Partitioned Views

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

Lesson: Introducing Distributed Queries

What Are Distributed Queries?

Ad Hoc Query Execution on Remote Data Source

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

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

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

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, UnitPriceFROM Northwind.dbo.Products ORDER BY UnitPrice')AS a

SELECT a.*

FROM OPENROWSET('SQLOLEDB', 'LONDON1'; 'newcustomer';'mypassword','SELECT ProductID, UnitPriceFROM Northwind.dbo.Products ORDER BY UnitPrice')AS a

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

Lesson: Setting Up a Linked Server Environment

What Is a Linked Server?

How Links Are Established

Security Considerations

Configuration Options

Linked Server Information

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

What Is a Linked Server?

Other Data SourcesRemote SQL Server

SQL Server allowsaccess to otherdata sources

Remote serversmust be linked tothe local computer running SQL Server

Local Computer Running SQL Server

Remote Computer Running SQL Server

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

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'

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

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

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

Configuration Options

Collation Compatible

USE masterEXEC sp_serveroption 'AccountingServer', 'collation compatible', true

USE masterEXEC sp_serveroption 'AccountingServer', 'collation compatible', true

Collation Name and Use Remote Collation

Data Access

RPC and RPC out

Lazy Schema Validation

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

Systemstored 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

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

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

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

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

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

Examples of Query Execution

Use Fully Qualified Names to Reference Objects on Linked Servers

SELECT CompanyName FROM AccountingServer.NorthwindRemote.dbo.SuppliersSELECT CompanyName FROM AccountingServer.NorthwindRemote.dbo.Suppliers

SELECT CompanyName, PhoneINTO PhoneList FROM AccountingServer.NorthwindRemote.dbo.Suppliers

SELECT CompanyName, PhoneINTO PhoneList FROM AccountingServer.NorthwindRemote.dbo.Suppliers

SELECT ProductName, CompanyNameFROM Products p JOIN AccountingServer.NorthwindRemote.dbo.SuppliersON p.supplierid = s.supplierid

SELECT ProductName, CompanyNameFROM Products p JOIN AccountingServer.NorthwindRemote.dbo.SuppliersON p.supplierid = s.supplierid

Example 1Example 1

Example 2Example 2

Example 3Example 3

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

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, orDELETE Statement

SELECT * FROM OPENQUERY(AsiaServer, 'SELECT ProductID, RoyaltyFROM Northwind.dbo.ProductInfo')

SELECT * FROM OPENQUERY(AsiaServer, 'SELECT ProductID, RoyaltyFROM Northwind.dbo.ProductInfo')

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

Stored Procedure Execution

User

Local Server Linked Server

Stored Procedure Processing

EXEC accounting.master.dbo.sp_helpntgroupEXEC accounting.master.dbo.sp_helpntgroup

Stored Procedure Call

Parameters and Output

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

Distributed Transaction Management

Managing Distributed Transactions by Using MS DTC

Managing Distributed Transactions by UsingComponent Services

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

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

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

Use Linked Servers for Frequent Remote Data AccessUse Linked Servers for Frequent Remote Data Access

Set Up Linked Servers to Execute Stored Procedures Remotely or to Execute Distributed QueriesSet Up Linked Servers to Execute Stored Procedures Remotely or to Execute Distributed Queries

Restrict Access to Linked ResourcesRestrict Access to Linked ResourcesAvoid Setting Up Duplicate Logon Accounts on Different ServersAvoid Setting Up Duplicate Logon Accounts on Different Servers

Use Ad Hoc Queries for Infrequent RemoteData AccessUse Ad Hoc Queries for Infrequent RemoteData Access

Best Practices

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

Lesson: Using Partitioned Views

Advantages of Partitioned Views

Partitioned Views

Implementation Steps for Partitioned Views

Considerations for Partitioning Data

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

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

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

Partitioned Views

Member Server 2

Member Server 1

Partitioned View

Table BB ~ ~ ~ ~

~ ~ ~ ~

CustomerA ~ ~ ~ ~

~ ~ ~ ~B ~ ~ ~ ~

~ ~ ~ ~

CustomerA ~ ~ ~ ~

~ ~ ~ ~B ~ ~ ~ ~

~ ~ ~ ~

Table AA ~ ~ ~ ~

~ ~ ~ ~

CREATE VIEW Cust_ViewSELECT Table A UNION ALLSELECT Table B

CREATE VIEW Cust_ViewSELECT Table A UNION ALLSELECT Table B

CREATE VIEW Cust_ViewSELECT Table A UNION ALLSELECT Table B

CREATE VIEW Cust_ViewSELECT Table A UNION ALLSELECT Table B

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

To Set Up Distributed Partitioned Views:To Set Up Distributed Partitioned Views:

Create multiple databases, each on a different member serverCreate multiple databases, each on a different member server11

Create linked server definitions on eachmember serverCreate linked server definitions on eachmember server33

Create a partitioned view on each member server by using the UNION ALL set operator Create a partitioned view on each member server by using the UNION ALL set operator 44

Horizontally partition the tablesHorizontally partition the tables22

Implementation Steps for Partitioned Views

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

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

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

Lab A: Using Distributed Data

Exercise 1: Setting Up Linked Servers

Exercise 2: Querying Remote Data

If Time Permits: ManagingDistributed Transactions