Проблема:
- Запрос прерывается через 10 минут
- Запрос должен быть быстрее
Я создал следующий запрос. Эта была самой быстрой из нескольких версий. К сожалению, при наличии большего количества данных даже это происходит через 600 секунд с ошибкой «Код ошибки: 2013. Потеря соединения с сервером MySQL во время запроса».
CREATE OR REPLACE VIEW 1 AS
SELECT `Timeperiod` AS `Timeperiod` ,
"at" AS `Domain` ,
`Content Groups`AS `Content Groups`,
...
FROM a
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
"com" AS `Domain` ,
`Content Groups`AS `Content Groups`,
...
FROM b
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
"com" AS `Domain`,
`Content Groups`AS `Content Groups`,
...
FROM c
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
"fr" AS `Domain`,
`Content Groups`AS `Content Groups`,
...
FROM d
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
"it" AS `Domain`,
`Content Groups`AS `Content Groups`,
...
FROM e;
CREATE OR REPLACE VIEW 2 AS
SELECT `Timeperiod` AS `Timeperiod` ,
`Content Group` AS `Content Group` ,
"at" AS `Domain`,
...
FROM f
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
`Content Group` AS `Content Group` ,
"com" AS `Domain`,
...
FROM g
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
`Content Group` AS `Content Group` ,
"com" AS `Domain`,
...
FROM h
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
`Content Group` AS `Content Group` ,
"fr" AS `Domain`,
...
FROM i
UNION ALL
SELECT `Timeperiod` AS `Timeperiod` ,
`Content Group` AS `Content Group` ,
"it" AS `Domain`,
...
FROM j;
CREATE OR REPLACE VIEW 3 AS
SELECT CG.`Domain` AS `Domain` ,
TP.`TimeperiodAlias` AS `Timeperiod` ,
CG.`Content Groups` AS `Content Group` ,
M.`InternalName` AS `Internal Model Name`,
...
FROM 1 CG ,
Timperiods TP ,
Models M
WHERE CG.`Content Groups` LIKE CONCAT(M.`ContentGroupName`, '%')
AND CG.`Timeperiod` = TP.`Timeperiod`;
CREATE OR REPLACE VIEW 4 AS
SELECT CGD.`Domain` AS `Domain` ,
TP.`TimeperiodAlias` AS `Timeperiod` ,
CGD.`Content Group` AS `Content Group`,
...
FROM 2 CGD,
Timeperiods TP ,
Models M
WHERE CGD.`Content Group` LIKE CONCAT(M.`ContentGroupName`, '%')
AND CGD.`Timeperiod` = TP.`Timeperiod`;
DROP TABLE IF EXISTS 5;
CREATE TABLE IF NOT EXISTS 5
(
`Domain` VARCHAR(3) NOT NULL ,
`Timeperiod` VARCHAR(30) NOT NULL,
`Content Group` varchar(70),
`Internal Model Name` VARCHAR(50),
...
PRIMARY KEY (`Domain`,`Timeperiod`, `Content Group`)
)
AS
SELECT CG.`Domain` AS `Domain` ,
CG.`Timeperiod` AS `Timeperiod` ,
CG.`Content Group` AS `Content Group` ,
CG.`Internal Model Name` AS `Internal Model Name`,
...
FROM 3 CG,
4 CGD
WHERE CG.`Content Group` = CGD.`Content Group`
AND CG.`Timeperiod` = CGD.`Timeperiod`
AND CG.`Domain` = CGD.`Domain`;
Количество строк шагов:
1: 64763
2: 51932
Период времени: 36
Модели: 15
3: 2706
4: 2172
Это ОБЪЯСНЕНИЕ:
'1', 'PRIMARY', 'M', 'ALL', NULL, NULL, NULL, NULL, '15', ''
'1', 'PRIMARY', 'M', 'index', NULL, 'CGIndex', '242', NULL, '15', 'Using index; Using join buffer'
'1', 'PRIMARY', '<derived3>', 'ALL', NULL, NULL, NULL, NULL, '9528', 'Using where; Using join buffer'
'1', 'PRIMARY', 'TP', 'eq_ref', 'PRIMARY', 'PRIMARY', '65', 'CG.Timeperiod', '1', ''
'1', 'PRIMARY', '<derived9>', 'ALL', NULL, NULL, NULL, NULL, '21226', 'Using where; Using join buffer'
'1', 'PRIMARY', 'TP', 'eq_ref', 'PRIMARY', 'PRIMARY', '65', 'CGD.Timeperiod', '1', 'Using where'
'9', 'DERIVED', 'ContentGroupDuration_jMKL35_ALL', 'ALL', NULL, NULL, NULL, NULL, '17794', ''
'10', 'UNION', 'ContentGroupDurationVisitDuration_k4cZ5M_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'11', 'UNION', 'ContentGroupDurationVisitDuration_k4cZ5M_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'12', 'UNION', 'ContentGroupDuration_jMKL35_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'13', 'UNION', 'ContentGroupDuration_jMKL35_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
NULL, 'UNION RESULT', '<union9,10,11,12,13>', 'ALL', NULL, NULL, NULL, NULL, NULL, ''
'3', 'DERIVED', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'4', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'5', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
'6', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '10476', ''
'7', 'UNION', 'ContentGroups_fd33ef1_ALL', 'ALL', NULL, NULL, NULL, NULL, '1', ''
NULL, 'UNION RESULT', '<union3,4,5,6,7>', 'ALL', NULL, NULL, NULL, NULL, NULL, ''
Кто-нибудь знает, как закрепить запрос и / или как избежать прерывания соединения?
Решение:
Проблема 1: выполнить «set wait_timeout = 2147483» из командной строки (не внутри sql)
Проблема 2: сохранить промежуточные результаты во временных таблицах и добавить индексы. Затем выполните большое соединение.
Лучший
Christian