gpt4 book ai didi

mysql - 优化 MySQL 表速度的方法

转载 作者:行者123 更新时间:2023-11-30 22:53:17 26 4
gpt4 key购买 nike

我目前正在为我的雇主开发一个 kpi 仪表板。

仪表板显示来自 mysql 数据库的各种 KPI。不幸的是,查询速度非常慢,尽管我尝试了我在博客和 Stack Overflow 上找到的所有优化建议。

该表有大约 16.7 Mio 行。

这是的创建语句:

CREATE TABLE `V12` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_id` int(3) unsigned NOT NULL,
`vorgang_id` int(1) unsigned NOT NULL,
`sparte_id` int(2) unsigned NOT NULL,
`standort_id` int(2) unsigned NOT NULL,
`verteilungsziel_id` int(2) unsigned NOT NULL,
`haupttyp_id` int(3) unsigned NOT NULL,
`kpi_id` int(2) unsigned NOT NULL,
`value` float NOT NULL,
PRIMARY KEY (`id`),
KEY `date_idx` (`date_id`),
KEY `vorgang_idx` (`vorgang_id`),
KEY `sparte_idx` (`sparte_id`),
KEY `standort_idx` (`standort_id`),
KEY `verteilungsziel_idx` (`verteilungsziel_id`),
KEY `haupttyp_idx` (`haupttyp_id`),
KEY `kpi_idx` (`kpi_id`),
CONSTRAINT `date` FOREIGN KEY (`date_id`) REFERENCES `V12_date` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `haupttyp` FOREIGN KEY (`haupttyp_id`) REFERENCES `V12_haupttyp` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `kpi` FOREIGN KEY (`kpi_id`) REFERENCES `V12_kpi` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `sparte` FOREIGN KEY (`sparte_id`) REFERENCES `V12_sparte` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `standort` FOREIGN KEY (`standort_id`) REFERENCES `V12_standort` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `verteilungsziel` FOREIGN KEY (`verteilungsziel_id`) REFERENCES `V12_verteilungsziel` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `vorgang` FOREIGN KEY (`vorgang_id`) REFERENCES `V12_vorgang` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=16761713 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

所有引用表都是这样构建的:

CREATE TABLE `V12_haupttyp` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`value` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `haupttyp_id_UNIQUE` (`id`),
UNIQUE KEY `haupttyp_UNIQUE` (`value`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我的查询确实是这样的:

# Time: 141211 19:28:24
# User@Host: dashboard[dashboard] @ localhost []
# Query_time: 1.143774 Lock_time: 0.000075 Rows_sent: 8 Rows_examined: 850
SET timestamp=1418322504;
SELECT `id`, `kpi_id` AS `kpi`, `date_id` AS `date`, `value` FROM `V12` WHERE `date_id` = '581' AND `vorgang_id` = '2' AND `sparte_id` = '6' AND `standort_id` = '1' AND `verteilungsziel_id` = '2' AND `haupttyp_id` = '16';

# Time: 141211 19:28:33
# User@Host: dashboard[dashboard] @ localhost []
# Query_time: 1.094251 Lock_time: 0.000095 Rows_sent: 56 Rows_examined: 608
SET timestamp=1418322513;
SELECT `id`, `sparte_id` AS `sparte`, `kpi_id` AS `kpi`, `date_id` AS `date`, `value` FROM `V12` WHERE `date_id` = '581' AND `vorgang_id` = '2' AND `standort_id` = '1' AND `verteilungsziel_id` = '2' AND `haupttyp_id` = '16';

# Time: 141211 19:28:38
# User@Host: dashboard[dashboard] @ localhost []
# Query_time: 4.913999 Lock_time: 0.000098 Rows_sent: 1789 Rows_examined: 1789
SET timestamp=1418322518;
SELECT `id`, `sparte_id` AS `sparte`, `date_id` AS `date`, `value` FROM `V12` WHERE `vorgang_id` = '2' AND `standort_id` = '1' AND `verteilungsziel_id` = '2' AND `haupttyp_id` = '16' AND `kpi_id` = '4';

# Time: 141211 19:29:48
# User@Host: knorr[knorr] @ localhost []
# Query_time: 4.748257 Lock_time: 0.000134 Rows_sent: 2215 Rows_examined: 2215
SET timestamp=1418322588;
SELECT `id`, `standort_id` AS `standort`, `date_id` AS `date`, `value` FROM `V12` WHERE `vorgang_id` = '2' AND `sparte_id` = '6' AND `verteilungsziel_id` = '2' AND `haupttyp_id` = '16' AND `kpi_id` = '2';

这是我的 MySQL 配置的链接: Config on Pastebin这是我的v-servers 性能概述的链接: lscpu & memory有什么地方明显是我做错了吗?

更新:我把 table 分成两张 table 。一个大的大约有 16.6 个 Mio 行,另一个有大约 0.1 个 Mio 行。我将监控用户的行为,并可能调整比例。

最佳答案

您应该采取的第一步是使用 EXPLAIN因为了解 MySQL 如何执行查询对于您可以进行的任何优化都是必不可少的。例如,您可能缺少索引。

关于mysql - 优化 MySQL 表速度的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27430094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com