2017-02-16 4 views
0

ElasticDBプールで使用可能なすべてのデータベース上のテーブルでSELECTを実行する方法を教えてください。それらはすべて同じDBスキーマを持ち、動的に作成されます。私はElastic Database Queryを調査しましたが、途中で迷子になりました。Azure ElasticDB Pool内のすべてのDBを照会するには?

Reporting across scaled-out cloud databases

最初にサンプルコンソールアプリケーションをダウンロードし、シャードを作成してから少し紛らわしいクエリを実行するように求められます。とにかく、SQL Server Management StudioからT-SQLクエリを実行してすべてのデータベースをクエリできますか?

PS:DBは断片化されていません。顧客1人当たりのDBです。

ありがとうございました!

答えて

0

私はあなたが外部のソースとしてデータベースを追加する必要があるので、クロスデータベースクエリを実行できると思っています。テーブルをローカルのようにクエリできます。 は、私はあなたがそれを設定することができ、ガイドが見つかりました:案内する リンク:

https://www.mssqltips.com/sqlservertip/4550/sql-azure-cross-database-querying/

ガイド:

DB1 has Db1Table table:

CREATE TABLE DB1.dbo.Db1Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
CustomerName NVARCHAR(50)); 
INSERT INTO DB1.dbo.Db1Table(CustomerId, CustomerName) VALUES 
(1, 'aaaaaaa'), 
(2, 'bbbbbbb'), 
(3, 'ccccccc'), 
(4, 'ddddddd'), 
(5, 'eeeeeee'); 
CREATE TABLE DB1.dbo.Db1Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
CustomerName NVARCHAR(50)); 
INSERT INTO DB1.dbo.Db1Table(CustomerId, CustomerName) VALUES 
(1, 'aaaaaaa'), 
(2, 'bbbbbbb'), 
(3, 'ccccccc'), 
(4, 'ddddddd'), 
(5, 'eeeeeee'); 

DB2 has Db2Table table:

CREATE TABLE DB2.dbo.Db2Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
Country NVARCHAR(50)); 
INSERT INTO DB2.dbo.Db2Table(CustomerId, Country) VALUES 
(1, 'United States'), 
(3, 'Greece'), 
(4, 'France'), 
(5, 'Germany'), 
(6, 'Ireland'); 
CREATE TABLE DB2.dbo.Db2Table (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY, 
CustomerId INT, 
Country NVARCHAR(50)); 
INSERT INTO DB2.dbo.Db2Table(CustomerId, Country) VALUES 
(1, 'United States'), 
(3, 'Greece'), 
(4, 'France'), 
(5, 'Germany'), 
(6, 'Ireland'); 

If we want to fetch customers whose country is Greece then we could do the following query:

SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB2.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 
SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB2.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 

but instead of returning customerId 3 we get the following error:

Reference to database and/or server name in 'DB2.dbo.Db2Table' is not supported in this version of SQL Server.

Reference to database and/or server name in 'DB2.dbo.Db2Table' is not supported in this version of SQL Server.

In order to be able to perform a cross database query we need to perform the following steps: Step1: Create Master Key

The database master key is a symmetric key used to protect the private keys of certificates and asymmetric keys that are present in the database. More info here.

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>'; 

-- Example -- 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>'; 


-- Example -- 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 

Step 2: Create Database Scoped Credential “my_credential”

A database credential is not mapped to a server login or database user. The credential is used by the database to access the external location anytime the database is performing an operation that requires access.

CREATE DATABASE SCOPED CREDENTIAL <credential_name> 
WITH IDENTITY = '<user>', 
SECRET = '<secret>'; 

-- Example -- 
CREATE DATABASE SCOPED CREDENTIAL my_credential 
WITH IDENTITY = 'dbuser', 
SECRET = '[email protected]'; 


CREATE DATABASE SCOPED CREDENTIAL <credential_name> 
WITH IDENTITY = '<user>', 
SECRET = '<secret>'; 


-- Example -- 
CREATE DATABASE SCOPED CREDENTIAL my_credential 
WITH IDENTITY = 'dbuser', 
SECRET = '[email protected]'; 

credential_name Specifies the name of the database scoped credential being created. credential_name cannot start with the number (#) sign. System credentials start with ##. IDENTITY =’identity_name‘ Specifies the name of the account to be used when connecting outside the server. SECRET =’secret‘ Specifies the secret required for outgoing authentication.

Step 3: Create External Data Source “my_datasource” of type RDBMS

This instruction creates an external data source for use in Elastic Database queries. For RDBMS, it specifies the logical server name of the remote database in Azure SQL Database. -- (only on Azure SQL Database v12 or later)

CREATE EXTERNAL DATA SOURCE <data_source_name> 
WITH (
TYPE=RDBMS, 
LOCATION='<server_name>.database.secure.windows.net', 
DATABASE_NAME='<remote_database_name>', 
CREDENTIAL = <sql_credential>); 

-- Example -- 
CREATE EXTERNAL DATA SOURCE my_datasource 
WITH (
TYPE=RDBMS, 
LOCATION='ppolsql.database.secure.windows.net', 
DATABASE_NAME='DB2', 
CREDENTIAL = my_credential); 

-- (only on Azure SQL Database v12 or later) 

CREATE EXTERNAL DATA SOURCE <data_source_name> 
WITH (
TYPE=RDBMS, 
LOCATION='<server_name>.database.secure.windows.net', 
DATABASE_NAME='<remote_database_name>', 
CREDENTIAL = <sql_credential>); 

-- Example -- 
CREATE EXTERNAL DATA SOURCE my_datasource 
WITH (
TYPE=RDBMS, 
LOCATION='ppolsql.database.secure.windows.net', 
DATABASE_NAME='DB2', 
CREDENTIAL = my_credential); 
CREATE EXTERNAL TABLE [ database_name . [ schema_name ] . | schema_name. ] table_name 
(<column_definition> [ ,...n ]) 
WITH ( 
    DATA_SOURCE = <data_source_name>); 

-- Example -- 
CREATE EXTERNAL TABLE [dbo].[Db2Table] (
[ID] int NOT NULL, 
[CustomerId] INT, 
[Country] NVARCHAR(50) 
) WITH (DATA_SOURCE = my_datasource) 


CREATE EXTERNAL TABLE [ database_name . [ schema_name ] . | schema_name. ] table_name 
(<column_definition> [ ,...n ]) 
WITH ( 
    DATA_SOURCE = <data_source_name> 
); 

-- Example -- 
CREATE EXTERNAL TABLE [dbo].[Db2Table] (
[ID] int NOT NULL, 
[CustomerId] INT, 
[Country] NVARCHAR(50) 
) WITH (DATA_SOURCE = my_datasource) 

database_name . [ schema_name ] . | schema_name. ] table_name The one to three-part name of the table to create. For an external table, only the table metadata is stored in SQL along with basic statistics about the file and/or folder referenced in Hadoop or Azure blob storage. No actual data is moved or stored in SQL Server. [ ,…n ] The column definitions, including the data types and number of columns, must match the data in the external files. DATA_SOURCE = external_data_source_name Specifies the name of the external data source that contains the location of the external data.

After running the DDL statements, you can access the remote table Db2Table as though it were a local table.

So, now if we want to fetch customers whose country is Greece the query would be executed successfully:

SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB1.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 

-- Result -- 
CustomerId | CustomerName 
------------------------- 
3  ccccccc 


SELECT 
db1.CustomerId, 
db1.CustomerName 
FROM DB1.dbo.Db1Table db1 
INNER JOIN DB1.dbo.Db2Table db2 ON db1.CustomerId = db2.CustomerId 
WHERE db2.Country = 'Greece'; 


-- Result -- 
CustomerId | CustomerName 
------------------------- 
3  ccccccc 
関連する問題