Skip to content

Instantly share code, notes, and snippets.

@taf2
Created November 4, 2009 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taf2/226405 to your computer and use it in GitHub Desktop.
Save taf2/226405 to your computer and use it in GitHub Desktop.
mysql> SELECT count(DISTINCT `companies`.id) AS count_all FROM `companies` LEFT OUTER JOIN `contacts` ON `contacts`.contactable_id = `companies`.id AND `contacts`.contactable_type = 'Company' LEFT OUTER JOIN `locations` ON `locations`.locatable_id = `companies`.id AND `locations`.locatable_type = 'Company' LEFT OUTER JOIN `events` ON `events`.eventable_id = `companies`.id AND `events`.eventable_type = 'Company'WHERE ( (`companies`.`type` = 'Prospect' ) );
+-----------+
| count_all |
+-----------+
| 882 |
+-----------+
1 row in set (1.60 sec)
mysql> explain SELECT count(DISTINCT `companies`.id) AS count_all FROM `companies` LEFT OUTER JOIN `contacts` ON `contacts`.contactable_id = `companies`.id AND `contacts`.contactable_type = 'Company' LEFT OUTER JOIN `locations` ON `locations`.locatable_id = `companies`.id AND `locations`.locatable_type = 'Company' LEFT OUTER JOIN `events` ON `events`.eventable_id = `companies`.id AND `events`.eventable_type = 'Company'WHERE ( (`companies`.`type` = 'Prospect' ) );
+----+-------------+-----------+------+------------------------------+------------------------------+---------+--------------------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+------------------------------+------------------------------+---------+--------------------------------+------+--------------------------+
| 1 | SIMPLE | companies | ref | index_companies_on_type | index_companies_on_type | 258 | const | 474 | Using where; Using index |
| 1 | SIMPLE | contacts | ALL | NULL | NULL | NULL | NULL | 843 | |
| 1 | SIMPLE | locations | ALL | NULL | NULL | NULL | NULL | 505 | |
| 1 | SIMPLE | events | ref | index_events_on_eventable_id | index_events_on_eventable_id | 5 | dev_captico_leads.companies.id | 1 | |
+----+-------------+-----------+------+------------------------------+------------------------------+---------+--------------------------------+------+--------------------------+
4 rows in set (0.01 sec)
mysql> explain SELECT count(DISTINCT `companies`.id) AS count_all FROM `companies` LEFT OUTER JOIN `contacts` ON `contacts`.contactable_id = `companies`.id AND `contacts`.contactable_type = 'Company' LEFT OUTER JOIN `locations` ON `locations`.locatable_id = `companies`.id AND `locations`.locatable_type = 'Company' LEFT OUTER JOIN `events` ON `events`.eventable_id = `companies`.id AND `events`.eventable_type = 'Company'WHERE ( (`companies`.`type` = 'Prospect' ) );
+----+-------------+-----------+------+------------------------------------------------------------------------------+-------------------------------------------------------+---------+--------------------------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+------------------------------------------------------------------------------+-------------------------------------------------------+---------+--------------------------------------+------+--------------------------+
| 1 | SIMPLE | companies | ref | index_companies_on_type | index_companies_on_type | 258 | const | 335 | Using where; Using index |
| 1 | SIMPLE | contacts | ref | index_contacts_on_contactable_id_and_contactable_type | index_contacts_on_contactable_id_and_contactable_type | 263 | dev_captico_leads.companies.id,const | 1 | Using index |
| 1 | SIMPLE | locations | ref | index_locations_on_locatable_id_and_locatable_type | index_locations_on_locatable_id_and_locatable_type | 263 | dev_captico_leads.companies.id,const | 1 | Using index |
| 1 | SIMPLE | events | ref | index_events_on_eventable_id,index_events_on_eventable_id_and_eventable_type | index_events_on_eventable_id_and_eventable_type | 263 | dev_captico_leads.companies.id,const | 5 | Using index |
+----+-------------+-----------+------+------------------------------------------------------------------------------+-------------------------------------------------------+---------+--------------------------------------+------+--------------------------+
4 rows in set (0.01 sec)
mysql>
mysql> SELECT count(DISTINCT `companies`.id) AS count_all FROM `companies` LEFT OUTER JOIN `contacts` ON `contacts`.contactable_id = `companies`.id AND `contacts`.contactable_type = 'Company' LEFT OUTER JOIN `locations` ON `locations`.locatable_id = `companies`.id AND `locations`.locatable_type = 'Company' LEFT OUTER JOIN `events` ON `events`.eventable_id = `companies`.id AND `events`.eventable_type = 'Company'WHERE ( (`companies`.`type` = 'Prospect' ) );
+-----------+
| count_all |
+-----------+
| 882 |
+-----------+
1 row in set (0.02 sec)
mysql>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment