检测到我的问题后即使重新启动后,Mysql 进程也会消耗高 CPU 和 RAM ;我对以下信息使用了慢速查询,请帮我检测;执行简单查询超过 1s 似乎无法接受?
mysql Ver 15.1 Distrib 5.5.56-MariaDB, for Linux (x86_64) using readline 5.1
CREATE TABLE IF NOT EXISTS `jsdata` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`url_rewrite` varchar(100) CHARACTER SET utf8 NOT NULL DEFAULT '',
`title_en` varchar(100) CHARACTER SET utf8 NOT NULL DEFAULT '',
`title_fr` varchar(100) COLLATE utf8_bin NOT NULL,
`title_vi` varchar(100) CHARACTER SET utf8 NOT NULL,
`description_en` text CHARACTER SET utf8 NOT NULL,
`description_fr` text CHARACTER SET utf8 NOT NULL,
`description_vi` text CHARACTER SET utf8 NOT NULL,
`tags_en` text COLLATE utf8_bin NOT NULL,
`tags_fr` varchar(200) COLLATE utf8_bin NOT NULL,
`tags_vi` varchar(200) COLLATE utf8_bin NOT NULL,
`submitday` int(11) unsigned NOT NULL,
`author` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`location` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`cat_id` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`sub_id` mediumint(4) NOT NULL DEFAULT '0',
`filename` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`views` mediumint(8) unsigned DEFAULT '0',
`comments` mediumint(8) unsigned NOT NULL DEFAULT '0',
`stores` mediumint(8) unsigned NOT NULL DEFAULT '0',
`marks` mediumint(8) unsigned DEFAULT '0',
`votes` float unsigned NOT NULL DEFAULT '0',
`sents` mediumint(4) unsigned NOT NULL DEFAULT '0',
`image` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
`code_files` text CHARACTER SET utf8 NOT NULL,
`code_css` text CHARACTER SET utf8 NOT NULL,
`code_js` text CHARACTER SET utf8 NOT NULL,
`code_html` text CHARACTER SET utf8 NOT NULL,
`display` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `filename` (`filename`),
KEY `title_en` (`title_en`,`title_fr`,`title_vi`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=2843 ;
# Query_time: 1.029749 Lock_time: 0.000083 Rows_sent: 10 Rows_examined: 4883
SELECT JS.id, JS.url_rewrite, JS.title_en AS title, JS.description_en AS description,
JS.submitday, JS.image, JS.comments, JS.stores, JS.cat_id, JS.sub_id
FROM jsdata JS
WHERE JS.display=1 AND 1=1
ORDER BY JS.submitday DESC LIMIT 2220,10;
--
# Query_time: 1.070827 Lock_time: 0.000065 Rows_sent: 1 Rows_examined: 2653
SELECT COUNT(id) AS sum FROM jsdata WHERE display=1;
尝试添加复合索引来支持查询的 WHERE 和 ORDER BY 子句。 我认为这个可以解决问题。
ALTER TABLE jsdata ADD INDEX ds(display, submitday);
为什么这会有帮助(如果有的话)?该索引包含 WHERE 子句中提到的字段,以便 DBMS 可以找到第一个符合条件的行。然后,它包含按正确顺序预排序的行,以便 DBMS 可以跳过其中的前 2210 个行,然后获取接下来的 10 个行,而不进行任何实际排序。
请注意
SELECT lots, of, columns
...
ORDER BY something
LIMIT bignumber, smallnumber
是一种臭名昭著的没有索引支持的查询性能反模式。 DBMS 必须检索大量数据,对其进行排序,跳过大量数据,然后仅返回几行。
评论中的人们注意到你们的 DBMS 软件已经过时多年了。我赞同这一点。升级。