Azure SQL Cross Database Query is a powerful feature that allows you to query data across multiple databases in a single database instance. This is achieved through the use of distributed queries.
To set up cross-database querying, you need to create a linked server, which is a connection to another database server. A linked server can be created by using the sp_addlinkedserver stored procedure.
The distributed query is then executed by using the four-part naming convention, which includes the linked server name, the database name, the schema name, and the table name. This allows you to access data from other databases as if it were local.
Querying Across Databases
To facilitate querying scenarios where multiple databases contribute rows into a single overall result, Azure SQL offers elastic database query. This feature allows you to query across several databases using the familiar SQL Server connectivity experience in tools like Excel, PowerBI, Tableau, or Cognos.
Elastic queries are especially useful for creating reports, using commercial BI or data integration tools, or any application that cannot be changed. They enable easy access to an entire collection of databases through queries issued by SQL Server Management Studio or Visual Studio.
There are two main topologies for elastic query: Vertical partitioning and Horizontal Partitioning – Sharding. Vertical partitioning involves cross-database queries, where data is partitioned vertically between a number of databases in a data tier. This approach requires querying across or compiling reports across tables in several databases.
Here are the common use cases for Vertical partitioning:
- Inventory data is on one database, while accounting-related tables are on a second database.
- Querying across tables in several databases to compile reports.
To implement elastic query for vertical and horizontal partitioning scenarios, follow the steps outlined in the documentation. These steps refer to more detailed documentation for the different partitioning scenarios.
Elastic queries can also be used for cross-database reporting, allowing you to query across several databases using the familiar SQL Server connectivity experience. This is especially useful when creating reports or using commercial BI or data integration tools.
Sharding and Partitioning
Sharding and partitioning are two techniques used to scale databases and improve query performance. To shard a database, you divide it into smaller, more manageable pieces called shards, each containing a subset of the data. This allows you to distribute the data across multiple databases, making it easier to query and manage.
In Azure SQL, you can use elastic queries to perform reporting tasks over a sharded data tier. This requires an elastic database shard map to represent the databases of the data tier. Typically, only a single shard map is used in this scenario.
To configure elastic database queries for horizontal partitioning scenarios, you need to create a shard map representing your data tier using the elastic database client library. This will allow you to access the horizontally partitioned table as though it were a local table.
Here are the steps to configure elastic database queries for horizontal partitioning scenarios:
- CREATE MASTER KEY mymasterkey
- CREATE DATABASE SCOPED CREDENTIAL mycredential
- Create a shard map representing your data tier using the elastic database client library.
- CREATE/DROP EXTERNAL DATA SOURCE mydatasource of type SHARD_MAP_MANAGER
- CREATE/DROP EXTERNAL TABLE mytable
By following these steps, you can access the horizontally partitioned table "mytable" as though it were a local table.
Horizontal Partitioning – Sharding
Horizontal partitioning, also known as sharding, is a technique that allows you to distribute large datasets across multiple databases. This is particularly useful when dealing with massive amounts of data that don't fit into a single database.
To get started with sharding, you'll need to create an elastic database shard map to represent the databases in your data tier. Typically, only a single shard map is used in this scenario.
A dedicated database with elastic query capabilities serves as the entry point for reporting queries. This dedicated database needs access to the shard map, but the databases in the data tier can be of any Azure SQL Database version or edition.
You can use elastic query to perform reporting tasks over a sharded data tier. This requires an elastic database shard map to represent the databases of the data tier.
To configure elastic database queries for horizontal partitioning, you'll need to perform the following steps:
- CREATE MASTER KEY mymasterkey
- CREATE DATABASE SCOPED CREDENTIAL mycredential
- Create a shard map representing your data tier using the elastic database client library.
- CREATE/DROP EXTERNAL DATA SOURCE mydatasource of type SHARD_MAP_MANAGER
- CREATE/DROP EXTERNAL TABLE mytable
Once you've completed these steps, you can access the horizontally partitioned table as though it were a local table. Azure SQL Database automatically opens multiple parallel connections to the remote databases where the tables are physically stored, processes the requests on the remote databases, and returns the results.
Vertical Partitioning
Vertical partitioning is a technique used to improve query performance by distributing data across multiple databases. It's particularly useful for reference data management and cross-database querying.
To implement vertical partitioning, you can use elastic queries. This allows you to access tables in one database from another database, without making any changes to the remote database. No changes are necessary on the remote database.
Elastic queries can be used for reference data management, such as accessing reference tables from other databases. This is useful when reference tables are small or remote queries into the reference table have selective predicates.
For cross-database querying, elastic queries enable use cases that require querying across several databases. This can be useful when different databases have different schemas, and you need to access data from multiple databases.
To configure elastic database queries for vertical partitioning scenarios, you'll need to run a few DDL statements. These include creating a master key, a database scoped credential, an external data source, and an external table.
Here's a step-by-step guide to configuring elastic database queries for vertical partitioning:
- Create a master key: `CREATE MASTER KEY mymasterkey`
- Create a database scoped credential: `CREATE DATABASE SCOPED CREDENTIAL mycredential`
- Create an external data source: `CREATE/DROP EXTERNAL DATA SOURCE mydatasource of type RDBMS`
- Create an external table: `CREATE/DROP EXTERNAL TABLE mytable`
After running these DDL statements, you can access the remote table as though it were a local table. Azure SQL Database automatically opens a connection to the remote database, processes your request on the remote database, and returns the results.
Sources
- https://www.mssqltips.com/sqlservertip/6445/azure-sql-cross-database-query/
- https://www.aspnetmonsters.com/2020/11/2020-11-05-cross-database-queries/
- https://github.com/Huachao/azure-content/blob/master/articles/sql-database/sql-database-elastic-query-overview.md
- https://www.mssqltips.com/sqlservertip/5439/crossdatabase-query-performance-in-azure-sql-database/
- https://blog.sqlauthority.com/2017/09/12/sql-server-create-linked-server-sql-azure-database/
Featured Images: pexels.com