bowenbowen Posted July 13, 2006 Share Posted July 13, 2006 Has anyone got any tips on how I could optimize this query?[code]SELECTs.first_name AS 'First Name',s.last_name AS 'Last Name',s.year AS 'Year',s.form_group AS 'Form Group',SUM(b.re = '1') AS RE,SUM(b.rw = '1') AS RW,SUM(b.rc = '1') AS RC,SUM(b.rb = '1') AS RB,SUM(b.hw = '1') AS Homework,SUM(b.late = '1') AS late,SUM(b.d1 != '') AS D1,SUM(b.d2 != '') AS D2,SUM(b.d3 != '') AS D3,SUM(b.d4 != '') AS D4FROM students s INNER JOIN behaviour_record_sheets_archive b ON s.student_id = b.student_idWHEREs.year = 8GROUP BYs.student_idHAVINGSUM(b.d1 != '') = 0 ANDSUM(b.d2 != '') = 0 ANDSUM(b.d3 != '') = 0 ANDSUM(b.d4 != '') = 0ORDER BY s.last_name ASC;[/code]Here is the result of my EXPLAIN statement[table][tr][td]ID[/td][td]Select Type[/td][td]Table[/td][td]Type[/td][td]Possible Keys[/td][td]Key[/td][td]Key Len[/td][td]Ref[/td][td]Rows[/td][td]Extra[/td][/tr][tr][td]1[/td][td]Simple[/td][td]s[/td][td]index[/td][td]PRIMARY[/td][td]PRIMARY[/td][td]30[/td][td][/td][td]1596[/td][td]Using where; Using temporary; Using filesort[/td][/tr][tr][td]1[/td][td]Simple[/td][td]b[/td][td]ref[/td][td]student_id[/td][td]student_id[/td][td]30[/td][td]behaviour.s.student_id[/td][td]162[/td][td][/td][/tr][/table]As you may guess, I have an index setup in my behaviour_record_sheets_archive (excuse the name) on the column "student_id".I would just be interested to see if this query could be sped up at all, it is currently taking around 12 seconds to run. Link to comment https://forums.phpfreaks.com/topic/14466-optimize-query/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.