Here is your original query
SELECT
`school`.`ARCO_name`,
`student-`.`ClassSize_7`,
`student-`.`ClassSize_8`,
`degree_o`.`degree_code`,
`accredit`.`full_faculty_3`,
`accredit`.`total_faculty_3`,
`accredit`.`pc_terminal`,
`accredit`.`stud_fac_ratio`
FROM school
INNER JOIN `student-` ON `school`.`scid` = `student-`.`scid`
INNER JOIN `degree_o` ON `student-`.`scid` = `degree_o`.`scid`
INNER JOIN `accredit` ON `degree_o`.`scid` = `accredit`.`scid`
ORDER BY `school`.`ARCO_name` ASC LIMIT 0, 25
;
Two Suggestions
- Perform
LIMIT 0, 25
on school table before the INNER JOINs
- INNER JOIN
school.scid
to the other three(3) tables
Here is my propsed query
SELECT
`school`.`ARCO_name`,
`student-`.`ClassSize_7`,
`student-`.`ClassSize_8`,
`degree_o`.`degree_code`,
`accredit`.`full_faculty_3`,
`accredit`.`total_faculty_3`,
`accredit`.`pc_terminal`,
`accredit`.`stud_fac_ratio`
FROM
(SELECT scid,ARCO_name FROM school ORDER BY ARCO_name LIMIT 0,25) school
INNER JOIN `student-` ON `school`.`scid` = `student-`.`scid`
INNER JOIN `degree_o` ON `school`.`scid` = `degree_o`.`scid`
INNER JOIN `accredit` ON `school`.`scid` = `accredit`.`scid`
;
Give it a Try !!!
UPDATE 2012-06-25 12:13 EDT
To speed up the subquery, make sure you have this index:
ALTER TABLE school ADD INDEX ARCO_name_scid_ndx (ARCO_name,scid);
UPDATE 2012-06-25 12:18 EDT
AS pointed out by @yercube in his comments to my answer, you may want to attach the student-id from to degree_o and degree_id over to accedit. Perhaps something like this:
SELECT
`school`.`ARCO_name`,
`student-`.`ClassSize_7`,
`student-`.`ClassSize_8`,
`degree_o`.`degree_code`,
`accredit`.`full_faculty_3`,
`accredit`.`total_faculty_3`,
`accredit`.`pc_terminal`,
`accredit`.`stud_fac_ratio`
FROM
(SELECT scid,ARCO_name FROM school ORDER BY ARCO_name LIMIT 0,25) school
INNER JOIN `student-` ON `school`.`scid` = `student-`.`scid`
INNER JOIN `degree_o` ON `student-`.`studend_id` = `degree_o`.`studend_id`
INNER JOIN `accredit` ON `degree_o`.`degree_id` = `accredit`.`degree_id`
;
In your query, the join has no conditions. Shouldnt be something like SELECT xxx FROM table1 JOIN table2 ON table1.column = table2.column and xxx.date between xxx to xxx .
Add:
Here is the explain for the test table
explain select find from table1 t1 join table2 t2 on t1.find between t2.from1 and t2.to1;
+----+-------------+-------+-------+-------------------+---------+---------+------+------+------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+-------------------+---------+---------+------+------+------------------------------------------------+
| 1 | SIMPLE | t2 | index | from1,to1,from_to | from_to | 12 | NULL | 2 | Using index |
| 1 | SIMPLE | t1 | ALL | idx | NULL | NULL | NULL | 5 | Range checked for each record (index map: 0x1) |
Best Answer
From my (rather limited) knowledge of php, I assume you want to combine the two queries into one:
As for efficiency, it's better to tune the first query separately and then this combined one.
An index on
plays (sid, time)
should help for the first query. For the second, if there is an index onmusic (sid)
, it should be quite efficient as it's a join to only 50 rows ofmusic
.