我需要您的帮助来优化查询以避免使用“使用文件排序”。查询的工作是选择属于特定标签的所有文章。查询是:
select title
from tag,
article
where tag = 'Riyad'
AND tag.article_id = article.id
order by tag.article_id
表结构如下:
标记表
CREATE TABLE `tag` (
`tag` VARCHAR( 30 ) NOT NULL ,
`article_id` INT NOT NULL ,
KEY `tag` (`tag`),
KEY `article_id` (`article_id`)
) ENGINE = MYISAM ;
文章表
CREATE TABLE `article` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 60 ) NOT NULL
) ENGINE = MYISAM
样本数据
INSERT INTO `article` VALUES (1, 'About Riyad');
INSERT INTO `article` VALUES (2, 'About Newyork');
INSERT INTO `article` VALUES (3, 'About Paris');
INSERT INTO `article` VALUES (4, 'About London');
INSERT INTO `tag` VALUES ('Riyad', 1);
INSERT INTO `tag` VALUES ('Saudia', 1);
INSERT INTO `tag` VALUES ('Newyork', 2);
INSERT INTO `tag` VALUES ('USA', 2);
INSERT INTO `tag` VALUES ('Paris', 3);
INSERT INTO `tag` VALUES ('France', 3);