title: Avoiding copying to temporary tables
body: <p>I normally do most of my work in java, I cannot really find what I'm doing here that is causing it to need to copy to a table and die.</p>
  
  <pre><code>SELECT company.tblusers.userid, 
  db.operations.id AS operation_id,
  SUM(TIME_TO_SEC(db.batch_log.time_elapsed)) AS time_elapsed, 
  SUM(db.tasks.estimated_nonrecurring + db.tasks.estimated_recurring) AS total_elapsed,
  COUNT(db2.ncr_ncr.id) AS number_of_ncrs
  FROM company.tblusers 
  INNER JOIN db.operations 
  INNER JOIN db.batch_log ON company.tblusers.userid = batch_log.userid 
  INNER JOIN db.batches ON batch_log.batch_id = batches.id 
  INNER JOIN db.tasks ON db.batches.id = db.tasks.batch_id
  INNER JOIN db2.ncr_ncr ON company.tblusers.sugar_name = db2.ncr_ncr.employee
  WHERE company.tblusers.departmentid = 8 
  AND db.operations.id = db.batches.operation_id 
  AND db.batches.id = db.tasks.batch_id 
  AND db.batch_log.userid = company.tblusers.userid
  AND db2.ncr_ncr.employee = company.tblusers.sugar_name 
  GROUP by company.tblusers.userid, db.batches.operation_id
  </code></pre>
  
  <p>EDIT: Explain output</p>
  
  <pre><code>"id";"select_type";"table";"type";"possible_keys";"key";"key_len";"ref";"rows";"Extra"
  "1";"SIMPLE";"ncr_ncr";"ALL";NULL;NULL;NULL;NULL;"2700";"Using temporary; Using filesort"
  "1";"SIMPLE";"batch_log";"ALL";NULL;NULL;NULL;NULL;"78026";""
  "1";"SIMPLE";"tblusers";"eq_ref";"PRIMARY";"PRIMARY";"52";"ramses.batch_log.userid";"1";"Using where"
  "1";"SIMPLE";"tasks";"ref";"Index 2";"Index 2";"38";"ramses.batch_log.batch_id";"2";""
  "1";"SIMPLE";"batches";"eq_ref";"PRIMARY";"PRIMARY";"38";"ramses.tasks.batch_id";"1";"Using where"
  "1";"SIMPLE";"operations";"eq_ref";"PRIMARY";"PRIMARY";"4";"ramses.batches.operation_id";"1";"Using where; Using index"
  </code></pre>