Improving SQL query performance is important in applications or systems. Improving SQL query performance helps execute queries faster, leading to faster data access. Applications with optimized SQL queries provide a better user experience.

Many solutions can improve SQL query performance such as: adding an index, avoiding using subqueries, using a partition, avoiding query in a loop, selecting fields instead of selecting *, using a matching join instead of a subqueries, etc. In this article, I will introduce some tips to improve SQL performance in SELECT and INSERT queries. It may be helpful for newbies.

  1. Use SELECT fields instead of SELECT *
    Retrieving all columns with SELECT * may result in unnecessary data transfer between the database and the application. This can lead to increased network traffic and slower query performance, especially when dealing with large datasets. So, we should only select the necessary fields.



  2. Use Join instead of subquery

    Using subqueries in SQL is not inherently bad, and they can be one of the best ways to express complex logic in queries. However, there are situations where using a subquery can impact performance, and developers should be cautious in these situations.

    In many cases, JOINs can be more efficient than subqueries, especially when dealing with large datasets. In such cases, alternatives like JOINs can often lead to better performance.



  3. Avoid query in a loop

    Executing frequent queries in a loop can place a significant load on the database server. This loading can lead to performance degradation, slow response times, etc. In a loop, if a query is performed repeatedly to retrieve from the database, consider fetching all necessary data in a query before entering the loop, which reduces the number of interactions with the database.

    Avoiding queries in loops is one of the best ways that improve the efficiency and performance of your application.

  4. Insert multiple rows using a query statement

    Improving the performance of SQL INSERT operations is important for efficient data loading, especially in situations where large amounts of data need to be inserted.

    Instead of performing individual INSERT statements for each record, consider using batch inserts. Batch inserts allow you to insert multiple records with a single SQL statement, reducing the number of round-trips to the database and improving overall performance.

    Batch inserts can lead to improved performance, especially when inserting a large volume of data. The overhead associated with processing each INSERT statement is minimized, resulting in faster data insertion.



    Note: There are several important considerations and notes when using batch insert with large amounts of data. That is out of memory in the server.

    Example:  When inserting 20 thousands data from csv file, the memory problem will occur if the configuration memory setting in your server is low. The problem is as below.

    To resolve this issue, you can review and adjust the memory configuration settings for your server or divide the data into smaller parts.

    Now I will introduce the solution of dividing the data into smaller parts.

    The data is stored in the csv file as below:


    I will divide the data in the file into 3 parts, and in each part I will insert 2 records into the database:

    Loop1 (part1): Read file from line 2 to line 3 -> insert into database

    Loop2 (part2): Read file from line 4 to line 5 -> insert into database

    Loop3 (part3): Read file from line 6 to line 7 -> insert into database

    The code I have implemented is as below:

    When you need to insert large amounts of data, Instead of inserting all the data into one transaction, split the data into smaller batches. This helps manage memory usage more effectively.

    In summary, optimizing SQL queries can significantly improve query performance and enhance the overall efficiency of the database. It directly impacts user satisfaction. Regular performance tuning and optimization are essential components to maintaining a high-performance system.

    [Reference]
    https://www.techagilist.com/mainframe/db2/sql-query-optimization-tips-tricks/

Leave a comment

*