1) First query used ... which took around 23 secs
select a.id from mza_movie_upload a,mza_movie_statics b
where a.status=1 and b.download=1 and a.id=b.rid
group by b.rid order by sum(b.download) desc
Currently i modified the query ..which takes around 9 secs
select a.id from mza_movie_upload a
INNER JOIN mza_movie_statics b
ON a.id=b.rid WHERE a.status=1 and b.download=1
group by b.rid order by sum(b.download) desc
explain select a.id from mza_movie_upload a INNER JOIN mza_movie_statics b ON a.id=b.rid WHERE a.status=1 and b.download=1 group by b.rid order by sum(b.download) desc;
+----+-------------+-------+--------+---------------+---------+---------+----------------------+---------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+---------+---------+----------------------+---------+----------------------------------------------+ | 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 1603089 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | mmdfurni_dev11.b.rid | 1 | Using where | +----+-------------+-------+--------+---------------+---------+---------+----------------------+---------+----------------------------------------------+ 2 rows in set (0.03 sec)
I am not sure what is the performance to be done ? I want this query to be fast .. I tried to index rid and id which still made the query worse.
Here is the table details
mza_movie_upload
+---------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | userid | varchar(200) | NO | | NULL | | | email | varchar(200) | NO | | NULL | | | up_date | datetime | NO | | NULL | | | file_size | varchar(200) | NO | | NULL | | | temp_filename | varchar(200) | NO | | NULL | | | fileneame | varchar(200) | NO | MUL | NULL | | | filepath | varchar(255) | NO | | NULL | | | status | varchar(20) | NO | | NULL | | | ip | varchar(200) | NO | | NULL | | | category | varchar(200) | NO | | NULL | | | mcode | bigint(20) | NO | | NULL | | | movie_name | varchar(200) | NO | | NULL | | +---------------+--------------+------+-----+---------+----------------+ 13 rows in set (0.00 sec)
mza_movie_statics
+-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | rid | int(11) | NO | | NULL | | | uid | int(11) | NO | | NULL | | | save | int(11) | NO | | NULL | | | download | int(11) | NO | | NULL | | | enterdate | date | NO | | NULL | | +-----------+---------+------+-----+---------+----------------+ 6 rows in set (0.00 sec)