Introduction to Querying Data in Azure

Azure provides a rich ecosystem of services for storing and retrieving data. Effective querying is crucial for extracting insights, troubleshooting issues, and building powerful applications. This documentation delves into the primary methods and services available for querying your data across Azure.

Whether you're working with relational databases, NoSQL stores, log analytics, or time-series data, Azure offers specialized tools and query languages designed for optimal performance and ease of use.

Azure Data Explorer (Kusto)

Azure Data Explorer (Kusto) is a fast, highly scalable data analytics service for real-time data exploration and analysis on large volumes of streaming data. It's particularly well-suited for log and telemetry analytics.

Kusto Query Language (KQL)

KQL is the language used to query data in Azure Data Explorer. It's a read-only query language with a rich set of operators for data manipulation and analysis.

Example KQL Query


AzureActivity
| where TimeGenerated > ago(1h)
| where OperationName has "Delete"
| project TimeGenerated, Caller, ResourceGroup, Resource
| order by TimeGenerated desc
                    

This query retrieves Azure activity logs from the past hour, filtering for delete operations, and projects specific details.

Key features of KQL include:

  • Simple syntax, powerful capabilities.
  • Rich set of scalar and tabular operators.
  • Intelligent fuzzy search.
  • Time-series analysis functions.

Learn more about Kusto Query Language.

Azure SQL Database

Azure SQL Database is a managed relational data service that supports SQL Server workloads. You can query your data using standard SQL.

Transact-SQL (T-SQL)

The primary query language for Azure SQL Database is Transact-SQL (T-SQL), a powerful extension of SQL.

Example T-SQL Query


SELECT TOP 100
    CustomerID,
    CompanyName,
    ContactName
FROM
    Customers
WHERE
    Country = 'USA'
ORDER BY
    CustomerID ASC;
                    

This T-SQL query retrieves the top 100 customers from the 'USA' based on their CustomerID.

Utilize standard SQL `SELECT`, `INSERT`, `UPDATE`, `DELETE` statements, along with stored procedures and functions, to interact with your data.

Explore Querying data in Azure SQL Database.

Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model database service. It supports various data models and query languages depending on the API used.

SQL API (DocumentDB)

The SQL API provides a SQL-like query experience for JSON documents.

Example Cosmos DB SQL API Query


SELECT c.id, c.name
FROM c
WHERE c.city = "Redmond"
AND c.isPremiumCustomer = true
                    

This query selects the ID and name of premium customers located in "Redmond".

Other APIs

  • MongoDB API: Uses MongoDB query language.
  • Cassandra API: Uses CQL (Cassandra Query Language).
  • Gremlin API: Uses Gremlin graph traversal language.
  • Table API: Uses OData filter expressions.

Refer to the Azure Cosmos DB documentation for specific API query details.

Azure Storage Analytics

Azure Storage Analytics provides valuable metrics and logs for your Azure Storage accounts. You can query these logs for operational insights.

Querying Storage Logs

Storage logs are stored as blobs in a dedicated container ($logs). You can download these logs and parse them, or integrate with services like Azure Data Explorer or Azure Databricks for more advanced analysis.

Tip:

For large-scale analysis of storage logs, consider using Azure Data Explorer with its schema mapping capabilities for efficient querying.

Learn how to enable and use Storage Analytics.

Azure Monitor Logs

Azure Monitor Logs collects and analyzes telemetry data from your Azure and on-premises environments. It uses Log Analytics workspaces and KQL for querying.

Log Analytics Workspace

Querying in Azure Monitor Logs is done within a Log Analytics workspace using KQL. This allows you to correlate data from various sources, including application logs, VM logs, and Azure service logs.

Example Azure Monitor Query


Perf
| where Computer == "MyServer01"
| where CounterName == "% Processor Time" and InstanceName == "_Total"
| summarize AVG(CounterValue) by bin(TimeGenerated, 5m)
| render timechart
                    

This query visualizes the average CPU utilization for "MyServer01" over 5-minute intervals.

Dive deeper into Log Analytics querying with KQL.

Best Practices for Querying Azure Data

To ensure efficient and cost-effective querying, consider these best practices:

  • Understand your data: Know the schema, data types, and relationships.
  • Filter early and often: Reduce the dataset size as early as possible in your query.
  • Use appropriate indexes: For relational databases, ensure relevant columns are indexed.
  • Select only necessary columns: Avoid `SELECT *`.
  • Optimize for the specific service: Each Azure service has its query optimization nuances.
  • Monitor query performance: Use built-in tools to identify and resolve performance bottlenecks.
  • Leverage caching: Where applicable, use caching mechanisms to speed up frequent queries.

Pro Tip:

Regularly review your most frequent or resource-intensive queries and look for optimization opportunities.