Thesql optimize group by having count

countgroup byMySQLoptimization

I have 2 mysql table:

table t1:

CREATE TABLE IF NOT EXISTS `t1` (
  `id` bigint(20) NOT NULL,
  `url` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `type` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

table t2

CREATE TABLE IF NOT EXISTS `owa_request` (
  `id` bigint(20) NOT NULL,
  `date` int(11) DEFAULT NULL,
  `ip` varchar(15) DEFAULT NULL,
  `d_id` bigint(20) DEFAULT NULL,
  `u_id` bigint(20) DEFAULT NULL,
  `browser` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `d_id` (`d_id`),
  KEY `u_id` (`u_id`),
  KEY `date` (`date`),
  KEY `ip` (`ip_address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Now I want to make such a query:

select 
t1.id,t1.url,t1.type,
t2.d_id,t2.date,t2.ip 
from t1,t2 
where t1.id=t2.d_id and t1.type='article' 
group by t2.ip 
HAVING COUNT(t2.ip)>10

get all the data group by t2.ip and each t2.ip should have at least 10 results.

But the query is very slow, 300k rows data (70MB), cost 12.5290 sec, have I make bad indexes? how to optimize let query under half second? Thanks.

Best Answer

I think that your type key is the main problem for you ! as you have it set to the max index size in mysql !
Try to see the real need of those 255(767 byte index) that is big for a table with that many rows. And check the distinct values in this column 'type' maybe a partition can be used in order to get rid of unnecessary reads.