Mysql 执行查询需要更多时间 执行查询大约需要5分钟到20分钟 由于这一点,我们得到负载峰值 请帮我重写查询 还可以帮助我提高查询的性能
查询:Mysql 执行查询需要更多时间 执行查询大约需要5分钟到20分钟 由于这一点,我们得到负载峰值 请帮我重写查询 还可以帮助我提高查询的性能,mysql,sql,performance,Mysql,Sql,Performance,查询: SELECT DATE(create_time) as createDate, count(url_id) FROM t_notification WHERE domain_id = 185 AND type = 12 AND create_time >= '2012-12-15' GROUP BY createDate 说明 explain select DATE(create_time) as createDate, count(url_id) f
SELECT DATE(create_time) as createDate, count(url_id)
FROM t_notification
WHERE domain_id = 185
AND type = 12
AND create_time >= '2012-12-15'
GROUP BY createDate
说明
explain select DATE(create_time) as createDate, count(url_id) from t_notification where domain_id = 185 and type = 12 and create_time >= '2012-12-15' group by createDate;
+----+-------------+----------------+------+---------------------------------+----------+---------+-------+---------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------------------------+----------+---------+-------+---------+----------------------------------------------+
| 1 | SIMPLE | t_notification | ref | FK_notification_domain,idx_test | idx_test | 5 | const | 9189516 | Using where; Using temporary; Using filesort |
+----+-------------+----------------+------+---------------------------------+----------+---------+-------+---------+----------------------------------------------+
1 row in set (0.29 sec)
mysql> show create table t_notification\G
*************************** 1. row ***************************
Table: t_notification
Create Table: CREATE TABLE `t_notification` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(11) DEFAULT NULL,
`content` varchar(512) DEFAULT NULL,
`create_time` date DEFAULT NULL,
`domain_id` int(11) DEFAULT NULL,
`url_id` int(11) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`targetrul_partnerurl_id` int(11) DEFAULT NULL,
`week_entrances` int(11) DEFAULT NULL COMMENT 'for keyword and target_url',
PRIMARY KEY (`id`),
KEY `url_id` (`url_id`),
KEY `targetrul_partnerurl_id` (`targetrul_partnerurl_id`),
KEY `FK_notification_domain` (`domain_id`,`id`),
KEY `idx_test` (`domain_id`,`status`,`type`,`create_time`)
) ENGINE=InnoDB AUTO_INCREMENT=50747991 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
考虑在域id和type列上创建复合索引,因为它们直接用于where子句。它肯定会提高查询的性能。来自 假设您发出以下SELECT语句:mysql>SELECT *来自tbl_名称,其中col1=val1和col2=val2 如果col1和col2上存在多列索引,则相应的 可以直接获取行。如果存在单独的单列索引 在col1和col2上,优化器将尝试使用索引合并 优化(见第8.3.1.4节“索引合并优化”),或 尝试通过确定哪个索引来查找限制性最强的索引 查找较少的行并使用该索引获取行 如果表具有多列索引,则 优化器可以使用索引查找行。例如,如果你 如果在(col1、col2、col3)上有一个三列索引,则您已经建立了索引 (col1)、(col1,col2)和(col1,col2,col3)上的搜索功能 在类型或创建时间上没有可用的索引。从键idx_test中删除状态,或在(type,create_time)上或在type和create_time上分别创建新索引