我有一个运行非常缓慢的查询。
运行大约需要 7 秒。
但是,如果我从查询中删除“和时间戳 > 20130201”,则运行时间只需不到 400 毫秒。
我想也许时间戳字段没有被索引,但它是,所以我想知道为什么将这样的日期添加到查询会导致它变慢?
如果可以的话,我宁愿不删除查询的那部分,但我不明白为什么它应该在那里慢得多。
询问:
select p1_score,p2_score,p3_score,p4_score from game where (p1_user_id='$uid' or p2_user_id='$uid' or p3_user_id='$uid' or p4_user_id='$uid') and turn=0 and timestamp > 20130201
解释:
1 SIMPLE game range p1_user_id,p2_user_id,p3_user_id,p4_user_id,turn,timestamp timestamp 4 NULL 51486 Using where
创建 SQL:
CREATE TABLE `game` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`p1_user_id` varchar(100) NOT NULL DEFAULT '',
`p2_user_id` varchar(100) NOT NULL DEFAULT '',
`p3_user_id` varchar(100) NOT NULL DEFAULT '',
`p4_user_id` varchar(100) NOT NULL DEFAULT '',
`player_one_name` varchar(100) DEFAULT NULL,
`player_two_name` varchar(100) DEFAULT NULL,
`player_three_name` varchar(100) DEFAULT NULL,
`player_four_name` varchar(100) DEFAULT NULL,
`player_one_email` varchar(100) DEFAULT NULL,
`player_two_email` varchar(100) DEFAULT NULL,
`player_three_email` varchar(100) DEFAULT NULL,
`player_four_email` varchar(100) DEFAULT NULL,
`p1_score` bigint(10) DEFAULT '0',
`p2_score` bigint(10) DEFAULT '0',
`p3_score` bigint(10) DEFAULT '0',
`p4_score` bigint(10) DEFAULT '0',
`game_name` varchar(255) DEFAULT NULL,
`players` smallint(1) DEFAULT NULL,
`turn` varchar(10) DEFAULT NULL,
`turn_num` smallint(10) DEFAULT '0',
`layout` longtext,
`verify` varchar(40) DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`message` longtext,
`tourn` smallint(5) NOT NULL DEFAULT '0',
`round` smallint(4) NOT NULL DEFAULT '0',
`tourn_id` varchar(20) NOT NULL DEFAULT '',
`move_history` longtext NOT NULL,
`next_turn` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `tourn` (`tourn`),
KEY `by_player_one_email` (`player_one_email`(10)),
KEY `by_player_two_email` (`player_two_email`(10)),
KEY `by_player_three_email` (`player_three_email`(10)),
KEY `by_player_four_email` (`player_four_email`(10)),
KEY `p1_user_id` (`p1_user_id`),
KEY `p2_user_id` (`p2_user_id`),
KEY `p3_user_id` (`p3_user_id`),
KEY `p4_user_id` (`p4_user_id`),
KEY `turn` (`turn`),
KEY `verify` (`verify`),
KEY `next_turn` (`next_turn`),
KEY `timestamp` (`timestamp`),
KEY `round` (`round`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1;