I have this query, which durate 1.3s. Why is so long when I have all indexes? Can I somewhat optimize or simplify this query? I need to calculate data online. Saving to a table and regenerating once in a while is not something I want.
SELECT `countries`.*, `c2`.`country_name_cs` AS `parent_name`, COUNT(countryperiods.countryPeriod_id) AS `numberCountryPeriods`, MIN(`from`) AS `min`, MAX(`to`) AS `max`, `continents`.`continent_id`, `continents`.`continent_name`, `continents`.`continent_seo`, `continents`.`continent_enabled` FROM `countries` INNER JOIN `us` LEFT JOIN `countries` AS `c2` ON c2.country_id = countries.parent_id LEFT JOIN `s` ON countries.country_id = s.country_id LEFT JOIN `countryperiods` ON countryperiods.country_id = countries.country_id LEFT JOIN `continents` ON continents.continent_id = countries.continent_id WHERE (countries.country_id = s.country_id) AND (f_user_id = '14') AND (s.id = us.f_s_id) AND (us_toChange = 1) GROUP BY `countries`.`country_id` ORDER BY `country_name` ASC LIMIT 1
I am attaching an overview of tables and indexes.
countries
CREATE TABLE `countries` (
`country_id` int(11) NOT NULL,
`continent_id` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`currency_code` varchar(10) DEFAULT NULL,
`country_code` varchar(5) DEFAULT NULL,
`country_name` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`country_seo` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`country_enabled` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
ALTER TABLE `countries`
ADD PRIMARY KEY (`country_id`),
ADD KEY `continent_id` (`continent_id`),
ADD KEY `parent_id` (`parent_id`);
s
CREATE TABLE `stamps` (
`id` int(11) NOT NULL,
`country_id` int(11) DEFAULT NULL,
`s_enabled` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
ALTER TABLE `s`
ADD PRIMARY KEY (`id`),
ADD KEY `country_id` (`country_id`);
countryperiods
CREATE TABLE `countryperiods` (
`countryPeriod_id` int(11) NOT NULL,
`country_id` int(11) DEFAULT NULL,
`from` smallint(4) DEFAULT NULL,
`to` smallint(4) DEFAULT NULL,
`countryPeriod_enabled` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
ALTER TABLE `countryperiods`
ADD PRIMARY KEY (`countryPeriod_id`),
ADD KEY `country_id` (`country_id`);
continents
CREATE TABLE `continents` (
`continent_id` int(11) NOT NULL,
`continent_name` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`continent_seo` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`continent_enabled` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
ALTER TABLE `continents`
ADD PRIMARY KEY (`continent_id`);
us
CREATE TABLE `userstamps` (
`uS_id` int(11) NOT NULL,
`f_user_id` int(11) DEFAULT NULL,
`f_s_id` int(11) DEFAULT NULL,
`us_toChange` tinyint(1) NOT NULL DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;
ALTER TABLE `us`
ADD PRIMARY KEY (`uS_id`),
ADD KEY `f_s_id` (`f_s_id`),
ADD KEY `f_user_id` (`f_user_id`),
ADD KEY `us` (`f_user_id`,`f_s_id`) USING BTREE;