Optimizing SQL using Generative AI

Objective

After completing this lesson, you will be able to use Generative AI to Optimize SQL Statements in SAP HANA Cloud Central - SQL Console

Optimizing SQL using Generative AI

In today's data-driven enterprise environment, SQL query performance is critical to maintaining efficient database operations and reducing infrastructure costs. SAP HANA Cloud Central's SQL Console provides a powerful interface for executing and managing queries, but even experienced database professionals often struggle to identify optimization opportunities in complex SQL statements.

Generative AI has emerged as a transformative tool that can analyze your SQL code, identify performance bottlenecks, suggest index strategies, and recommend query rewrites—all without requiring deep expertise in query optimization. By leveraging generative AI capabilities within or alongside your SQL Console workflow, you can accelerate development cycles, improve query execution times, reduce system load, and ultimately maximize the performance and cost-efficiency of your SAP HANA Cloud investment.

Using Generative AI to Optimize SQL

In the SAP HANA Cloud Central - SQL Console it is now possible to let the generative AI explain SQL code in the SAP HANA Cloud Central - SQL Console. In the SQL Console the Generative AI will explain the selected SQL statements for you when you use the function Explain SQL Statement using AI as shown in the image.

The image shows the option Optimize SQL Statement using AI function available in the SQL Console.

An Example of a Common SQL Mistake

As a start lets try to retrieve customer and order information from the CUSTOMER and ORDERS tables. The following field need to be retrieved from the two tables:

  • Customer ID
  • First Name
  • Last Name
  • Email Address
  • Order ID
  • Country
  • Total Amount
  • Order Status
  • Customer Type

The results should only show the premium customers located in Spain who have orders with a status of "Hold". The SQL could look like this:

SQL
123456789101112
SET SCHEMA HC2AI_DEMO; SELECT CUSTOMER.CUSTOMER_ID, CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME, CUSTOMER.EMAIL, ORDERS.ORDER_ID, CUSTOMER.COUNTRY, ORDERS.TOTAL_AMOUNT, ORDERS.ORDER_STATUS, CUSTOMER.CUSTOMER_TYPE FROM ORDERS, CUSTOMER WHERE ORDERS.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID AND CUSTOMER.COUNTRY = 'SPAIN' AND CUSTOMER.CUSTOMER_TYPE = 'PREMIUM' AND ORDERS.ORDER_STATUS = 'Hold'; Result Set Rows: 321

Using the option Optimize SQL Statement using AI generates the following SQL statement:

SQL
12345678910111213141516171819202122232425262728
-- AI Optimization Result: -- The query was optimized by replacing the implicit join with an explicit INNER -- JOIN, which is more efficient and readable in SAP HANA. Table aliases were -- introduced for clarity. Filters on CUSTOMER and ORDERS are pushed as early as -- possible to minimize the number of rows processed. These changes improve query -- performance and maintain the original semantics. SET SCHEMA HC2AI_DEMO; SELECT C.CUSTOMER_ID, C.FIRST_NAME, C.LAST_NAME, C.EMAIL, O.ORDER_ID, C.COUNTRY, O.TOTAL_AMOUNT, O.ORDER_STATUS, C.CUSTOMER_TYPE FROM CUSTOMER C INNER JOIN ORDERS O ON O.CUSTOMER_ID = C.CUSTOMER_ID WHERE C.COUNTRY = 'SPAIN' AND C.CUSTOMER_TYPE = 'PREMIUM' AND O.ORDER_STATUS = 'Hold'; -- Optimization Summary: -- 1. Replaced comma-separated tables with explicit INNER JOIN for better readability and -- performance. -- 2. Introduced table aliases (C for CUSTOMER, O for ORDERS) to improve clarity. -- 3. Pushed all filters into the WHERE clause as early as possible to reduce data volume -- before the join. -- 4. Only necessary columns and tables are included, and no unnecessary grouping or ordering -- is performed. Result Set Rows: 321

The Optimize SQL Statement using AI function changes the query to an INNER JOIN for better performance, introduces table aliases for better readability, optimizes the WHERE clause. The optimized SQL statement is certainly more readable, the performance improvement for this specific example wasn't huge.

Example: Optimize the Delta Merge Analysis script using AI in SQL Console

As an bit more complicated SQL example to optimize I choose the Delta Merge Analysis script found in the SAP HANA Cloud Central - SQL Console - Statement Library. In the image the option Optimize SQL Statement using AI is used to optimize this SQL Script.

The image shows an example how you can optimize the Delta Merge Analysis script using the Optimize SQL Statement using AI functionality in the SQL Console.

The table shows the average runtime of 5 executions before and after optimization. In both scenarios the first execution was discarded from the results.

Before Optimization

RunExecution Time (ms)Row selected
12.54159
23.38559
32.50359
42.49359
52.69559
Average2.723 

After Optimization

RunExecution Time (ms)Row selected
12.13859
22.12259
32.4.8759
42.26659
52.17859
Average2.238 

The optimized Delta Merge Analysis script runtime is 19.55% shorter then the non-optimized version.

Summary

In this lesson you learned about the "Optimize SQL Statement using AI" available in the SAP HANA Cloud Central - SQL Console. Using the "Optimize SQL Statement using AI" could improve the performance by using joins, introduces table aliases for better readability, optimizes the SQL statement by making sure the amount of selected data was reduced by filtering via the WHERE clause as early as possible..