Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you studied the execution plan? I suspect the performance issue is deeper down than the final use of exists and it would help if the full query was available - or at least a full representation of its structure. In a comment you indicate this more elaborate structure:</p> <pre><code>SELECT 1 FROM table_A AS a LEFT JOIN ( -- &lt;&lt; why a left join, is it achieving anything? SELECT * -- &lt;&lt; not literally select * I hope FROM table_B AS b JOIN table_C AS c ON b.thing = c.thing WHERE B.ColumnA IN ( -- &lt;&lt; avoid this type of IN() SELECT -- &gt;&gt; if this result is big columnX FROM table_D AS d ) ) AS x ON a.thing = x.thing ; </code></pre> <p>At the outer query you are (or at least indicating) only doing 'select 1' so no extraneous fields need be selected in nested subqueries.</p> <p>You indicate the use of a left join, but so little is known about the details that I can't tell if it's needed or not. </p> <p>Possibly the most obvious place to look for improvement would be use of IN( subquery here ) if that subquery is producing a substantial number of rows performance will degrade. Either a join or the use of EXISTS is likely be a better choice for this.</p> <p>EDIT: with respect to getting an execution plan, consider running each subquery independently starting with the inner most nesting:</p> <pre><code> SELECT -- &gt;&gt; if this result is big columnX FROM table_D AS d </code></pre> <p>Is it using indexes, how many rows are returned (perhaps just do a count(*) for that)</p> <p>Work up from there to the next subquery, and consider alternatives. e.g. this might work:</p> <pre><code> SELECT thing -- &lt;&lt; only that which is needed FROM table_B AS b JOIN table_C AS c ON b.thing = c.thing JOIN ( SELECT distinct -- &gt;&gt; not a fan of distinct but might help here columnX FROM table_D ) AS D ON B.ColumnA = D.columnX </code></pre> <p>What does the execution plan of this piece tell you? </p> <p>Working up like this should aid the overall optimization. </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. 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