1

这是查询:

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);

这两个表是profilesrelations。两者都有超过 1,000,000 行的 InnoDB 引擎。两者都有索引,在 ( , ) 上twitter_id有一个额外relations的索引。查询执行时间超过 6 秒,这真的让我很沮丧。我知道我可以以某种方式加入这个,但我的 MySQL 知识不是很酷,这就是我寻求你帮助的原因。twitter_idfollowed_by

提前谢谢大家=)

干杯,K~

更新

好的,我设法降低到 2.5 秒。我使用了 INNER JOIN 并添加了三个索引对。这是解释结果:

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, ''

希望这可以帮助。

另一个更新

以下是两个表的 SHOW CREATE TABLE 语句:

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

哇,真是一团糟=)对不起!

4

5 回答 5

3

连接看起来像这样:

SELECT COUNT(*) AS c,
MAX(p.`followers_count`) AS max_fc,
MIN(p.`followers_count`) AS min_fc,
MAX(p.`following_count`) AS max_fgc,
MIN(p.`following_count`) AS min_fgc,
SUM(p.`followers_count`) AS fc,
SUM(p.`following_count`) AS fgc,
MAX(p.`updates_count`) AS max_uc,
MIN(p.`updates_count`) AS min_uc,
SUM(p.`updates_count`) AS uc
FROM `profiles` AS p
INNER JOIN `relations` AS r ON p.`twitter_id` = r.`followed_by`
WHERE r.`twitter_id` = 123;

为了帮助优化它,您应该在两个查询上运行 EXPLAIN SELECT ...。

于 2009-07-09T14:33:55.747 回答
1
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`
JOIN `relations`
  ON (profiles.twitter_id = relations.followed_by)
WHERE relations.twitted_id = 123;

可能会快一点,但您需要测量并检查是否确实如此。

于 2009-07-09T14:35:33.780 回答
1

创建以下复合索引:

profiles (twitter_id, followers_count)
profiles (twitter_id, following_count)
profiles (twitter_id, updates_count)

并发布查询计划,看在上帝的份上。

顺便问一下,这会COUNT(*)返回多少行?

更新:

您的表格行很长。在您选择的所有字段上创建复合索引:

profiles (twitter_id, followers_count, following_count, updates_count)

这样JOIN查询就可以从该索引中检索它需要的所有值。

于 2009-07-09T14:35:57.053 回答
1

count(*)在 InnoDB 引擎下是一个非常昂贵的操作,你试过这个查询没有那块吗?如果它导致最多的处理时间,那么也许您可以保留一个运行值而不是每次都查询它。

于 2009-07-09T14:36:29.627 回答
1

我会从程序员的角度来解决这个问题;我将有一个单独的表(或某处的存储区域)存储与原始查询中的每个字段关联的最大值、最小值和总和值,并在每次更新和添加表记录时更新这些值。(尽管如果处理不当,删除可能会出现问题)。

在填充这些值的原始查询完成后(这与您发布的查询几乎相同),您实际上是将最终查询减少为从数据表中获取一行,而不是一次计算所有内容。

于 2009-07-09T15:02:02.150 回答