如何优化MySQL表速度

时间:2014-12-11 18:40:08

标签: mysql performance

我目前正在为我的雇主制作kpi仪表板。

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

该表有大约16.7 Mio Rows。

这里是的创建声明:

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 有什么东西显然我做错了吗?

更新 我将表分成两个表。一个大约16.6 Mio行,另一个大约0.1 Mio行。我将监控用户的行为,并可能缩放比率。

1 个答案:

答案 0 :(得分:0)

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