[Solved] SQL query in dire need of optimization
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

SQL query in dire need of optimization

I have this query, which works fine, except it takes a couple of minutes to load. I need help optimizing it so it runs faster and I don't know where to start:

SELECT 
    job_header.job,
    job_header.suffix,
    job_header.customer,
    job_header.description,
    job_header.comments_1,
    job_header.date_due,
    job_header.part,
    job_header.customer_po,
    job_header.date_closed,
    job_header.flag_hold,
    job_header.code_sort,
    wo_user_flds.user_7,
    wo_user_flds.user_3,
    wo_user_flds.user_6,
    wo_user_flds.user_5,
    wo_user_flds.user_2,
    quote_lines.user_2 as serialNo,
    quote_lines.user_3 as unit,
    quote_lines.user_4 as package

FROM job_header

LEFT JOIN wo_user_flds ON 
    (job_header.job = wo_user_flds.job) AND
    (job_header.suffix = wo_user_flds.suffix)

LEFT JOIN quote_lines ON
    (job_header.part = quote_lines.part)

WHERE job_header.date_closed = '000000'

AND LENGTH(job_header.job) > 5;

More information that might be of use:

I could speculate on what I think I need to do, but I'm really just guessing at this point. I looked at similar questions and lot of talk of 'indexes', so I checked and these tables do have some indexes...if that helps? Thanks in advance.

[EDIT]

Thanks for the quick responses guys, really appreciate it. I'm going to look into everything everyone said, but here is the ddl for these tables: http://paste.ubuntu.com/13247664/

[EDIT 2]

My query takes 1 minute to load. My expectations may not be realistic in how much faster it can be. I might have to resort to breaking up the query into more than one and then just assemble the data on the client.

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. 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. Index Function Calls Using Generated Columns (modified query below): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index to optimize the search. Creating and indexing a generated column (supported in MySQL 5.7) will allow MySQL to optimize the search.
  3. Use Numeric Column Types For Numeric Values (query line: 37): Referencing a numeric value (e.g. 000000) 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 `job_header` ADD INDEX `job_header_idx_date_closed_length_job` (`date_closed`,`length_job`);
ALTER TABLE `quote_lines` ADD INDEX `quote_lines_idx_part` (`part`);
ALTER TABLE `wo_user_flds` ADD INDEX `wo_flds_idx_job_suffix` (`job`,`suffix`);
The optimized query:
SELECT
        job_header.job,
        job_header.suffix,
        job_header.customer,
        job_header.description,
        job_header.comments_1,
        job_header.date_due,
        job_header.part,
        job_header.customer_po,
        job_header.date_closed,
        job_header.flag_hold,
        job_header.code_sort,
        wo_user_flds.user_7,
        wo_user_flds.user_3,
        wo_user_flds.user_6,
        wo_user_flds.user_5,
        wo_user_flds.user_2,
        quote_lines.user_2 AS serialNo,
        quote_lines.user_3 AS unit,
        quote_lines.user_4 AS package 
    FROM
        job_header 
    LEFT JOIN
        wo_user_flds 
            ON (
                job_header.job = wo_user_flds.job
            ) 
            AND (
                job_header.suffix = wo_user_flds.suffix
            ) 
    LEFT JOIN
        quote_lines 
            ON (
                job_header.part = quote_lines.part
            ) 
    WHERE
        job_header.date_closed = '000000' 
        AND job_header.length_job > 5

Related Articles



* original question posted on StackOverflow here.