Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformance Penalties for Unused Joins
    primarykey
    data
    text
    <p>I'm writing a script that generates a report based on a query that uses several tables joined together. One of the inputs to the script is going to be a list of the fields that are required on the report. Depending on the fields requested, some of the tables might not be needed. My question is: is there a [significant] performance penalty for including a join when if it is not referenced in a SELECT or WHERE clause?</p> <p>Consider the following tables:</p> <pre><code>mysql&gt; SELECT * FROM `Books`; +----------------------+----------+ | title | authorId | +----------------------+----------+ | Animal Farm | 3 | | Brave New World | 2 | | Fahrenheit 451 | 1 | | Nineteen Eighty-Four | 3 | +----------------------+----------+ mysql&gt; SELECT * FROM `Authors`; +----+----------+-----------+ | id | lastName | firstName | +----+----------+-----------+ | 1 | Bradbury | Ray | | 2 | Huxley | Aldous | | 3 | Orwell | George | +----+----------+-----------+ </code></pre> <p>Does</p> <pre><code>SELECT `Authors`.`lastName` FROM `Authors` WHERE `Authors`.`id` = 1 </code></pre> <p>Outperform:</p> <pre><code>SELECT `Authors`.`lastName` FROM `Authors` JOIN `Books` ON `Authors`.`id` = `Books`.`authorId` WHERE `Authors`.`id` = 1 </code></pre> <p>?</p> <p>It seems to me that MySQL should just know to ignore the JOIN completely, since the table is not referenced in the SELECT or WHERE clause. But somehow I doubt this is the case. Of course, this is a really basic example. The actual data involved will be much more complex.</p> <p>And really, it's not a terribly huge deal... I just need to know if my script needs to be "smart" about the joins, and only include them if the fields requested will rely on them.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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