Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Right I've tried to do what I can. Mostly some reordering, some swapping to INNER JOINs and movement of WHERE syntax to the joins. Some renaming of aliases because you had used alias names matching table names negating their purpose.</p> <p>This will return rows where there are no matching transactions, so you might want to change that join to be INNER also, rather depends on the purpose / intended output, but this should be a good starting point. It allows MySQL to reduce the number of rows it looks at.</p> <p>Further improvement can be had by suitable indexes, but advising on these is hard without knowing data types / the variance in the data etc.</p> <pre><code>SELECT DISTINCT cnt.last_name, cnt.bus_ph_num, cnt.email_addr, u.login, cnt.fst_name, cnt.fk_id FROM contact cnt -- Changed to INNER JOIN as the WHERE indicates this is required INNER JOIN party p ON p.pk_id=cnt.fk_id -- Moved this limiting statement to the join AND p.fk_id= 'xyz' -- Changed to INNER JOIN as the LIKE % indicates htis is required INNER JOIN user u ON u.fk_id=cnt.fk_id -- OP added later, quite inefficient due to use of wildcards on both sides causing scans AND u.login LIKE '%somevalue%' -- Also here, INNER, as the WHERE means this is required INNER JOIN role r ON r.pk_row_id = u.role_id -- Moved this limiting statement to the join AND r.role_name ='ROLE_ADMIN' LEFT JOIN transaction tran ON tran.id=cnt.fk_id -- Moved this limiting statement to the join AND tran.txn_typ IN ( 1 , 2 , 3 ) -- OP added later, quite inefficient due to use of wildcards on both sides causing scans AND tran.txn_trk_num LIKE '%somevalue%' WHERE cnt.status_cd = 1 -- OP added later, quite inefficient due to use of wildcards on both sides causing scans AND cnt.last_name LIKE '%somevalue%' AND cnt.email_addr LIKE '%somevalue%' AND cnt.busin_ph_num LIKE '%somevalue%' </code></pre> <p>If you can get rid of those LIKE statements it will get better also. The following are able to use an index if a suitable one is available :</p> <ul> <li><code>... LIKE 'somevalue'</code></li> <li><code>... = 'somevalue'</code></li> <li><code>... LIKE 'somevalue%'</code></li> </ul> <p>However <code>... LIKE '%somevalue%'</code> is unable to use an index</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload