【发布时间】:2020-09-10 08:11:19
【问题描述】:
编辑:另见我的答案,主要区别是phpmyadmin添加的LIMIT,但我还是不明白,phpmyadmin仍然比mysqli慢.
在我们的数据库 (+web) 服务器上,在 phpmyadmin 中进行查询与从 php (mysqli) 或直接在 mariadb 服务器上进行查询时,性能存在巨大差异。 60 秒 vs
这个查询功能很好:
SELECT * FROM `TitelDaggegevens`
WHERE `datum` > '2020-03-31' AND datum < '2020-05-02' AND `fondskosten` IS NULL
ORDER BY isbn;
但是,仅在 phpMyAdmin 中,当我们将 2020-05-02 更改为 2020-05-01 时,查询变得非常缓慢。
SHOW PROCESSLIST表示查询u主要是Sending data,同时运行。
按照mysql.rjweb.org/doc.php/index_cookbook_mysql#handler_counts,我做了以下查询系列:
FLUSH STATUS;
SELECT-query above with one of the two dates;
SHOW SESSION STATUS LIKE 'Handler%';
这些差异令人着迷。 (在所有情况下,我都忽略了所有等于 0 的值)。并且随着时间的推移保持一致。
| how: | server/MySqli | phpMyAdmin
| date used in query: | 2020-05-02 | 2020-05-01 | 2020-05-02 | 2020-05-01
| records returned: | 6912 | 1 | 6912 | 1
| avg speed: | 0.27s | 0.00s | 0.52s | 60s (!)
| Variable_name | Value | Value | Value | Value
| Handler_icp_attempts | 213197 | 206286 | 213197 | 0
| Handler_icp_match | 6912 | 1 | 6912 | 0
| Handler_read_next | 6912 | 1 | 26651 | 11728896 (!)
| Handler_read_key | 1 | 1 | 151 | 4
| Handler_commit | 1 | 1 | 152 | 5
| Handler_read_first | 0 | 0 | 1 | 1
| Handler_read_rnd_next | 0 | 0 | 82 | 83
| Handler_read_rnd | 0 | 0 | 0 | 1
| Handler_tmp_write | 0 | 0 | 67 | 67
EXPLAIN 结果在所有情况下相同(phpmyadmin/mysqli/putty+mariadb)。
[select_type] => SIMPLE
[table] => TitelDaggegevens
[type] => range
[possible_keys] => fondskosten,Datum+isbn+fondskosten
[key] => Datum+isbn+fondskosten
[key_len] => 3
[ref] =>
[Extra] => Using index condition; Using filesort
唯一的区别在于行:
[rows] => 422796 for 2020-05-01
[rows] => 450432 for 2020-05-02
问题
您能给我们任何方向,我们应该在哪里解决这个问题?我们已经工作了一周来优化 mariadb 服务器(现在是最佳的,除了在 phpmyadmin 中)并将我们的一些问题缩小到下面的示例。我们经常使用 phpmyadmin,但对表面下的东西几乎没有经验(比如它如何连接到数据库)。
关于索引/排序
在慢查询中,如果我们将ORDER BY从索引的isbn字段更改为非索引字段或完全省略ORDER BY,一切又恢复了正常的闪电速度。将 ORDER BY 更改为主键 id 也会使其变慢,但仍是索引 isbn 字段的 10 倍。
我们*知道*我们可以通过更好的索引来解决这个特定的查询,我们已经准备好实施。但是,我们想知道是什么导致了 phpmyadmin 与 mysqli/directly 的不同时间。
详情:
TitelDaggegevens 包含
表结构:
CREATE TABLE `TitelDaggegevens` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`isbn` decimal(13,0) NOT NULL,
`datum` date NOT NULL,
`volgendeDatum` date DEFAULT NULL,
`prijs` decimal(8,2) DEFAULT NULL,
`prijsExclLaag` decimal(8,2) DEFAULT NULL,
`prijsExclHoog` decimal(8,2) DEFAULT NULL,
`stadiumDienstverlening` char(2) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`stadiumLevenscyclus` char(1) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`gewicht` double(7,3) DEFAULT NULL,
`volume` double(7,3) DEFAULT NULL,
`24uurs` tinyint(1) DEFAULT NULL,
`UitgeverCode` varchar(4) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`imprintId` int(11) DEFAULT NULL,
`distributievormId` tinyint(4) DEFAULT NULL,
`boeksoort` char(1) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
`publishingStatus` tinyint(4) DEFAULT NULL,
`productAvailability` tinyint(4) DEFAULT NULL,
`voorraadAlles` mediumint(8) unsigned DEFAULT NULL,
`voorraadBeschikbaar` mediumint(8) unsigned DEFAULT NULL,
`voorraadGeblokkeerdEigenaar` smallint(5) unsigned DEFAULT NULL,
`voorraadGeblokkeerdCB` smallint(5) unsigned DEFAULT NULL,
`voorraadGereserveerd` smallint(5) unsigned DEFAULT NULL,
`fondskosten` enum('depot leverbaar','depot onleverbaar','POD','BOV','eBoek','geen') COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ISBN+datum` (`isbn`,`datum`) USING BTREE,
KEY `UitgeverCode` (`UitgeverCode`),
KEY `Imprint` (`imprintId`),
KEY `VolgendeDatum` (`volgendeDatum`),
KEY `Index op voorraad om maxima snel te vinden` (`isbn`,`voorraadAlles`) USING BTREE,
KEY `fondskosten` (`fondskosten`),
KEY `Datum+isbn+fondskosten` (`datum`,`isbn`,`fondskosten`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=16519430 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci
我们的虚拟网络+数据库+邮件服务器的配置:
MariaDB 10.4
InnoDB
CentOs7
phpMyAdmin 4.9.5
php 5.6
Apache
一些重要的 mariadb 配置参数,我们更改了虚拟网络服务器的默认配置参数:
[mysqld]
innodb_buffer_pool_size=2G
innodb_buffer_pool_instances=4
innodb_flush_log_at_trx_commit=2
tmp_table_size=64M
max_heap_table_size=64M
join_buffer_size=4M
sort_buffer_size=8M
optimizer_search_depth=5
【问题讨论】:
标签: mysql configuration phpmyadmin mariadb