如今你经常阅读,MariaDB是MySQL'的替代品。但实际上有时存在巨大差异。例如。这一个:
有一个使用MariaDB查询的应用程序表现良好。现在必须使用MySQL,并且令人惊讶的是性能不足。
如何在MySQL上实现相同的性能?
查询确定一年前的当前值和值,但每天都没有值,因此会搜索与一年前最接近的日期。
以下是查询。创建一些测试数据的代码如下。
SELECT c.id, c.nname as n, a.wert as x,
(SELECT z.wert FROM tbl_history z
WHERE z.indize_id = a.indize_id
AND z.hdate > DATE_SUB(a.hdate, INTERVAL 1 YEAR)
ORDER BY z.hdate LIMIT 1
) as y
FROM tbl_history a
LEFT JOIN tbl_indize c ON a.indize_id = c.id
WHERE a.hdate =
(SELECT b.hdate FROM tbl_history b
WHERE b.indize_id = a.indize_id ORDER BY b.hdate DESC LIMIT 1)
AND a.indize_id = 1;
执行时间
这里有一些假数据
CREATE TABLE tbl_indize (
id int(11) NOT NULL AUTO_INCREMENT,
nname varchar(145) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE tbl_history (
id int(11) NOT NULL AUTO_INCREMENT,
indize_id int(11) NOT NULL,
hdate date NOT NULL,
wert decimal(18,6) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY idx_id_date (indize_id,hdate)
);
INSERT INTO tbl_indize (nname) values ('test');
INSERT INTO tbl_history (indize_id, hdate, wert) values (1, '1970-01-01', RAND());
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 4096 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 2048 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 1024 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 512 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 256 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 128 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 64 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 32 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 16 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 8 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 4 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 2 DAY), SQRT(wert * RAND()) FROM tbl_history;
INSERT INTO tbl_history (indize_id, hdate, wert) SELECT indize_id, date_add(hdate, INTERVAL 1 DAY), SQRT(wert * RAND()) FROM tbl_history;
DELETE FROM tbl_history WHERE DAYOFWEEK(hdate) = 1;
DELETE FROM tbl_history WHERE DAYOFWEEK(hdate) = 2;
以下是解释
MariaDB的
+------+--------------------+-------+-------+---------------+-------------+---------+---------------+------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------+-------+---------------+-------------+---------+---------------+------+------------------------------------+
| 1 | PRIMARY | a | ALL | idx_id_date | NULL | NULL | NULL | 5877 | Using where |
| 1 | PRIMARY | c | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 3 | DEPENDENT SUBQUERY | b | ref | idx_id_date | idx_id_date | 4 | t.a.indize_id | 2938 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | z | ref | idx_id_date | idx_id_date | 4 | t.a.indize_id | 2938 | Using index condition; Using where |
+------+--------------------+-------+-------+---------------+-------------+---------+---------------+------+------------------------------------+
的MySQL
+----+--------------------+-------+------------+-------+---------------+-------------+---------+---------------+------+----------+------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+------------+-------+---------------+-------------+---------+---------------+------+----------+------------------------------------------+
| 1 | PRIMARY | a | NULL | ALL | idx_id_date | NULL | NULL | NULL | 5877 | 99.57 | Using where |
| 1 | PRIMARY | c | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
| 3 | DEPENDENT SUBQUERY | b | NULL | ref | idx_id_date | idx_id_date | 4 | t.a.indize_id | 1 | 100.00 | Using where; Using index; Using filesort |
| 2 | DEPENDENT SUBQUERY | z | NULL | ref | idx_id_date | idx_id_date | 4 | t.a.indize_id | 1 | 33.33 | Using index condition; Using filesort |
+----+--------------------+-------+------------+-------+---------------+-------------+---------+---------------+------+----------+------------------------------------------+
答案 0 :(得分:0)
在玩完之后,我发现了一个改进:
SELECT c.id, c.nname as n, a.wert as x,
(SELECT z.wert FROM tbl_history z
WHERE z.indize_id = a.indize_id
AND z.hdate > DATE_SUB(a.hdate, INTERVAL 1 YEAR)
ORDER BY z.hdate LIMIT 1
) as y
FROM tbl_history a
LEFT JOIN tbl_indize c ON a.indize_id = c.id
LEFT JOIN (SELECT MAX(hdate) as d, indize_id FROM tbl_history GROUP BY indize_id) x
ON x.indize_id = a.indize_id
WHERE a.hdate = x.d
AND a.indize_id = 1;
现在两个数据库的速度都还可以。
嗯 - 非常兼容^^
基本食谱
使用join-choices替换where子句中的子选择。