Explore Your Data with AI-Assisted SQL Editor
You can use the built-in AI-assisted SQL Editor in the TiDB Cloud console to maximize your data value.
In SQL Editor, you can either write SQL queries manually or simply press ⌘ + I on macOS (or Control + I on Windows or Linux) to instruct Chat2Query (beta) to generate SQL queries automatically. This enables you to run SQL queries against databases without a local SQL client. You can intuitively view the query results in tables or charts and easily check the query logs.
Use cases
The recommended use cases of SQL Editor are as follows:
- Utilize the AI capabilities of Chat2Query to generate, debug, or rewrite complex SQL queries instantly.
- Quickly test the MySQL compatibility of TiDB.
- Easily explore the SQL features in TiDB using your own datasets.
Limitations
- SQL queries generated by the AI might not be 100% accurate, and you might need to refine them.
- SQL Editor is only supported for TiDB clusters that are v6.5.0 or later and hosted on AWS.
- SQL Editor is available by default for TiDB Cloud Serverless clusters. To use SQL Editor and Chat2Query on TiDB Cloud Dedicated clusters, contact TiDB Cloud support.
Access SQL Editor
Go to the Clusters page of your project.
Click your cluster name, and then click SQL Editor in the left navigation pane.
Enable or disable AI to generate SQL queries
PingCAP takes the privacy and security of users' data as a top priority. The AI capacity of Chat2Query in SQL Editor only needs to access database schemas to generate SQL queries, not your data itself. For more information, see Chat2Query Privacy FAQ.
When you access Chat2Query for the first time, you will be prompted with a dialog about whether to allow PingCAP and OpenAI to use your code snippets to research and improve the services.
- To enable the AI to generate SQL queries, select the checkbox and click Save and Get Started.
- To disable the AI to generate SQL queries, close this dialog directly.
After the first-time access, you can still change the AI setting as follows:
- To enable AI, click Enable AI power for data exploration in the upper-right corner of Chat2Query.
- To disable AI, click in the lower-left corner of the TiDB Cloud console, click Account Settings, click the Privacy tab, and then disable the AI-powered Data Exploration option.
Write and run SQL queries
In SQL Editor, you can write and run SQL queries using your own dataset.
Write SQL queries.
- macOS
- Windows/Linux
For macOS:
If AI is enabled, simply press ⌘ + I followed by your instructions and press Enter to let AI generate SQL queries automatically.
For a SQL query generated by Chat2Query, click Accept to accept the query and continue editing. If the query does not meet your requirements, click Discard to reject it. Alternatively, click Regenerate to request a new query from Chat2Query.
If AI is disabled, write SQL queries manually.
For Windows or Linux:
If AI is enabled, simply press Ctrl + I followed by your instructions and press Enter to let AI generate SQL queries automatically.
For a SQL query generated by Chat2Query, click Accept to accept the query and continue editing. If the query does not meet your requirements, click Discard to reject it. Alternatively, click Regenerate to request a new query from Chat2Query.
If AI is disabled, write SQL queries manually.
Run SQL queries.
- macOS
- Windows/Linux
For macOS:
If you have only one query in the editor, to run it, press ⌘ + Enter or click Run.
If you have multiple queries in the editor, to run one or several of them sequentially, select the lines of the target queries with your cursor, and then press ⌘ + Enter or click Run.
To run all queries in the editor sequentially, press ⇧ + ⌘ + Enter, or select the lines of all queries with your cursor and click Run.
For Windows or Linux:
If you have only one query in the editor, to run it, press Ctrl + Enter or click Run.
If you have multiple queries in the editor, to run one or several of them sequentially, select the lines of the target queries with your cursor, and then press Ctrl + Enter or click Run.
To run all queries in the editor sequentially, press Shift + Ctrl + Enter, or select the lines of all queries with your cursor and click Run.
After running the queries, you can see the query logs and results immediately at the bottom of the page.
Rewrite SQL queries using Chat2Query
In SQL Editor, you can use Chat2Query to rewrite existing SQL queries to optimize performance, fix errors, or meet other specific requirements.
Select the lines of SQL queries you want to rewrite with your cursor.
Invoke Chat2Query for rewriting by using the keyboard shortcut for your operating system:
- ⌘ + I on macOS
- Control + I on Windows or Linux
Press Enter after providing your instructions to let AI handle the rewrite.
After invoking Chat2Query, you can see the suggested rewrite and the following options:
- Accept: click this to accept the suggested rewrite and continue editing.
- Discard: click this if the suggested rewrite does not meet your expectations.
- Regenerate: click this to request another rewrite from Chat2Query based on your feedback or additional instructions.
Manage SQL files
In SQL Editor, you can save your SQL queries in different SQL files and manage SQL files as follows:
- To add a SQL file, click + on the SQL Files tab.
- To rename a SQL file, move your cursor on the filename, click ... next to the filename, and then select Rename.
- To delete a SQL file, move your cursor on the filename, click ... next to the filename, and then select Delete. Note that when there is only one SQL file on the SQL Files tab, you cannot delete it.
Access Chat2Query via API
In addition to accessing Chat2Query via UI, you can also access Chat2Query via API. To do so, you need to create a Chat2Query Data App first.
In Chat2Query, you can access or create a Chat2Query Data App as follows:
Click ... in the upper-right corner, and then click Access Chat2Query via API.
In the displayed dialog, do one of the following:
- To create a new Chat2Query Data App, click New Chat2Query Data App.
- To access an existing Chat2Query Data App, click the name of target Data App.
For more information, see Get started with Chat2Query API.
Generate an endpoint from a SQL file
For TiDB clusters, TiDB Cloud provides a Data Service (beta) feature that enables you to access TiDB Cloud data via an HTTPS request using a custom API endpoint. In SQL Editor, you can generate an endpoint of Data Service (beta) from a SQL file by taking the following steps:
- Move your cursor on the filename, click ... next to the filename, and then select Generate endpoint.
- In the Generate endpoint dialog box, select the Data App you want to generate the endpoint for and enter the endpoint name.
- Click Generate. The endpoint is generated and its details page is displayed.
For more information, see Manage an endpoint.
Manage SQL Editor settings
In SQL Editor, you can change the following settings:
- The maximum number of rows in query results
- Whether to show system database schemas on the Schemas tab
To change the settings, take the following steps:
- In the upper-right corner of SQL Editor, click ... and select Settings.
- Change the settings according to your need.
- Click Save.