【问题标题】:mySQL query is very slow after using DISTINCT and GROUP BY?使用 DISTINCT 和 GROUP BY 后 mySQL 查询很慢?
【发布时间】:2012-08-21 17:42:16
【问题描述】:

我有以下结构的表:

-- Table structure for table `temp_app`
--

CREATE TABLE IF NOT EXISTS `temp_app` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vid` int(5) NOT NULL,
  `num` varchar(64) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `vid` (`vid`),
  KEY `num` (`num`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=69509;

-- Table structure for table `inv_flags`
--

CREATE TABLE IF NOT EXISTS `inv_flags` (
  `num` varchar(64) NOT NULL,
  `vid` int(11) NOT NULL,
  `f_special` tinyint(1) NOT NULL, /*0 or 1*/
  `f_inserted` tinyint(1) NOT NULL, /*0 or 1*/
  `f_notinserted` tinyint(1) NOT NULL, /*0 or 1*/
  `userID` int(11) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY `num` (`num`),
  KEY `userID` (`userID`),
  KEY `vid` (`vid`),
  KEY `timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

以下查询的执行时间为 9 秒,显示 30 条记录。怎么了?

SELECT date_format(ifs.`timestamp`,'%y/%m/%d') as `date`
                ,count(DISTINCT ta.num) as inserted /*Unique nums*/
                ,SUM(ifs.f_notinserted) as not_inserted
                ,SUM(ifs.f_special)  as special
                ,count(ta.num) as links /*All nums*/
                from inventory_flags ifs
                LEFT JOIN temp_app ta ON ta.num = ifs.num AND ta.vid = ifs.vid
                WHERE ifs.userID = 3
                GROUP BY date(ifs.`timestamp`) DESC LIMIT 30

解释结果

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  ifs ref userID  userID  4   const   12153   Using where
1   SIMPLE  ta  ref vid,num num 194 ifs.num 1   

【问题讨论】:

    标签: mysql performance group-by distinct


    【解决方案1】:

    COUNT DISTINCT 有时会导致 MySql 性能下降。试试这个:

    select count(*) from (select distinct...
    

    因为它有时会阻止 MySql 将整个中间结果写入磁盘。

    这里是 MySql 错误信息:

    http://bugs.mysql.com/bug.php?id=21849

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 2021-12-26
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 2014-06-21
      相关资源
      最近更新 更多