mysql distinct 慢_使用DISTINCT和GROUP BY后,mySQL查询速度很慢?

我有以下结构的表格:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值