Psycho Posted November 22, 2006 Share Posted November 22, 2006 I have a query that is taking several seconds to process and was wondering if anyone had any ideas on how to get the same data with a more efficient query.I have two tables that are involved. There are additional fields, but I am only showing the ones relevant to the query I need.[movies_crew] - movie_id - crew_id (unique to a record in the crew table) - role_id[roles] - role_id - roleWhat I am trying to obtain is the number of unique crewmembers for each role. For example "Tom Hanks" might be an actor in several movies, but I only want him counted once when determining the number of actors. However, "Tom Hanks" could also be a Producer and would need to be counted in that role calculation.The result set I am looking for is like this:Actor, 3,567Director, 356Producer, 512Writer, 435I am already getting those results with the query below, but it takes several seconds to run.[code]SELECT r.role as role, COUNT(DISTINCT mc.crew_id) as count FROM movies_crew mcLEFT JOIN roles r ON mc.role_id = r.role_id GROUP BY r.role_id ORDER BY role ASC[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/ Share on other sites More sharing options...
designationlocutus Posted November 22, 2006 Share Posted November 22, 2006 Hmm I've often done counters. To save time on query execution, I created an additional table that stores the count totals. On insert or update of your Actor, Director, Producer, Writer tables, an additional query is executed that counts these and stores the totals.Very useful when you have thousands upon thousands of records that are always being counted on. Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/#findComment-128844 Share on other sites More sharing options...
printf Posted November 23, 2006 Share Posted November 23, 2006 Could you please show your real table scheme(s) and tell me what version (#.#) of database are you using.printf Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/#findComment-128925 Share on other sites More sharing options...
Psycho Posted November 24, 2006 Author Share Posted November 24, 2006 MySQL version is 5.0.24aHere are the tables involved (I have left off some fields that are extraneous):[movies]- movie_id (KEY)[crew]- crew_id (KEY)[roles]- role_id (Key)[movies_crew]- index (KEY)- movie_id (F Key)- crew_id (F Key)- role_id (F Key)As you can see I have three tables that are linked with a fourth. I am trying to find out how many unique crew_id's are associated with each role_id.I have been able to get the results much faster by using a sub query, but I would appreciate if anyone has any comments on what I am doing and if this is a good approach. The new query I am using is as follows:[code]SELECT subq.role as role, COUNT(subq.crew_id) as countFROM (SELECT DISTINCT r.role as role, mc.crew_id as crew_id FROM roles r INNER JOIN movies_crews mc ON r.role_id = mc.role_id) as subqGROUP BY subq.roleORDER BY subq.role[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/#findComment-129794 Share on other sites More sharing options...
fenway Posted November 25, 2006 Share Posted November 25, 2006 I'd need to see the explain output from both queries. Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/#findComment-129891 Share on other sites More sharing options...
Psycho Posted November 25, 2006 Author Share Posted November 25, 2006 Thank you for your time.[b]Query #1[/b]This was my original query which could take a few seconds to run[code]SELECT r.role as role, COUNT(DISTINCT mc.crew_id) as count FROM roles r INNER JOIN movies_crews mc ON r.role_id = mc.role_id GROUP BY r.role_id ORDER BY role ASC[/code]This is the EXPLAIN output:[code]id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 1 | SIMPLE | r | ALL | | | | | 24 | Using temporary; Using filesort 1 | SIMPLE | mc | ALL | | | | | 8101 | Using where[/code][b]Query #2[/b]This is my new query which is running much faster. I'm just not sure this is the most efficient.[code]SELECT subq.role as role, COUNT(subq.crew_id) as countFROM (SELECT DISTINCT r.role as role, mc.crew_id as crew_id FROM roles r INNER JOIN movies_crews mc ON r.role_id = mc.role_id ) as subqGROUP BY subq.roleORDER BY subq.role[/code]Here is the EXPLAIN output[code]id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 1 | PRIMARY | | ALL | | | | | 5214 | Using temporary; Using filesort 2 | DERIVED | r | ALL | | | | | 24 | Using temporary 2 | DERIVED | mc | ALL | | | | | 8101 | Using where [/code] Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/#findComment-130129 Share on other sites More sharing options...
fenway Posted November 26, 2006 Share Posted November 26, 2006 Why am I seeing nothing from keys... even if you have none, it should be saying "null".... Quote Link to comment https://forums.phpfreaks.com/topic/28167-optimize-query/#findComment-130320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.