Use Natural Language to Query Your Database with Chat2DB

Jing - Sep 19 - - Dev Community

In the past, querying databases required specialized knowledge in SQL, making data access a challenge for non-technical professionals.This barrier often hindered the ability of business teams to derive timely insights from their data, slowing down decision-making processes and creating bottlenecks in workflows. The technical complexity involved not only limited immediate data retrieval but also restricted the broader use of data-driven strategies across departments that were not proficient in SQL.

The shift began with the advent of natural language processing (NLP) tools, which revolutionized how users interact with databases by turning complex queries into simple conversational exchanges.

workflow

In this blog, I will show you the steps to make use of the Text-to-SQL functionality of Chat2DB.

Before we dive into using Chat2DB, ensure that you have the following:

  • A compatible database system installed (e.g., MySQL, PostgreSQL).
  • Chat2DB application installed or access to its web interface.
  • The sample ERP database set up and populated with data.

Getting started with MySQLDB

For this demonstration, I have prepared a simple ERP (Enterprise Resource Planning) database.

To give you some context, the ERP database is designed to integrate all aspects of a business, including planning, purchasing, inventory, sales, marketing, and human resources. For our purposes today, we will focus on demonstrating how Chat2DB's Text-to-SQL feature can simplify the process of interacting with such a database using natural language queries.

1. Connecting to the Database

Launch the Chat2DB application and connect it to your ERP database by providing the necessary connection details such as the database name, host name, port, username, and password.

link to database
link to database

2. Creating an AI Data Collection

To enable the Text-to-SQL feature, you need to create an AI data collection that Chat2DB can use to understand natural language inputs and convert them into SQL queries.

ai data collection

3. Using Natural Language Queries

Once connected, you can start leveraging the Text-to-SQL feature.

  1. Open Query Console: Right-click on the database name in the left panel or select it from the database list.
  2. Access Query Console: Click on "Query Console" to open the query console window.
  3. Activate AI: In the query console, use the / command to activate the AI.

open qurey console

Instead of writing SQL queries manually, you can type in plain English what you want to achieve. For example, if you want to find out which employees have not submitted their monthly reports, you could simply ask:

"List all employees who have not submitted their monthly reports in 2024.8."

Chat2DB will interpret your request and generate the appropriate SQL query behind the scenes.

nl to sql

After executing the query, Chat2DB will display the results in a structured format.

SQL Optimization

Additionally, Chat2DB offers advanced features like SQL optimization, especially those that might otherwise perform poorly due to their complexity or structure, which enhance the tool's utility for both beginners and experienced database administrators.

Suppose we have a database with the following tables:

  1. orders table - containing order information:
    1. order_id
    2. customer_id
    3. order_date
    4. total_amount
  2. customers table - containing customer information:
    1. customer_id
    2. name
    3. email
  3. products table - containing product information:
    1. product_id
    2. product_name
    3. price
  4. order_details table - containing order details:
    1. order_id
    2. product_id
    3. quantity

Now, suppose we want to find all customers who placed orders in the first quarter of 2023 (January to March) and calculate the total amount spent by each customer. This is a typical multi-table join query that, if not handled properly, can become very slow.

Original Slow Query Example:

SELECT c.name, SUM(od.quantity * p.price) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_details od ON o.order_id = od.order_id
JOIN products p ON od.product_id = p.product_id
WHERE o.order_date BETWEEN '2023-01-01' AND '2023-03-31'
GROUP BY c.name
ORDER BY total_spent DESC;
Enter fullscreen mode Exit fullscreen mode

This query involves joining four tables (customers, orders, order_details, products) and requires summing up the total amount spent by each customer. Given the potentially large volume of data and the lack of proper indexing, such a query can become quite slow.

Optimize with Chat2DB

Let's see how Chat2DB can optimize it:

optimize sql

  1. Indexing: Ensure that there are indexes on the columns used in the JOIN conditions and WHERE clause. Specifically:
    1. customers.customer_id
    2. orders.customer_id
    3. orders.order_id
    4. order_details.order_id
    5. order_details.product_id
    6. products.product_id
    7. orders.order_date (if this field is not already indexed)
  2. Filter Early: If possible, filter the data as early as possible in the query to reduce the amount of data being processed. In this case, the WHERE clause is already well placed to filter orders by order_date before joining with other tables.
  3. Avoid SELECT: Instead of using SELECT *, specify only the columns needed. In this query, only c.name and the aggregate function are used, which is good.
  4. Use Subqueries: If the dataset is large, consider using subqueries to pre-aggregate data. This can sometimes reduce the amount of data being processed in the main query.
  5. Optimize JOINs: Make sure that the joins are necessary and optimized. Using explicit JOIN types (e.g., INNER JOIN, LEFT JOIN) can sometimes help the optimizer.

Optimized queries look like this:

SELECT
  c.name,
  SUM(od.quantity * p.price) AS total_spent
FROM
  customers c
  JOIN orders o ON c.customer_id = o.customer_id
  JOIN order_details od ON o.order_id = od.order_id
  JOIN products p ON od.product_id = p.product_id
WHERE
  o.order_date BETWEEN '2023-01-01' AND '2023-03-31'
GROUP BY
  c.name
ORDER BY
  total_spent DESC;
Enter fullscreen mode Exit fullscreen mode

By doing so, Chat2DB's AI SQL optimization feature can help automatically identify and improve query performance, thus enhancing query efficiency and reducing resource consumption.

Conclusion

These days, AI is at the forefront of technological innovation, with numerous tools being developed that harness the power of large language models. Chat2DB, offers real value by streamlining the process from conceptualizing data queries to conducting comprehensive analyses, making it a significant time-saver.


Community

Go to Chat2DB website
šŸ™‹ Join the Chat2DB Community
šŸ¦ Follow us on X
šŸ“ Find us on Discord

. .
Terabox Video Player