小编ima*_*ive的帖子

优化慢查询以更好地使用索引

我有一个如下所示的查询:

SELECT time_stop, some_count
        FROM foo
        WHERE user_id = 1
        AND time_start >= '2016-07-27 00:00:00'
        AND time_stop <= '2016-07-27 23:59:59' 
        AND some_count = ( SELECT MAX(some_count) 
          FROM foo
          WHERE user_id = 1
          AND tm_start >= '2016-07-27 00:00:00'
          AND tm_stop <= '2016-07-27 23:59:59'
       );
Run Code Online (Sandbox Code Playgroud)

表架构如下所示:

Create Table: CREATE TABLE `foo` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `time_start` datetime(6) DEFAULT NULL,
  `time_stop` datetime(6) DEFAULT NULL,
  `some_count` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),,
  KEY `user_id` (`user_id`),
  KEY …
Run Code Online (Sandbox Code Playgroud)

mysql performance query-performance

2
推荐指数
1
解决办法
57
查看次数

标签 统计

mysql ×1

performance ×1

query-performance ×1