Tune NL2SQL performance in Data Copilot
DMS Data Copilot is an intelligent data assistant from DMS that is built on Alibaba Cloud's Large Language Models (LLMs). It combines the expert data management capabilities of DMS to help developers, O&M personnel, product managers, operations staff, analysts, and database administrators use and manage data more efficiently and in a consistent manner. This topic describes how to use Data Copilot.
Public preview
Data Copilot is in public preview. During the public preview, model resources are free of charge. If you have any questions, join the DingTalk group (ID: 44962304) to contact DMS technical support.
To improve the accuracy of the answers that Copilot provides, DMS needs to retrieve metadata from the database instance. DMS does not access your actual data. Therefore, you must enable security hosting for your instance.
Limits
Data Copilot builds prompts based on metadata. Because of model limitations, for databases that contain a large amount of data, Copilot samples only a portion of the metadata, such as tables and fields. This may cause some information to be ignored.
Therefore, Copilot may not perform as well on large databases as it does on small databases.
Data Copilot understands a database based on the names and comments of its tables and columns. Therefore, using standard names and comprehensive comments for tables and columns improves the accuracy of the results.
Try Copilot for free
Alibaba Cloud provides a DB+AI experience center for database solutions. You can try the natural language to SQL (NL2SQL) and chart recommendation features of DMS Data Copilot online without needing to purchase any resources. This lets you experience the intelligent service capabilities of DMS products. For more information, see Try ChatBI on DMS for free.
Use Copilot in the SQL Window
Prerequisites
Security hosting is enabled for the instance.
You have the query permissions for the target database. To view your permissions, see View my permissions.
Entry points
Log in to DMS 5.0.
Move the pointer over the
icon in the upper-left corner of the DMS console and choose . NoteIf you use the DMS console in normal mode, choose in the top navigation bar.
In the Please select the database first dialog box, enter a keyword to search for a database, select the database instance from the search results, and then click Confirm.
At the top of the page, click the
icon to start using Copilot. For demonstrations of Copilot features, see Copilot feature demonstrations.Copilot searches within the scope of all tables in the current database.
Copilot feature demonstrations
Generate SQL
Generate SQL statements using the intelligent assistant in the SQL Console
Enter a comment in the SQL Console and press Enter to trigger the intelligent assistant to generate an SQL statement.
The comment must be in the following format: two hyphens (
-) followed by a space and a natural language query. For example,-- Find the city where Xiao Ming bought a phone.Press the Tab key to accept the generated SQL statement.
For example, find the city where Xiao Ming bought a phone and display the phone model.
-- Find the city where Xiao Ming bought a phone and display the phone model.
SELECT
city,
item_id AS phone_model
FROM
orders_test
WHERE
buyer_name = 'Xiao Ming'
AND item_id LIKE '%phone%';Generate SQL statements in the Copilot dialog box
Enter a natural language query for data search and analysis.
After you ask a question, Copilot analyzes the table comments and the question to select the tables required to answer it.
Copilot displays the associated tables it found and their descriptions. You can click View to see the details of a table, or click Regenerate and Adjust Tables to modify the table selection.
If Copilot is highly confident about the selected tables, it automatically proceeds to generate the SQL statement.
NoteIf Copilot cannot determine which tables contain the data that you need, or if the selected tables do not meet your requirements, you must manually click Adjust Tables to select and add the appropriate tables.
Optional: View the details of a table.
In the associated tables area, click View to see the table's information. This includes the table data, the database where the table resides, the number of rows, your database permissions, and the table's field and index information.
Copilot generates an SQL statement.
If you are satisfied with the generated SQL statement, you can copy it to the SQL Console or a Notebook to run it.
If you are not satisfied with the generated SQL statement, you can Provide Feedback. Copilot modifies the SQL statement based on your feedback and automatically extracts the business knowledge implied in your feedback.
After Data Copilot generates an SQL statement, the interface displays the SQL query with syntax highlighting and comments on each line. A Run Query button is provided above the query. Below the query, a Referenced Knowledge Base label is displayed, along with a This SQL was generated based on the following business knowledge. Please verify section that lists the mapping between business knowledge and SQL conditions (for example, the "Supporter" badge corresponds to
b.Name = 'Supporter'). You can click Edit to modify the knowledge. At the bottom, the To Be Reviewed status is shown with thumbs up/down rating buttons, along with Regenerate and Adjust Tables options.
NL2SQL
NL2SQL supports thinking templates. This feature lets you view the thought process that Copilot uses to generate an SQL statement. You can also add new thinking templates to the knowledge base or edit existing ones for the AI to use as a reference. This improves the accuracy and interpretability of query results.
The following example shows the AI's thought process:
If you ask Copilot to query which city Xiao Hong bought a phone in, the AI finds the relevant tables and explains why it selected them. It provides an analysis based on measures (metrics) and dimensions (filter conditions), and then generates an SQL statement.
Manage business knowledge
Add knowledge
DMS supports several ways to build business knowledge. You can accumulate knowledge through Q&A interactions in the Copilot interface, add knowledge manually, or build knowledge through the data augmentation pipeline. After business knowledge is generated, Copilot references it when generating SQL statements and indicates where the knowledge was used.
Manually add knowledge
Log in to DMS 5.0.
-
In the upper-left corner of the console, click the
icon and choose .NoteIf you are using the classic console, choose from the top menu bar.
-
On the Knowledge Granularity tab, click Add.
-
In the Add Knowledge panel, enter the Description and Content, and provide a chain of thought. Select a knowledge type, knowledge level, and associated database.
The chain of thought parameter appears only when you set knowledge type to SQL record. For optimal performance, follow the template that DMS provides.
The following example shows a chain of thought (thought template):
Metric: The user asked about the academic performance of schools in California and wants to know which school is the best. Based on the context, the total average score for each school needs to be calculated. Dimension (Filter Condition): The user's question does not mention any specific filter conditions. The core dimension is the school name. In summary, this requires using the satscores table and the sname column. sname will be used as the projection. Calculate the total average score for sorting. No grouping is involved. -
Click OK.
Automatically generate knowledge
-
Copilot creates knowledge when you like or provide feedback on the information it generates. For more information, see Data Copilot (New).
-
The system automatically adds liked SQL queries and their corresponding questions to the knowledge base as SQL records with a knowledge level of pending review.
-
Your feedback and modified SQL are automatically added to the knowledge base as knowledge snippets, with the knowledge level set to pending review.
-
-
Build knowledge through the data augmentation pipeline.
Verify knowledge
On the Business Knowledge tab of the Table Details page, you can review the generated business knowledge. If the knowledge is correct, you can change the Knowledge Level from To Be Reviewed or To Be Verified to Verified.
NoteOnly database developers and table owners have permission to verify knowledge.
Adjust knowledge
If the business knowledge is incorrect, you can edit the knowledge in the referenced knowledge base section of the SQL response, or adjust it in the knowledge base. For more information about how to adjust knowledge in the knowledge base, see Knowledge base.
Manage SQL knowledge
To reduce AI errors when you use Copilot, DMS uses a knowledge base and a similar query matching mechanism.
In the DMS Data Copilot interface, after you enter a natural language question (such as "How many posts were published this year?"), the system automatically generates the corresponding SQL query with annotation comments. Below the generated result, the Referenced Knowledge Base entries and Similar Questions recommendations are displayed. You can click Run Query to run the generated SQL.
SQL auto-completion
After you enter part of an SQL statement, the intelligent assistant automatically completes the statement. This typically takes 1 to 4 seconds. The wait time depends on the length of the completed code.
Press the Tab key to accept the auto-completed SQL statement.
The DMS SQL editor also supports NL2SQL: enter a -- comment in the editor and press Enter to trigger the intelligent assistant, which automatically converts a natural language question (such as -- Which tables are in the dmsdb01 database?) into an SQL statement (such as SELECT * FROM `test` LIMIT 20;). The editor also provides Optimize, Comment, and Fix features that offer intelligent optimization suggestions for SQL statements you have entered.
Manage similar SQL queries
Copilot generates SQL by referencing past questions, whereas Agent uses only verified knowledge.
Generate similar questions
If you are satisfied with the SQL statement that Copilot generated, click the like icon in the lower-right corner. This saves the query record. If you ask a similar question later, Copilot uses the similar question as a reference to generate an SQL statement.
Delete saved similar questions
Double-click a table name to go to the Table Details page. On the Historical Questions tab, delete the question.
Recommended Reports
After you run a SELECT statement, click Chart on the left side of the results to display the data in a chart. You can also perform the following operations:
Download the chart: In the upper-right corner of the chart area, click Download Image.
Use an intelligently recommended chart: In the upper-right corner of the chart area, click Change. If you select a recommended chart, your current configuration is cleared.
Apply the chart to a dashboard. For more information about dashboards, see Dashboards.
In the left panel, select a chart type (such as a bar chart), and then select the fields to display in the X-axis and Y-axis drop-down lists.
Related operations
Use an AI agent
After a data developer uses Copilot to query data, add knowledge, or tune NL2SQL performance, they can use an AI agent. The agent can handle data Q&A requests from product managers, operations personnel, or even external parties. The agent retrieves data through natural language interactions. Users do not need to be familiar with tables or SQL. They can simply ask questions to obtain the results they need.
Build a ChatBI application using Dify and NL2SQL
Dify on DMS+NL2SQL provides a complete ChatBI solution that makes complex data analysis accessible. You only need to ask questions directly. DMS ChatBI can then understand your intent through powerful AI tools and convert it into an executable SQL statement using NL2SQL. Using the dedicated plugin provided by DMS, the SQL statement is sent to the target database for execution, and the analysis results are displayed in clear, intuitive charts.
Manage a knowledge base
The knowledge in a knowledge base helps the Copilot intelligent assistant better understand and answer questions, and implement automated decision-making and inference.
FAQ
Q: If I am satisfied with the SQL statement that Copilot generated, why should I like it?
A: Liking the SQL statement prompts Copilot to save it. This greatly improves the accuracy of answers to similar questions in the future.
Q: If the SQL statement that Copilot generated does not match my question, why should I provide feedback?
A: Providing feedback improves the accuracy of Copilot's answers.