Свернутый текст
SELECT `t`.`id` AS `t0_c0`, `competitors`.`id` AS `t1_c0`, `competitors`.`sid` AS `t1_c1`, `competitors`.`comp_name` AS `t1_c2`, `competitors`.`se_compid` AS `t1_c3`, `competitorsAlexas`.`id` AS `t6_c0`, `competitorsAlexas`.`cid` AS `t6_c1`, `competitorsAlexas`.`alexa` AS `t6_c2`, `competitorsAlexas`.`date` AS `t6_c3`, `competitorsAlexas`.`time` AS `t6_c4`, `competitorsPositions`.`id` AS `t7_c0`, `competitorsPositions`.`cid` AS `t7_c1`, `competitorsPositions`.`kid` AS `t7_c2`, `competitorsPositions`.`position` AS `t7_c3`, `competitorsPositions`.`date` AS `t7_c4`, `competitorsPositions`.`time` AS `t7_c5` FROM `rw_sites` `t` LEFT OUTER JOIN `rw_competitors` `competitors` ON (`competitors`.`sid`=`t`.`id`) LEFT OUTER JOIN `rw_competitors_alexa` `competitorsAlexas` ON (`competitorsAlexas`.`cid`=`competitors`.`id`) LEFT OUTER JOIN `rw_competitors_position` `competitorsPositions` ON (`competitorsPositions`.`cid`=`competitors`.`id`) WHERE (`t`.`id` IN ('6', '7', '5')) ORDER BY competitorsAlexas.id ASC, competitorsPositions.id DESC;
Для интересующихся зачем так и почему - запрос строится динамически yii при жадной загрузке, основная цель - построение и вывод данных и зависимостей при отображении SEO-данных. Собственно вопрос потому и поднят, что запрос тяжелый и встает вопрос о целесообразности, но переписывать вручную долго и не просто и хочется понять единственный ли это вариант...
Результат выполнения EXPLAIN:
Сами таблицы которые используются
Свернутый текст
--
-- База данных: `ranker_widget`
--
-- --------------------------------------------------------
--
-- Структура таблицы `rw_competitors`
--
CREATE TABLE IF NOT EXISTS `rw_competitors` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`sid` int(18) unsigned NOT NULL,
`comp_name` varchar(255) NOT NULL,
`se_compid` int(18) NOT NULL,
PRIMARY KEY (`id`),
KEY `sid` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_competitors_alexa`
--
CREATE TABLE IF NOT EXISTS `rw_competitors_alexa` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`cid` int(18) unsigned NOT NULL,
`alexa` int(12) NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
PRIMARY KEY (`id`),
KEY `cid` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=84 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_competitors_position`
--
CREATE TABLE IF NOT EXISTS `rw_competitors_position` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`cid` int(18) unsigned NOT NULL,
`kid` int(18) unsigned NOT NULL,
`position` int(4) NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
PRIMARY KEY (`id`),
KEY `cid` (`cid`,`kid`),
KEY `kid` (`kid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=572 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_keywords`
--
CREATE TABLE IF NOT EXISTS `rw_keywords` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`sid` int(18) unsigned NOT NULL,
`keyword` varchar(255) NOT NULL,
`se_keyid` int(18) NOT NULL,
`new` int(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `sid` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=40 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_sites`
--
CREATE TABLE IF NOT EXISTS `rw_sites` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`site_name` varchar(255) NOT NULL,
`se_siteid` int(18) NOT NULL,
`ordering` int(4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `site_name` (`site_name`),
KEY `ordering` (`ordering`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_sites_alexa`
--
CREATE TABLE IF NOT EXISTS `rw_sites_alexa` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`sid` int(18) unsigned NOT NULL,
`alexa` int(12) NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
PRIMARY KEY (`id`),
KEY `sid` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_sites_pagerank`
--
CREATE TABLE IF NOT EXISTS `rw_sites_pagerank` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`sid` int(18) unsigned NOT NULL,
`pagerank` int(2) NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
PRIMARY KEY (`id`),
KEY `sid` (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;
-- --------------------------------------------------------
--
-- Структура таблицы `rw_sites_position`
--
CREATE TABLE IF NOT EXISTS `rw_sites_position` (
`id` int(18) unsigned NOT NULL AUTO_INCREMENT,
`sid` int(18) unsigned NOT NULL,
`kid` int(18) unsigned NOT NULL,
`position` int(4) NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
PRIMARY KEY (`id`),
KEY `sid` (`sid`,`kid`),
KEY `kid` (`kid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=168 ;
--
-- Ограничения внешнего ключа сохраненных таблиц
--
--
-- Ограничения внешнего ключа таблицы `rw_competitors`
--
ALTER TABLE `rw_competitors`
ADD CONSTRAINT `rw_competitors_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `rw_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ограничения внешнего ключа таблицы `rw_competitors_alexa`
--
ALTER TABLE `rw_competitors_alexa`
ADD CONSTRAINT `rw_competitors_alexa_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `rw_competitors` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ограничения внешнего ключа таблицы `rw_competitors_position`
--
ALTER TABLE `rw_competitors_position`
ADD CONSTRAINT `rw_competitors_position_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `rw_competitors` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `rw_competitors_position_ibfk_2` FOREIGN KEY (`kid`) REFERENCES `rw_keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ограничения внешнего ключа таблицы `rw_keywords`
--
ALTER TABLE `rw_keywords`
ADD CONSTRAINT `rw_keywords_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `rw_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ограничения внешнего ключа таблицы `rw_sites_alexa`
--
ALTER TABLE `rw_sites_alexa`
ADD CONSTRAINT `rw_sites_alexa_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `rw_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ограничения внешнего ключа таблицы `rw_sites_pagerank`
--
ALTER TABLE `rw_sites_pagerank`
ADD CONSTRAINT `rw_sites_pagerank_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `rw_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ограничения внешнего ключа таблицы `rw_sites_position`
--
ALTER TABLE `rw_sites_position`
ADD CONSTRAINT `rw_sites_position_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `rw_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `rw_sites_position_ibfk_2` FOREIGN KEY (`kid`) REFERENCES `rw_keywords` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Заранее благодарен.