[Solved] How to increase perfromance of SQL query for selecting from 2 tables

EverSQL Database Performance Knowledge Base

How to increase perfromance of SQL query for selecting from 2 tables

Database type:

Please help me, how to speed up this sql query?

SELECT pa.*
FROM ParametrickeVyhladavanie pa, 
     (SELECT p.*
     FROM produkty p
     WHERE p.KATEGORIA IN ('$categoryArray')) produkt
WHERE produkt.ATTRIBUTE_CODE LIKE CONCAT('%', pa.code, '%')
AND produkt.ATTRIBUTE_VALUE LIKE CONCAT('%', pa.ValueCode, '%')
GROUP BY pa.code

Indexes: pa.code, pa.ValueCode, p.ATTRIBUTE_CODE, p.ATTRIBUTE_VALUE

Showing rows 0 - 25 ( 26 total, Query took 20.4995 sec)

EDIT Actual code:

SELECT pa.*
FROM ParametrickeVyhladavanie pa
WHERE EXISTS
(
   SELECT 1 FROM produkty p
   JOIN 
   PRODUCT_INFO AS pi
   ON p.ProId = pi.ProduktID
   AND p.KATEGORIA IN ('Mobily'))

AND pi.ATTRIBUTE_CODE = pa.AttributeCode
AND pi.ATTRIBUTE_VALUE = pa.ValueCode
GROUP BY pa.code

This code says error #1054 - Unknown column 'pi.ATTRIBUTE_CODE' in 'where clause' The pi. table working only between ( and )

EDIT - THIS IS ANSWER

I changed MySQL 5.1 to MariaDB 5.5 and its faster!!!

How to optimize this SQL query?

The following recommendations will help you in your 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. Avoid Calling Functions With Indexed Columns (query line: 14): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `code` is indexed, the index won’t be used as it’s wrapped with the function `CONCAT`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 15): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `ValueCode` is indexed, the index won’t be used as it’s wrapped with the function `CONCAT`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid LIKE Searches With Leading Wildcard (query line: 14): The database will not use an index when using like searches with a leading wildcard (e.g. '%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  4. Avoid LIKE Searches With Leading Wildcard (query line: 15): The database will not use an index when using like searches with a leading wildcard (e.g. '%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  5. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  6. Avoid Selecting Unnecessary Columns (query line: 6): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  7. Avoid Subqueries (query line: 5): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  8. 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.
  9. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `ParametrickeVyhladavanie` ADD INDEX `parametrickevyhlad_idx_code` (`code`);
ALTER TABLE `produkty` ADD INDEX `produkty_idx_kategoria` (`KATEGORIA`);
The optimized query:
SELECT
        pa.* 
    FROM
        ParametrickeVyhladavanie pa,
        (SELECT
            p.* 
        FROM
            produkty p 
        WHERE
            p.KATEGORIA IN (
                '$categoryArray'
            )) produkt 
    WHERE
        produkt.ATTRIBUTE_CODE LIKE CONCAT('%', pa.code, '%') 
        AND produkt.ATTRIBUTE_VALUE LIKE CONCAT('%', pa.ValueCode, '%') 
    GROUP BY
        pa.code 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.