Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is mySQL query, left join 'considerably' faster than my inner join
    primarykey
    data
    text
    <p>I've researched this, but I still cannot explain why:</p> <pre><code>SELECT cl.`cl_boolean`, l.`l_name` FROM `card_legality` cl INNER JOIN `legality` l ON l.`legality_id` = cl.`legality_id` WHERE cl.`card_id` = 23155 </code></pre> <p>Is significantly slower than:</p> <pre><code>SELECT cl.`cl_boolean`, l.`l_name` FROM `card_legality` cl LEFT JOIN `legality` l ON l.`legality_id` = cl.`legality_id` WHERE cl.`card_id` = 23155 </code></pre> <p>115ms Vs 478ms. They are both using InnoDB and there are relationships defined. The 'card_legality' contains approx 200k rows, while the 'legality' table contains 11 rows. Here is the structure for each:</p> <pre><code>CREATE TABLE `card_legality` ( `card_id` varchar(8) NOT NULL DEFAULT '', `legality_id` int(3) NOT NULL, `cl_boolean` tinyint(1) NOT NULL, PRIMARY KEY (`card_id`,`legality_id`), KEY `legality_id` (`legality_id`), CONSTRAINT `card_legality_ibfk_2` FOREIGN KEY (`legality_id`) REFERENCES `legality` (`legality_id`), CONSTRAINT `card_legality_ibfk_1` FOREIGN KEY (`card_id`) REFERENCES `card` (`card_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; </code></pre> <p>And:</p> <pre><code>CREATE TABLE `legality` ( `legality_id` int(3) NOT NULL AUTO_INCREMENT, `l_name` varchar(16) NOT NULL DEFAULT '', PRIMARY KEY (`legality_id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1; </code></pre> <p>I could simply use LEFT-JOIN, but it doesn't seem quite right... any thoughts, please?</p> <p><strong>UPDATE:</strong> As requested, I've included the results of explain for each. I had run it previously, but I dont pretend to have a thorough understanding of it..</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE cl ALL PRIMARY NULL NULL NULL 199747 Using where 1 SIMPLE l eq_ref PRIMARY PRIMARY 4 hexproof.co.uk.cl.legality_id 1 </code></pre> <p>AND, inner join:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE l ALL PRIMARY NULL NULL NULL 11 1 SIMPLE cl ref PRIMARY,legality_id legality_id 4 hexproof.co.uk.l.legality_id 33799 Using where </code></pre>
    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.
 

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