[Solved] How to optimise a sqlite_3 query?

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. Avoid Subqueries (query line: 24): 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.
  2. Avoid Subqueries (query line: 30): 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.
  3. Avoid Subqueries (query line: 40): 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.
  4. Avoid Subqueries (query line: 47): 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.
  5. Avoid Subqueries (query line: 53): 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.
  6. Avoid Subqueries (query line: 63): 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.
  7. Avoid Subqueries (query line: 69): 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. Use Numeric Column Types For Numeric Values (query line: 7): Referencing a numeric value (e.g. 123) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  10. Use Numeric Column Types For Numeric Values (query line: 15): Referencing a numeric value (e.g. 123) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  11. Use Numeric Column Types For Numeric Values (query line: 16): Referencing a numeric value (e.g. 2) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
ALTER TABLE `available_material_printmode_config` ADD INDEX `available_printmod_idx_material_name` (`material_name`);
ALTER TABLE `available_material_printmode_config` ADD INDEX `available_printmod_idx_printer_name_printmode_name` (`printer_name`,`printmode_name`);
ALTER TABLE `available_materials` ADD INDEX `available_material_idx_material_name` (`material_name`);
The optimized query:
WITH printercheck AS (SELECT
        EXISTS (SELECT
            1 
        FROM
            available_material_printmode_config 
        WHERE
            available_material_printmode_config.printer_name = '123' LIMIT 1) AS p),
        materialcheck AS (SELECT
            CASE 
                WHEN available_material_printmode_config.material_name IS NULL THEN 0 
                ELSE 1 END AS m 
FROM
available_material_printmode_config 
WHERE
available_material_printmode_config.printer_name = '123' 
AND available_material_printmode_config.printmode_name = '2' LIMIT 1) SELECT
m.name 
FROM
material m 
LEFT JOIN
available_material_printmode_config ac 
    ON ac.material_name = m.name 
    AND (
        SELECT
            printercheck.p 
    FROM
        printercheck LIMIT 1
) 
AND (
    SELECT
        m 
    FROM
        materialcheck LIMIT 1
) 
LEFT JOIN
available_materials am1 
    ON am1.material_name = m.name 
    AND (
        (
            SELECT
                printercheck.p 
        FROM
            printercheck LIMIT 1
    ) != 1 
    OR (
        (
            SELECT
                printercheck.p 
            FROM
                printercheck LIMIT 1
        ) 
        AND (
            SELECT
                m 
            FROM
                materialcheck LIMIT 1
        ) != 1
    )
) 
WHERE
CASE 
    WHEN (
        SELECT
            printercheck.p 
        FROM
            printercheck LIMIT 1
    ) 
    AND (
        SELECT
            m 
        FROM
            materialcheck LIMIT 1
    ) THEN ac.printer_name = '123' 
    AND ac.printmode_name = '2' 
    ELSE am1.printer_name = '123' END

Related Articles



* original question posted on StackOverflow here.