Всем привет и с наступившим Новым Годом!
Теперь к вопросу: имеется sql запрос
SELECT `tema`.*,
(SELECT Count(*)
FROM `post`
WHERE `post`.`id_tema` = `tema`.`id`) AS `count_post`,
(SELECT `id`
FROM `post`
WHERE `post`.`id_tema` = `tema`.`id`
ORDER BY `id` DESC
LIMIT 1) AS `id_post`,
(SELECT `login`
FROM `users`
WHERE `users`.`id` = (SELECT `id_user`
FROM `post`
WHERE `post`.`id_tema` = `tema`.`id`
ORDER BY `id` DESC
LIMIT 1)
) AS `login`
FROM `tema`
WHERE `id_forum` = '4'
ORDER BY `up` DESC, `time` DESC
LIMIT 3260, 20
Срок выполнения - 2.7 секунды.
Структура БД:
--
-- Структура таблицы `tema`
--
DROP TABLE IF EXISTS `tema`;
CREATE TABLE IF NOT EXISTS `tema` (
`id` int(11) NOT NULL auto_increment,
`id_razdel` int(11) NOT NULL,
`id_forum` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`name` varchar(500) NOT NULL,
`translate` varchar(500) NOT NULL,
`text` text NOT NULL,
`time` int(11) NOT NULL,
`up` enum('0', '1') NOT NULL default '0',
`closed` enum('0', '1') NOT NULL default '0',
`count` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `id_razdel` (`id_razdel`),
KEY `id_forum` (`id_forum`),
KEY `id_user` (`id_user`),
KEY `time` (`time`),
KEY `up` (`up`),
FULLTEXT KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Структура таблицы `post`
--
DROP TABLE IF EXISTS `post`;
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL auto_increment,
`id_razdel` int(11) NOT NULL,
`id_forum` int(11) NOT NULL,
`id_tema` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`text` text NOT NULL,
`cit` int(11) NOT NULL,
`time` int(11) NOT NULL,
`timeedit` int(11) NOT NULL,
`kedit` int(11) NOT NULL,
`id_user_edit` int(11) NOT NULL,
`vote` varchar(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_razdel` (`id_razdel`),
KEY `id_forum` (`id_forum`),
KEY `id_tema` (`id_tema`),
KEY `id_user` (`id_user`),
KEY `time` (`time`),
FULLTEXT KEY `text` (`text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Собственно вопрос: как можно оптимизировать данный sql запрос?