Note that there are some explanatory texts on larger screens.

plurals
  1. POAnyone care to help optimize a MySQL query?
    primarykey
    data
    text
    <p>Here's the query:</p> <pre><code>SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`) AS max_fgc, MIN(`following_count`) AS min_fgc, SUM(`followers_count`) AS fc, SUM(`following_count`) AS fgc, MAX(`updates_count`) AS max_uc, MIN(`updates_count`) AS min_uc, SUM(`updates_count`) AS uc FROM `profiles` WHERE `twitter_id` IN (SELECT `followed_by` FROM `relations` WHERE `twitter_id` = 123); </code></pre> <p>The two tables are <code>profiles</code> and <code>relations</code>. Both have over 1,000,000 rows, InnoDB engine. Both have indexes on <code>twitter_id</code>, <code>relations</code> has an extra index on (<code>twitter_id</code>, <code>followed_by</code>). The query is taking over 6 seconds to execute, this really frustrates me. I know that I can JOIN this somehow, but my MySQL knowledge is not so cool, that's why I'm asking for your help.</p> <p>Thanks in advance everyone =)</p> <p>Cheers, K ~</p> <p><strong>Updated</strong></p> <p>Okay I managed to get down to 2,5 seconds. I used INNER JOIN and added the three index pairs. Here's the EXPLAIN results:</p> <pre><code>id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra 1, 'SIMPLE', 'r', 'ref', 'relation', 'relation', '4', 'const', 252310, 'Using index' 1, 'SIMPLE', 'p', 'ref', 'PRIMARY,twiter_id,id_fc,id_fgc,id_uc', 'id_uc', '4', 'follerme.r.followed_by', 1, '' </code></pre> <p>Hope this helps.</p> <p><strong>Another update</strong></p> <p>Here are the SHOW CREATE TABLE statements for both tables:</p> <pre><code>CREATE TABLE `profiles` ( `twitter_id` int(10) unsigned NOT NULL, `screen_name` varchar(45) NOT NULL default '', `followers_count` int(10) unsigned default NULL, `following_count` int(10) unsigned default NULL, `updates_count` int(10) unsigned default NULL, `location` varchar(45) default NULL, `bio` varchar(160) default NULL, `url` varchar(255) default NULL, `image` varchar(255) default NULL, `registered` int(10) unsigned default NULL, `timestamp` int(10) unsigned default NULL, `relations_timestamp` int(10) unsigned default NULL, PRIMARY KEY USING BTREE (`twitter_id`,`screen_name`), KEY `twiter_id` (`twitter_id`), KEY `screen_name` USING BTREE (`screen_name`,`twitter_id`), KEY `id_fc` (`twitter_id`,`followers_count`), KEY `id_fgc` (`twitter_id`,`following_count`), KEY `id_uc` (`twitter_id`,`updates_count`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 CREATE TABLE `relations` ( `id` int(10) unsigned NOT NULL auto_increment, `twitter_id` int(10) unsigned NOT NULL default '0', `followed_by` int(10) unsigned default NULL, `timestamp` int(10) unsigned default NULL, PRIMARY KEY USING BTREE (`id`,`twitter_id`), UNIQUE KEY `relation` (`twitter_id`,`followed_by`) ) ENGINE=InnoDB AUTO_INCREMENT=1209557 DEFAULT CHARSET=utf8 </code></pre> <p>Wow, what a mess =) Sorry!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload