[Solved] Hibernate query execution issue

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Sort and Limit Before Joining (modified query below): In cases where the joins aren't filtering any rows, it's possible to sort and limit the amount of rows using a subquery in the FROM clause, before applying the joins to all other tables.
  3. Use Equality Operator Over LIKE (modified query below): Equality operators (such as '\u003d') are usually better optimized and more readable. Prefer the equality operator when searching for a constant value such as `SecondMessage`.
Optimal indexes for this query:
ALTER TABLE `log` ADD INDEX `log_idx_creationtime` (`creationtime`);
ALTER TABLE `wsc_logrecords` ADD INDEX `wsc_logrecords_idx_message` (`message`);
The optimized query:
SELECT
        logid4_1_ AS logid4_1_,
        loglevel4_1_ AS loglevel4_1_,
        creation3_4_1_ AS creation3_4_1_,
        service17_4_1_ AS service17_4_1_,
        logtype4_1_ AS logtype4_1_,
        logrecord1_.logrecordid AS logrecor1_3_0_,
        logrecord1_.timestamp AS timestamp3_0_,
        logrecord1_.message AS message3_0_,
        logrecord1_.loglevel AS loglevel3_0_,
        logrecord1_.logid AS logid3_0_,
        logrecord1_.indexcolumn AS indexcol6_3_0_ 
    FROM
        (SELECT
            this_.logid AS logid4_1_,
            this_.loglevel AS loglevel4_1_,
            this_.creationtime AS creation3_4_1_,
            this_.serviceInitiatorID AS service17_4_1_,
            this_.logtype AS logtype4_1_ 
        FROM
            log this_ 
        ORDER BY
            this_.creationtime DESC LIMIT 25) this_ 
    INNER JOIN
        wsc_logrecords logrecord1_ 
            ON this_.logid4_1_ = logrecord1_.logid 
    WHERE
        (
            1 = 1
        ) 
        AND (
            1 = 1
        ) 
        AND logrecord1_.message = 'SecondMessage' LIMIT 25

Related Articles



* original question posted on StackOverflow here.