查询
SELECT *
FROM user_ip_tmp
WHERE too = 'http://example.com/'
AND contry != 'CN'
AND contry != 'TW'
ORDER BY id DESC
LIMIT 50
MySQL返回:
Showing rows 0 - 29 ( 50 total, Query took 11.9276 sec) [id: 3452538 - 3448824]
如果我删除 ORDER BY ID DESC
Showing rows 0 - 29 ( 50 total, Query took 0.0033 sec)
解释计划:
计数
SELECT count( * )
FROM user_ip_tmp
使用的数据库示例
CREATE TABLE IF NOT EXISTS `user_ip_tmp` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`ip` varchar(20) NOT NULL,
`dataip` bigint(20) NOT NULL,
`ref` text NOT NULL,
`click` int(20) NOT NULL,
`code` varchar(17) NOT NULL,
`too` text NOT NULL,
`checkopen` varchar(17) NOT NULL,
`contry` text NOT NULL,
`vOperation` text NOT NULL,
`vBrowser` text NOT NULL,
`iconOperation` text NOT NULL,
`iconBrowser` text NOT NULL,
PRIMARY KEY (`id`),
KEY `ip` (`dataip`),
KEY `ip` (`checkopen`),
KEY `ip` (`code`),
KEY `ip` (`too`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5426268 ;
我想要正确的方法来执行查询并优化数据库 ORDER BY ID DESC
答案 0 :(得分:2)
了解有关数据分布的信息会很有趣。您可以将以下查询的结果添加到您的帖子中吗? (不需要图片,纯文本会做)。
SELECT count(*) FROM user_ip_tmp WHERE too = 'http://example.com/' AND contry != 'CN' AND contry != 'TW';
SELECT count(*) FROM user_ip_tmp WHERE too = 'http://example.com/';
另外,你可以测试这种替代性能吗? 编辑:子查询的别名
SELECT sub.* FROM
(SELECT *
FROM user_ip_tmp
WHERE too = 'http://example.com/'
AND contry != 'CN'
AND contry != 'TW'
) sub
ORDER BY sub.id DESC
LIMIT 50
修改如果它是添加和试验索引的选项,那么您可以尝试其中一种(或两者都看看哪种更好)
CREATE INDEX index_name ON `user_ip_tmp` (`too`, `id`);
CREATE INDEX index_name ON `user_ip_tmp` (`too`, `contry`, `id`);
答案 1 :(得分:0)
您可以使用以下内容创建索引:
id, too and contry