[Solved] Differences between equal sign(=) and IN with subquery

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: 34): 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: 45): 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. 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.
  4. Use Numeric Column Types For Numeric Values (query line: 8): Referencing a numeric value (e.g. 01) 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.
  5. Use Numeric Column Types For Numeric Values (query line: 9): Referencing a numeric value (e.g. 01) 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.
  6. Use Numeric Column Types For Numeric Values (query line: 15): Referencing a numeric value (e.g. 01) 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.
  7. Use Numeric Column Types For Numeric Values (query line: 16): Referencing a numeric value (e.g. 01) 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.
  8. Use Numeric Column Types For Numeric Values (query line: 23): Referencing a numeric value (e.g. 01) 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.
  9. Use Numeric Column Types For Numeric Values (query line: 24): Referencing a numeric value (e.g. 01) 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: 28): Referencing a numeric value (e.g. 01) 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: 29): Referencing a numeric value (e.g. 01) 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.
  12. Use Numeric Column Types For Numeric Values (query line: 41): Referencing a numeric value (e.g. 01) 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.
  13. Use Numeric Column Types For Numeric Values (query line: 42): Referencing a numeric value (e.g. 01) 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.
  14. Use Numeric Column Types For Numeric Values (query line: 52): Referencing a numeric value (e.g. 01) 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.
  15. Use Numeric Column Types For Numeric Values (query line: 53): Referencing a numeric value (e.g. 01) 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 `OR_1INSUMOS` ADD INDEX `or_1insumos_idx_empres_filial_numero_opcao_cod_ma` (`EMPRESA`,`FILIAL`,`NUMERO`,`OPCAO_SIMULACAO`,`COD_INSUMO_MATER`);
ALTER TABLE `OR_1SIMULACOES` ADD INDEX `or_1simulacoes_idx_numero_opcao_empresa_filial` (`NUMERO`,`OPCAO_SIMULACAO`,`EMPRESA`,`FILIAL`);
ALTER TABLE `OR_MATERIAIS` ADD INDEX `or_materiais_idx_empresa_filial_id` (`EMPRESA`,`FILIAL`,`ID`);
ALTER TABLE `OR_MAT_GRUPOS` ADD INDEX `or_grupos_idx_empresa_filial_codigo_id` (`EMPRESA`,`FILIAL`,`CODIGO_INTERNO`,`ID`);
ALTER TABLE `OR_MAT_LIGACAO` ADD INDEX `or_ligacao_idx_empresa_filial_codigo_cod_fam` (`EMPRESA`,`FILIAL`,`CODIGO_MATERIAL`,`COD_MAT_FAMILIA`);
The optimized query:
SELECT
        MATLIGA.COD_MAT_FAMILIA 
    FROM
        ORCAMENTOS.dbo.OR_1INSUMOS INSUMOS 
    INNER JOIN
        ORCAMENTOS.dbo.OR_MAT_GRUPOS GRUPOS 
            ON (
                GRUPOS.EMPRESA = '01' 
                AND GRUPOS.FILIAL = '01' 
                AND GRUPOS.CODIGO_INTERNO = 'HOT'
            ) 
    INNER JOIN
        ORCAMENTOS.dbo.OR_MATERIAIS MATER 
            ON (
                MATER.EMPRESA = '01' 
                AND MATER.FILIAL = '01' 
                AND MATER.CODIGO_GRUPO = GRUPOS.ID 
                AND MATER.ID = INSUMOS.COD_INSUMO_MATER
            ) 
    INNER JOIN
        ORCAMENTOS.dbo.OR_MAT_LIGACAO MATLIGA 
            ON (
                MATLIGA.EMPRESA = '01' 
                AND MATLIGA.FILIAL = '01' 
                AND MATLIGA.CODIGO_MATERIAL = INSUMOS.COD_INSUMO_MATER
            ) 
    WHERE
        INSUMOS.EMPRESA = '01' 
        AND INSUMOS.FILIAL = '01' 
        AND INSUMOS.COD_INSUMO_MATER IS NOT NULL 
        AND INSUMOS.NUMERO = 10865812 
        AND INSUMOS.OPCAO_SIMULACAO = 1 
        AND INSUMOS.CODIGO_MAQUINA = (
            SELECT
                ORC.COD_MAQ_PROPOSTA 
            FROM
                ORCAMENTOS.dbo.OR_1SIMULACOES AS ORC 
            WHERE
                ORC.NUMERO = 10865812 
                AND ORC.OPCAO_SIMULACAO = 1 
                AND ORC.EMPRESA = '01' 
                AND ORC.FILIAL = '01'
        ) 
        AND INSUMOS.OPCAO_MAQUINA = (
            SELECT
                ORC.OPCAO_MAQUINA 
            FROM
                ORCAMENTOS.dbo.OR_1SIMULACOES AS ORC 
            WHERE
                ORC.NUMERO = 10865812 
                AND ORC.OPCAO_SIMULACAO = 1 
                AND ORC.EMPRESA = '01' 
                AND ORC.FILIAL = '01'
        ) 
    GROUP BY
        MATLIGA.COD_MAT_FAMILIA 
    ORDER BY
        1

Related Articles



* original question posted on StackOverflow here.