Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Explain: what's causing 'Using temporary; Using filesort'
    primarykey
    data
    text
    <p>I'm planning on creating a view using this SQL SELECT, but the explain for it shows it's using temporary and using filesort. I can't figure out what indices I need in order to fix this problem. Mostly, I'm wondering why it's using filesort intead of using an index to sort.</p> <p>Here are my tables:</p> <pre><code>CREATE TABLE `learning_signatures` ( `signature_id` int(11) NOT NULL AUTO_INCREMENT, `signature_file` varchar(100) NOT NULL, `signature_md5` varchar(32) NOT NULL, `image_file` varchar(100) NOT NULL, PRIMARY KEY (`signature_id`), UNIQUE KEY `unique_signature_md5` (`signature_md5`) ) ENGINE=InnoDB AUTO_INCREMENT=640 DEFAULT CHARSET=latin1 CREATE TABLE `learning_user_suggestions` ( `user_suggestion_id` int(11) NOT NULL AUTO_INCREMENT, `signature_id` int(11) NOT NULL, `ch` char(1) NOT NULL, `time_suggested` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `user_id` int(11) NOT NULL, PRIMARY KEY (`user_suggestion_id`), KEY `char_index` (`ch`), KEY `ls_sig_id_indx` (`signature_id`), KEY `user_id_indx` (`user_id`), KEY `sig_char_indx` (`signature_id`,`ch`) ) ENGINE=InnoDB AUTO_INCREMENT=1173 DEFAULT CHARSET=latin1 </code></pre> <p>And here is the problematic SQL statement I'm planning on using in my view:</p> <pre><code>select ls.signature_id, ls.signature_file, ls.signature_md5, ls.image_file, sug.ch , count(sug.ch) AS suggestion_count from (`learning_signatures` `ls` left join `learning_user_suggestions` `sug` on(ls.signature_id = sug.signature_id)) group by ls.signature_id, sug.ch; </code></pre> <p>Output from explain:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE ls ALL NULL NULL NULL NULL 514 "Using temporary; Using filesort" 1 SIMPLE sug ref ls_sig_id_indx,sig_char_indx ls_sig_id_indx 4 wwf.ls.signature_id 1 </code></pre> <p>Another example, this time using a where clause:</p> <pre><code>explain select ls.signature_id, ls.signature_file, ls.signature_md5, ls.image_file, sug.ch , count(sug.ch) AS suggestion_count from (`learning_signatures` `ls` left join `learning_user_suggestions` `sug` on(ls.signature_id = sug.signature_id)) WHERE signature_md5 = '75f8a5b1176ecc2487b90bacad9bc4c' group by ls.signature_id, sug.ch; </code></pre> <p>Explain output:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE ls const unique_signature_md5 unique_signature_md5 34 const 1 "Using temporary; Using filesort" 1 SIMPLE sug ref ls_sig_id_indx,sig_char_indx ls_sig_id_indx 4 const 1 </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