|
2 | 2 | title: Best practices for SQL on-demand (preview) in Azure Synapse Analytics
|
3 | 3 | description: Recommendations and best practices you should know as you work with SQL on-demand (preview).
|
4 | 4 | services: synapse-analytics
|
5 |
| -author: mlee3gsd |
| 5 | +author: filippopovic |
6 | 6 | manager: craigg
|
7 | 7 | ms.service: synapse-analytics
|
8 | 8 | ms.topic: conceptual
|
9 | 9 | ms.subservice:
|
10 |
| -ms.date: 04/15/2020 |
11 |
| -ms.author: martinle |
12 |
| -ms.reviewer: igorstan |
| 10 | +ms.date: 05/01/2020 |
| 11 | +ms.author: fipopovi |
| 12 | +ms.reviewer: jrasnick |
13 | 13 | ---
|
14 | 14 |
|
15 | 15 | # Best practices for SQL on-demand (preview) in Azure Synapse Analytics
|
@@ -45,17 +45,79 @@ If possible, you can prepare files for better performance:
|
45 | 45 | - It's better to have equally sized files for a single OPENROWSET path or an external table LOCATION.
|
46 | 46 | - Partition your data by storing partitions to different folders or file names - check [use filename and filepath functions to target specific partitions](#use-fileinfo-and-filepath-functions-to-target-specific-partitions).
|
47 | 47 |
|
| 48 | +## Push wildcards to lower levels in path |
| 49 | + |
| 50 | +You can use wildcards in your path to [query multiple files and folders](develop-storage-files-overview.md#query-multiple-files-or-folders). SQL on-demand lists files in your storage account starting from first * using storage API and eliminates files that do not match specified path. Reducing initial list of files can improve performance if there are many files that match specified path up to first wildcard. |
| 51 | + |
| 52 | +## Use appropriate data types |
| 53 | + |
| 54 | +Data types used in your query affects performance. You can get better performance if you: |
| 55 | + |
| 56 | +- Use the smallest data size that will accommodate the largest possible value. |
| 57 | + - If maximum character value length is 30 characters, use character data type of length 30. |
| 58 | + - If all character column values are of fixed size, use char or nchar. Otherwise, use varchar or nvarchar. |
| 59 | + - If maximum integer column value is 500, use smallint as it is smallest data type that can accommodate this value. You can find integer data type ranges [here](https://docs.microsoft.com/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-ver15). |
| 60 | +- If possible, use varchar and char instead of nvarchar and nchar. |
| 61 | +- Use integer-based data types if possible. Sort, join and group by operations are performed faster on integers than on characters data. |
| 62 | +- If you are using schema inference, [check inferred data type](#check-inferred-data-types). |
| 63 | + |
| 64 | +## Check inferred data types |
| 65 | + |
| 66 | +[Schema inference](query-parquet-files.md#automatic-schema-inference) helps you quickly write queries and explore data without knowing file schema. This comfort comes at expense of inferred data types being larger than they actually are. It happens when there is not enough information in source files to make sure appropriate data type is used. For example, Parquet files do not contain metadata about maximum character column length and SQL on-demand infers it as varchar(8000). |
| 67 | + |
| 68 | +You can check resulting data types of your query using [sp_describe_first_results_set](https://docs.microsoft.com/sql/relational-databases/system-stored-procedures/sp-describe-first-result-set-transact-sql?view=sql-server-ver15). |
| 69 | + |
| 70 | +The following example shows how you can optimize inferred data types. Procedure is used to show inferred data types. |
| 71 | +```sql |
| 72 | +EXEC sp_describe_first_result_set N' |
| 73 | + SELECT |
| 74 | + vendor_id, pickup_datetime, passenger_count |
| 75 | + FROM |
| 76 | + OPENROWSET( |
| 77 | + BULK ''https://sqlondemandstorage.blob.core.windows.net/parquet/taxi/*/*/*'', |
| 78 | + FORMAT=''PARQUET'' |
| 79 | + ) AS nyc'; |
| 80 | +``` |
| 81 | + |
| 82 | +Here is the result set. |
| 83 | + |
| 84 | +|is_hidden|column_ordinal|name|system_type_name|max_length| |
| 85 | +|----------------|---------------------|----------|--------------------|-------------------|| |
| 86 | +|0|1|vendor_id|varchar(8000)|8000| |
| 87 | +|0|2|pickup_datetime|datetime2(7)|8| |
| 88 | +|0|3|passenger_count|int|4| |
| 89 | + |
| 90 | +Once we know inferred data types for query we can specify appropriate data types: |
| 91 | + |
| 92 | +```sql |
| 93 | +SELECT |
| 94 | + vendor_id, pickup_datetime, passenger_count |
| 95 | +FROM |
| 96 | + OPENROWSET( |
| 97 | + BULK 'https://sqlondemandstorage.blob.core.windows.net/parquet/taxi/*/*/*', |
| 98 | + FORMAT='PARQUET' |
| 99 | + ) |
| 100 | + WITH ( |
| 101 | + vendor_id varchar(4), -- we used length of 4 instead of inferred 8000 |
| 102 | + pickup_datetime datetime2, |
| 103 | + passenger_count int |
| 104 | + ) AS nyc; |
| 105 | +``` |
| 106 | + |
48 | 107 | ## Use fileinfo and filepath functions to target specific partitions
|
49 | 108 |
|
50 | 109 | Data is often organized in partitions. You can instruct SQL on-demand to query particular folders and files. This function will reduce the number of files and amount of data the query needs to read and process. An added bonus is that you'll achieve better performance.
|
51 | 110 |
|
52 | 111 | For more information, check [filename](develop-storage-files-overview.md#filename-function) and [filepath](develop-storage-files-overview.md#filepath-function) functions and examples on how to [query specific files](query-specific-files.md).
|
53 | 112 |
|
| 113 | +> [!TIP] |
| 114 | +> Always cast result of filepath and fileinfo functions to appropriate data types. If you use character data types, make sure appropriate length is used. |
| 115 | +
|
54 | 116 | If your stored data isn't partitioned, consider partitioning it so you can use these functions to optimize queries targeting those files. When [querying partitioned Spark tables](develop-storage-files-spark-tables.md) from SQL on-demand, the query will automatically target only the files needed.
|
55 | 117 |
|
56 | 118 | ## Use CETAS to enhance query performance and joins
|
57 | 119 |
|
58 |
| -[CETAS](develop-tables-cetas.md) is one of the most important features available in SQL on-demand. CETAS is a parallel operation that creates external table metadata and exports the SELECT query results to a set of files in your storage account. |
| 120 | +[CETAS](develop-tables-cetas.md) is one of the most important features available in SQL on-demand. CETAS is a parallel operation that creates external table metadata and exports the SELECT query results to a set of files in your storage account. |
59 | 121 |
|
60 | 122 | You can use CETAS to store frequently used parts of queries, like joined reference tables, to a new set of files. Next, you can join to this single external table instead of repeating common joins in multiple queries.
|
61 | 123 |
|
|
0 commit comments