Optimize MySql query to avoid using "Using filesort"

I need your help to optimize the query to avoid using File System Usage. The task of the request is to select all articles related to a specific tag. Inquiry:

  select title 
    from tag,
         article 
   where tag = 'Riyad' 
     AND tag.article_id = article.id 
order by tag.article_id

The structure of the tables is as follows:

Tag table

 CREATE TABLE `tag` (
 `tag` VARCHAR( 30 ) NOT NULL ,
 `article_id` INT NOT NULL ,
 KEY `tag` (`tag`),
 KEY `article_id` (`article_id`)
 ) ENGINE = MYISAM ;

Article table

 CREATE TABLE `article` (
 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `title` VARCHAR( 60 ) NOT NULL
 ) ENGINE = MYISAM 

Data examples

 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);
+3
source share
2 answers

In the table, tagreplace the key tagwith the ( tag, article_id) key :

ALTER TABLE `tag` DROP INDEX `tag`, ADD INDEX `tag_article_id` (`tag`, `article_id`)

MySQL . tag, ORDER BY. , ORDER BY.

+7

, , tag.article_id=article.id, , . ( ) tag.article_id.

0

Source: https://habr.com/ru/post/1747762/


All Articles