我有这个查询需要1.7秒才能执行:
SELECT
i.id_items AS id,
i.names,
GROUP_CONCAT(DISTINCT iafv.face_value ORDER BY iafv.face_value ASC SEPARATOR ', ') AS facesValues,
GROUP_CONCAT(DISTINCT iacut.translation ORDER BY iacut.translation ASC SEPARATOR ', ') AS currencies,
GROUP_CONCAT(DISTINCT cout.translation ORDER BY cout.translation ASC SEPARATOR ', ') AS countries,
GROUP_CONCAT(DISTINCT iacot.translation ORDER BY iacot.translation ASC SEPARATOR ', ') AS compositions,
GROUP_CONCAT(DISTINCT iacc.catalog_code ORDER BY iacc.catalog_code ASC SEPARATOR ', ') AS catalogCodes,
GROUP_CONCAT(DISTINCT iaio.issues_on ORDER BY iaio.issues_on ASC SEPARATOR ', ') AS issuesOn,
GROUP_CONCAT(DISTINCT iali.last_issues ORDER BY iali.last_issues ASC SEPARATOR ', ') AS latestIssues,
MIN(ucfs.price) AS minPrice,
SUM(ucfs.units) AS totalUnits
FROM
(SELECT
i.id_items,
GROUP_CONCAT(DISTINCT iant.translation ORDER BY iant.translation asc SEPARATOR ', ') AS names
FROM
items i
INNER JOIN languages AS l ON l.language_code = "en"
LEFT JOIN items_atr_names_match AS ianm ON ianm.id_items = i.id_items
LEFT JOIN items_atr_names_translations AS iant ON iant.id_items_atr_names = ianm.id_items_atr_names AND iant.id_language_code = l.id_languages
LEFT JOIN items_atr_names AS ian ON ian.id_items_atr_names = ianm.id_items_atr_names
GROUP BY id_items
ORDER BY names asc
LIMIT 0, 20) AS i
INNER JOIN languages AS l ON l.language_code = "en"
LEFT JOIN items_atr_faces_values_match AS iafvm ON iafvm.id_items = i.id_items
LEFT JOIN items_atr_faces_values AS iafv ON iafv.id_items_atr_faces_values = iafvm.id_items_atr_faces_values
LEFT JOIN items_atr_currencies_match AS iacum ON iacum.id_items = i.id_items
LEFT JOIN items_atr_currencies_translations AS iacut ON iacut.id_items_atr_currencies = iacum.id_items_atr_currencies AND iacut.id_language_code = l.id_languages
LEFT JOIN items_atr_currencies AS iacu ON iacu.id_items_atr_currencies = iacum.id_items_atr_currencies
LEFT JOIN items_atr_countries_match AS iacoum ON iacoum.id_items = i.id_items
LEFT JOIN items_atr_countries_translations AS cout ON cout.id_items_atr_countries = iacoum.id_items_atr_countries AND cout.id_language_code = l.id_languages
LEFT JOIN items_atr_countries AS cou ON cou.id_items_atr_countries = iacoum.id_items_atr_countries
LEFT JOIN items_atr_compositions_match AS iacom ON iacom.id_items = i.id_items
LEFT JOIN items_atr_compositions_translations AS iacot ON iacot.id_items_atr_compositions = iacom.id_items_atr_compositions AND iacot.id_language_code = l.id_languages
LEFT JOIN items_atr_compositions AS iaco ON iaco.id_items_atr_compositions = iacom.id_items_atr_compositions
LEFT JOIN items_atr_catalog_codes_match AS iaccm ON iaccm.id_items = i.id_items
LEFT JOIN items_atr_catalog_codes AS iacc ON iacc.id_items_atr_catalog_codes = iaccm.id_items_atr_catalog_codes
LEFT JOIN items_atr_issues_on_match AS iaiom ON iaiom.id_items = i.id_items
LEFT JOIN items_atr_issues_on AS iaio ON iaio.id_items_atr_issues_on = iaiom.id_items_atr_issues_on
LEFT JOIN items_atr_last_issues_match AS ialim ON ialim.id_items = i.id_items
LEFT JOIN items_atr_last_issues AS iali ON iali.id_items_atr_last_issues = ialim.id_items_atr_last_issues
LEFT JOIN users_coins_for_sale AS ucfs ON ucfs.id_items = i.id_items
LEFT JOIN users AS u ON u.id_users = ucfs.id_users AND u.status = 'active'
GROUP BY id
ORDER BY names asc
这是具有索引信息的表的创建sql:
CREATE TABLE IF NOT EXISTS `items` (
`id_items` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_users` int(11) unsigned NOT NULL DEFAULT '0',
`date` date NOT NULL,
`views` bigint(20) NOT NULL DEFAULT '0',
`status` enum('draft','published') NOT NULL DEFAULT 'draft',
PRIMARY KEY (`id_items`),
UNIQUE KEY `id_items` (`id_items`),
KEY `id_users` (`id_users`),
CONSTRAINT `FK_items_users` FOREIGN KEY (`id_users`) REFERENCES `users` (`id_users`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_catalog_codes` (
`id_items_atr_catalog_codes` int(11) unsigned NOT NULL AUTO_INCREMENT,
`catalog_code` varchar(100) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_catalog_codes`),
UNIQUE KEY `id_items_atr_catalog_codes` (`id_items_atr_catalog_codes`),
FULLTEXT KEY `catalog_code` (`catalog_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_catalog_codes_match` (
`id_items_atr_catalog_codes_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_catalog_codes` int(11) unsigned NOT NULL,
`id_items` int(11) unsigned NOT NULL,
PRIMARY KEY (`id_items_atr_catalog_codes_match`),
UNIQUE KEY `id_items_atr_catalog_codes_match` (`id_items_atr_catalog_codes_match`),
KEY `id_items` (`id_items`),
KEY `id_items_atr_catalog_codes` (`id_items_atr_catalog_codes`),
CONSTRAINT `FK_catalog_codes_items_match_catalog_codes` FOREIGN KEY (`id_items_atr_catalog_codes`) REFERENCES `items_atr_catalog_codes` (`id_items_atr_catalog_codes`),
CONSTRAINT `FK_catalog_codes_items_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_compositions` (
`id_items_atr_compositions` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id_items_atr_compositions`),
UNIQUE KEY `id_items_atr_compositions` (`id_items_atr_compositions`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_compositions_match` (
`id_items_atr_compositions_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_compositions` int(11) unsigned NOT NULL DEFAULT '0',
`id_items` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_compositions_match`),
UNIQUE KEY `id_items_atr_compositions_match` (`id_items_atr_compositions_match`),
KEY `id_items` (`id_items`),
KEY `id_items_atr_compositions` (`id_items_atr_compositions`),
CONSTRAINT `FK_compositions_items_match_compositions` FOREIGN KEY (`id_items_atr_compositions`) REFERENCES `items_atr_compositions` (`id_items_atr_compositions`),
CONSTRAINT `FK_compositions_items_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_compositions_translations` (
`id_items_atr_compositions_translations` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_compositions` int(11) unsigned NOT NULL DEFAULT '0',
`id_language_code` int(11) unsigned NOT NULL DEFAULT '0',
`translation` varchar(250) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_compositions_translations`),
UNIQUE KEY `id_items_atr_compositions_translations` (`id_items_atr_compositions_translations`),
KEY `id_language_code` (`id_language_code`),
KEY `id_items_atr_compositions` (`id_items_atr_compositions`),
FULLTEXT KEY `translation` (`translation`),
CONSTRAINT `FK_compositions_translations_compositions` FOREIGN KEY (`id_items_atr_compositions`) REFERENCES `items_atr_compositions` (`id_items_atr_compositions`),
CONSTRAINT `FK_compositions_translations_languages` FOREIGN KEY (`id_language_code`) REFERENCES `languages` (`id_languages`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_countries` (
`id_items_atr_countries` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id_items_atr_countries`),
UNIQUE KEY `id_items_atr_countries` (`id_items_atr_countries`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_countries_match` (
`id_items_atr_countries_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_countries` int(11) unsigned NOT NULL,
`id_items` int(11) unsigned NOT NULL,
PRIMARY KEY (`id_items_atr_countries_match`),
UNIQUE KEY `items_atr_countries_match` (`id_items_atr_countries_match`),
KEY `id_items` (`id_items`),
KEY `id_items_atr_countries` (`id_items_atr_countries`),
CONSTRAINT `FK__countries` FOREIGN KEY (`id_items_atr_countries`) REFERENCES `items_atr_countries` (`id_items_atr_countries`),
CONSTRAINT `FK__items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_countries_translations` (
`id_items_atr_countries_translations` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_countries` int(11) unsigned NOT NULL DEFAULT '0',
`id_language_code` int(11) unsigned NOT NULL DEFAULT '0',
`translation` varchar(250) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_countries_translations`),
UNIQUE KEY `id_items_atr_countries_translations` (`id_items_atr_countries_translations`),
KEY `id_items_atr_countries` (`id_items_atr_countries`),
KEY `id_language_code` (`id_language_code`),
FULLTEXT KEY `translation` (`translation`),
CONSTRAINT `FK__items_atr_countries` FOREIGN KEY (`id_items_atr_countries`) REFERENCES `items_atr_countries` (`id_items_atr_countries`),
CONSTRAINT `FK_items_atr_countries_translations_languages` FOREIGN KEY (`id_language_code`) REFERENCES `languages` (`id_languages`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_currencies` (
`id_items_atr_currencies` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id_items_atr_currencies`),
UNIQUE KEY `id_items_atr_currencies` (`id_items_atr_currencies`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_currencies_match` (
`id_items_atr_currencies_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_currencies` int(11) unsigned NOT NULL DEFAULT '0',
`id_items` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_currencies_match`),
UNIQUE KEY `id_items_atr_currencies_match` (`id_items_atr_currencies_match`),
KEY `id_items_atr_currencies` (`id_items_atr_currencies`),
KEY `id_items` (`id_items`),
CONSTRAINT `FK_items_atr_currencies_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`),
CONSTRAINT `FK_items_atr_currencies_match_items_atr_currencies` FOREIGN KEY (`id_items_atr_currencies`) REFERENCES `items_atr_currencies` (`id_items_atr_currencies`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_currencies_translations` (
`id_items_atr_currencies_translations` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_currencies` int(11) unsigned NOT NULL DEFAULT '0',
`id_language_code` int(11) unsigned NOT NULL DEFAULT '0',
`translation` varchar(255) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_currencies_translations`),
UNIQUE KEY `id_items_atr_currencies_translations` (`id_items_atr_currencies_translations`),
KEY `id_items_atr_currencies` (`id_items_atr_currencies`),
KEY `id_language_code` (`id_language_code`),
FULLTEXT KEY `translation` (`translation`),
CONSTRAINT `FK_items_atr_currencies_translations_items_atr_currencies` FOREIGN KEY (`id_items_atr_currencies`) REFERENCES `items_atr_currencies` (`id_items_atr_currencies`),
CONSTRAINT `FK_items_atr_currencies_translations_languages` FOREIGN KEY (`id_language_code`) REFERENCES `languages` (`id_languages`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_faces_values` (
`id_items_atr_faces_values` int(11) unsigned NOT NULL AUTO_INCREMENT,
`face_value` decimal(11,2) unsigned NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id_items_atr_faces_values`),
KEY `face_value` (`face_value`),
KEY `id-fv` (`id_items_atr_faces_values`,`face_value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_faces_values_match` (
`id_items_atr_faces_values_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_faces_values` int(11) unsigned NOT NULL DEFAULT '0',
`id_items` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_faces_values_match`),
UNIQUE KEY `id_items_atr_faces_values_match` (`id_items_atr_faces_values_match`),
KEY `id_items_atr_faces_values` (`id_items_atr_faces_values`),
KEY `id_items` (`id_items`),
KEY `idItems-IdFaceValue` (`id_items`,`id_items_atr_faces_values`),
CONSTRAINT `FK_items_atr_faces_values_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`),
CONSTRAINT `FK_items_atr_faces_values_match_items_atr_faces_values` FOREIGN KEY (`id_items_atr_faces_values`) REFERENCES `items_atr_faces_values` (`id_items_atr_faces_values`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_issues_on` (
`id_items_atr_issues_on` int(11) unsigned NOT NULL AUTO_INCREMENT,
`issues_on` varchar(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_issues_on`),
UNIQUE KEY `id_items_atr_issues_on` (`id_items_atr_issues_on`),
FULLTEXT KEY `issues_on` (`issues_on`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_issues_on_match` (
`id_items_atr_issues_on_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_issues_on` int(11) unsigned NOT NULL,
`id_items` int(11) unsigned NOT NULL,
PRIMARY KEY (`id_items_atr_issues_on_match`),
UNIQUE KEY `id_items_atr_issues_on_match` (`id_items_atr_issues_on_match`),
KEY `id_items_atr_issues_on` (`id_items_atr_issues_on`),
KEY `id_items` (`id_items`),
CONSTRAINT `FK_items_atr_issues_on_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`),
CONSTRAINT `FK_items_atr_issues_on_match_items_atr_issues_on` FOREIGN KEY (`id_items_atr_issues_on`) REFERENCES `items_atr_issues_on` (`id_items_atr_issues_on`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_last_issues` (
`id_items_atr_last_issues` int(11) unsigned NOT NULL AUTO_INCREMENT,
`last_issues` varchar(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_last_issues`),
UNIQUE KEY `id_items_atr_last_issues` (`id_items_atr_last_issues`),
FULLTEXT KEY `last_issues` (`last_issues`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_last_issues_match` (
`id_items_atr_last_issues_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_last_issues` int(11) unsigned NOT NULL DEFAULT '0',
`id_items` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_last_issues_match`),
UNIQUE KEY `id_items_atr_last_issues_match` (`id_items_atr_last_issues_match`),
KEY `id_items_atr_last_issues` (`id_items_atr_last_issues`),
KEY `id_items` (`id_items`),
CONSTRAINT `FK_items_atr_last_issues_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`),
CONSTRAINT `FK_items_atr_last_issues_match_items_atr_last_issues` FOREIGN KEY (`id_items_atr_last_issues`) REFERENCES `items_atr_last_issues` (`id_items_atr_last_issues`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_names` (
`id_items_atr_names` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id_items_atr_names`),
UNIQUE KEY `id_items_atr_names` (`id_items_atr_names`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_names_match` (
`id_items_atr_names_match` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_names` int(11) unsigned NOT NULL DEFAULT '0',
`id_items` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_names_match`),
UNIQUE KEY `id_items_atr_names_match` (`id_items_atr_names_match`),
KEY `id_items_atr_names` (`id_items_atr_names`),
KEY `id_items` (`id_items`),
CONSTRAINT `FK_items_atr_names_match_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`),
CONSTRAINT `FK_items_atr_names_match_items_atr_names` FOREIGN KEY (`id_items_atr_names`) REFERENCES `items_atr_names` (`id_items_atr_names`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items_atr_names_translations` (
`id_items_atr_names_translations` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items_atr_names` int(11) unsigned NOT NULL DEFAULT '0',
`id_language_code` int(11) unsigned NOT NULL DEFAULT '0',
`translation` varchar(250) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_items_atr_names_translations`),
UNIQUE KEY `id_items_atr_names_translations` (`id_items_atr_names_translations`),
KEY `id_language_code` (`id_language_code`),
KEY `id_items_atr_names` (`id_items_atr_names`),
KEY `translation_indx` (`translation`),
FULLTEXT KEY `translation` (`translation`),
CONSTRAINT `FK_items_atr_names_translations_items_atr_names` FOREIGN KEY (`id_items_atr_names`) REFERENCES `items_atr_names` (`id_items_atr_names`),
CONSTRAINT `FK_items_atr_names_translations_languages` FOREIGN KEY (`id_language_code`) REFERENCES `languages` (`id_languages`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `languages` (
`id_languages` int(11) unsigned NOT NULL,
`language_code` char(2) NOT NULL,
PRIMARY KEY (`id_languages`),
UNIQUE KEY `language_code` (`language_code`),
KEY `id-language_code` (`id_languages`,`language_code`),
KEY `language-code` (`language_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `languages_translations` (
`id_languages_translations` int(11) unsigned NOT NULL,
`id_languages` int(11) unsigned NOT NULL,
`id_language_code` int(11) unsigned NOT NULL,
`translation` varchar(255) NOT NULL,
PRIMARY KEY (`id_languages_translations`),
UNIQUE KEY `id_languages_translations` (`id_languages_translations`),
KEY `id_languages` (`id_languages`),
KEY `language_code` (`id_language_code`),
FULLTEXT KEY `translation` (`translation`),
CONSTRAINT `FK_languages_translations_languages` FOREIGN KEY (`id_languages`) REFERENCES `languages` (`id_languages`),
CONSTRAINT `FK_languages_translations_languages_2` FOREIGN KEY (`id_language_code`) REFERENCES `languages` (`id_languages`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `users_coins_for_sale` (
`id_users_coins_for_sale` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_items` int(11) unsigned NOT NULL,
`id_users` int(11) unsigned NOT NULL,
`units` int(5) unsigned NOT NULL,
`year` varchar(5) NOT NULL,
`grade` enum('good','very_good','fine','very_fine','extra_fine','about_uncirculated','uncirculated','brilliant_uncirculated','proof') NOT NULL,
`price` decimal(8,2) unsigned NOT NULL,
`picture` varchar(500) DEFAULT NULL,
`comment` varchar(500) DEFAULT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id_users_coins_for_sale`),
UNIQUE KEY `id_users_coins_for_sale_unique` (`id_users_coins_for_sale`),
KEY `id_items` (`id_items`),
KEY `id_users` (`id_users`),
KEY `year` (`year`),
KEY `grade` (`grade`),
KEY `id_users_coins_for_sale` (`id_users_coins_for_sale`),
KEY `idItems-idUsers` (`id_items`,`id_users`),
CONSTRAINT `FK_users_coins_for_sale_items` FOREIGN KEY (`id_items`) REFERENCES `items` (`id_items`),
CONSTRAINT `FK_users_coins_for_sale_users` FOREIGN KEY (`id_users`) REFERENCES `users` (`id_users`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在解释中我有时会看到索引被使用但有时没有,我想知道如果我正确地创建索引或者我缺少任何索引,我可以做些什么来提高这个查询的性能。 / p>
项目表有90k条目,但我认为它不够大,需要1.7秒......
我一直在关注此链接以了解最佳做法:https://stackoverflow.com/a/3049329
您对如何提高性能有任何建议吗?
提前致谢
答案 0 :(得分:0)
如果JOINs
不是1:1,SUM()
可能会被夸大。检查值!
当GROUP BY
和ORDER BY
不同时,没有索引可以帮助避免在LIMIT
之前收集暂定结果集。否则,某些索引可能会一直到达LIMIT
,从而避免完全扫描。
PRIMARY KEY
是UNIQUE KEY
,因此后者是多余的; DROP
它。 (我在表格的所有(?)中看到了这种模式。)
This讨论了许多的最佳索引:许多映射表。
这闻起来像一个"键值"或" EAV"架构很糟糕。
有时候这是更好的改变
此
GROUP_CONCAT(DISTINCT ... )
...
LEFT JOIN ...
到
( SELECT GROUP_CONCAT( ... ) FROM ... )
上述建议在派生查询中尤为重要,因为它可能是最慢的部分。建议你拉出子查询并计算时间并优化它。请注意,它可能会扫描大量行,但不会超过20个。然后,假设您至少拥有最小索引,那么这20个应该不会对其余查询造成太大负担。
唉,这只有一半有用 - 它可能会摆脱GROUP BY
,但它无法摆脱全扫描。这是因为ORDER BY
取决于计算值(names
)。不过,它应该摆脱" inflate-deflate"由JOINs
+ GROUP BY
引起。
摆脱表languages
。它所做的只是将类似'en'
的2字节language_like映射到4字节INT
。这是浪费(1)空间,(2)JOINs
。
很抱歉,但这个建议清单可能不完整。请尽你所能改进,然后再回来寻求更多帮助。