页面简介
In this post, we show you how to enable the Amazon Q generative SQL feature in the Redshift query editor and use the feature to get tailored SQL commands based on your natural language queries. With Amazon Q, you can spend less time worrying about the nuances of SQL syntax and optimizations, allowing you to concentrate your efforts on extracting invaluable business insights from your data.
正文摘录
AWS Big Data Blog Write queries faster with Amazon Q generative SQL for Amazon Redshift Amazon Redshift is a fully managed, AI-powered cloud data warehouse that delivers the best price-performance for your analytics workloads at any scale. Amazon Q generative SQL brings the capabilities of generative AI directly into the Amazon Redshift query editor. Amazon Q generative SQL for Amazon Redshift was launched in preview during AWS re:Invent 2023. With over 85,000 queries executed in preview, Amazon Redshift announced the general availability in September 2024. Amazon Q generative SQL for Amazon Redshift uses generative AI to analyze user intent, query patterns, and schema metadata to identify common SQL query patterns directly within Amazon Redshift, accelerating the query authoring process for users and reducing the time required to derive actionable data insights. It provides a conversational interface where users can submit queries in natural language within the scope of their current data permissions. Generative SQL uses query history for better accuracy, and you can further improve accuracy through custom context, such as table descriptions, column descriptions, foreign key and primary key definitions, and sample queries. Custom context enhances the AI model’s understanding of your specific data model, business logic, and query patterns, allowing it to generate more relevant and accurate SQL recommendations. It enables you to get insights faster without extensive knowledge of your organization’s complex database schema and metadata. Within this feature, user data is secure and private. Your data is not shared across accounts. Your queries, data and database schemas are not used to train a generative AI foundational model (FM). Your input is used as contextual prompts to the FM to answer only your queries. In this post, we show you how to enable the Amazon Q generative SQL feature in the Redshift query editor and use the feature to get tailored SQL commands based on your natural language queries. With Amazon Q, you can spend less time worrying about the nuances of SQL syntax and optimizations, allowing you to concentrate your efforts on extracting invaluable business insights from your data. Solution overview At a high level, the feature works as follows: For generating the SQL code, you can write your query request in plain English within the conversational interface in the Redshift query editor. The query editor sends the query context to the underlying Amazon Q generative SQL platform, which uses generative AI to generate SQL code recommendations based on your Redshift metadata. You receive the generated SQL code suggestions within the same chat interface. The following diagram illustrates this workflow. Your content processed by generative SQL is not stored or used by AWS for service improvement. Amazon Q generative SQL uses a large language model (LLM) and Amazon Bedrock to generate the SQL query. AWS uses different techniques, such as prompt engineering and Retrieval Augmented Generation (RAG), to query the model based on your context: The database you’re connected to The schema you’re working on Your query history Optionally, the query history of other users connected to the same endpoint Amazon Q generative SQL is conversational, and you can ask it to refine a previously generated query. In the following sections, we demonstrate how to enable the generative SQL feature in the Redshift query editor and use it to generate SQL queries using natural language. Prerequisites To get started, you need an Amazon Redshift Serverless endpoint or an Amazon Redshift provisioned cluster. For this post, we use Redshift Serverless. Refer to Easy analytics and cost-optimization with Amazon Redshift Serverless to get started. Enable the Amazon Q generative SQL feature in the Redshift query editor If you’re using the feature for the first time, you need to enable the Amazon Q generative SQL feature in the Redshift query editor. To enable the feature, complete the following steps: On the Amazon Redshift console, open the Redshift Serverless dashboard. Choose Query data. You can also choose Query Editor V2 in the navigation pane of the Amazon Redshift console. When you open the Redshift query editor, you will see the new icon for Amazon Q next to the database dropdown menu on the top of the query editor console. If you choose the Amazon Q icon, you will see the message “Amazon Redshift query editor V2 now supports generative SQL functionality. Contact your administrator to activate this feature in Settings.” If you’re not the administrator, you need to work with the account administrator to enable this feature. If you’re the administrator, choose the hyperlink in the message, or go to the settings icon and choose Generative SQL settings. In the Generative SQL settings section, select Q generative SQL, which will turn on Amazon Q generative SQL for all users of the account. Amazon Q generative SQL is personalized to your database and, based on the updates or conversations you have had with the feature, will apply those learnings to other user conversations who connect to the same database with their own credentials. In the generative SQL settings, you can see the instructions to grant the sys:monitor role to a user or role. Choose Save. You will receive a confirmation that the Amazon Q generative SQL settings have been successfully updated. Load notebooks with sample TPC-DS data The Redshift query editor comes with sample data and SQL notebooks that you can load into a sample database and corresponding schema. For this post, we use TPC-DS for a decision support benchmark. We start by loading the TPC-DS data into the Redshift database. When you load this data, the schema tpcds is updated with sample data. We also use the provided notebooks with the tpcds schema to run queries to build a query history. Complete the following steps: Connect to your Redshift Serverless workgroup or Redshift provisioned cluster. Navigate to the sample_data_dev database to view the sample databases available for running the generative SQL feature. Hover over the tpcds schema and choose Open sample notebooks. In the Create sample database pop-up message, choose Create. In a few seconds, you will see the notification that the database sample_data_dev is created successfully and tpcds sample data is loaded successfully. Two sample notebooks for the schema are also generated. Choose Run all on each notebook tab. This will take a few minutes to run and will establish a query history for the tpcds data. This step is not mandatory for using the feature for your organization’s data warehouse. Use Amazon Q to generate SQL queries from natural language Now that the Amazon Q generative SQL feature is enabled and ready for use, open a new notebook and choose the Amazon Q icon to open a chat pane in the Redshift query editor. Amazon Q generative SQL is personalized to your schema. It uses metadata from database schemas to improve the SQL query suggestions. Optionally, administrators can allow the use of the account’s query history to further improve the generated SQL. This can be enabled by running the following GRANT commands to provide access to your query history to other roles or users: GRANT ROLE SYS:MONITOR to "IAMR:role-name"; GRANT ROLE SYS:MONITOR to "IAM:user-name"; GRANT ROLE SYS:MONITOR to "database-username"; This optional step allows users to make query monitoring history available to other users connected to the same database. Let’s get started with some query examples. First, make sure you’re connected to sample_data_dev Let’s ask the query “What are the top 10 stores in sales in 1998?” This generates a SQL query. Amazon Q generative SQL is also personalized to your data domain. You will notice that it joins to the Store table to retrieve store_name. Choose Add to notebook under the query to add the generated SQL. Our query runs successfully and shows that the store able has the most sales. Amazon Q is personalized to your conversation. Suppose you want to know what the top selling item was for store able. You can ask this question “What was the unique identifier of the top selling item for the store ‘able’?” The results show the top selling item. However, the query didn’t filter on the year. Let’s ask Amazon Q to give us the top selling item for store able in 1998. Instead of repeating the whole question again, you can simply ask “Can you filter by the year 1998?” Now we have the top selling item for store able for 1998. To display the item description, you can ask the query “Can you modify the query to include its name and description?” Amazon Q added the join to the item table and the query ran successfully. Now that we have done some basic queries, let’s do some deeper analysis. Let’s ask Amazon Q “Can you give me aggregated store sales, for each county by quarter for all years?” The answer is correct, but let’s ask a follow-up to include the state. Ask the follow-up question: “Can you include state?” This answer looks good; you can also add an ORDER BY clause if you want the data sorted or ask Amazon Q to add that. So far, we have only been looking at store_sales data. The TPC-DS data contains data for other sales channels, including web_sales and catalog_sales. Let’s ask Amazon Q “Can you give me the total sales for 1998, from different sales channels, using a union of the sales data from different channels?” Let’s dive deeper into some other capabilities of Amazon Q generative SQL. Let’s try logging in with a different user and see how Amazon Q generative SQL interacts with that user. We have created User3 and granted the sys:monitor Logged in as User3, let’s ask the original question of “What are the top 10 stores in sales in 1998?” Amazon Q generative SQL is able to use the query history and provide SQL recommendations for User3’s prompts because they have access to the system metadata provided through the role sys:monitor. Safety f