views:

36

answers:

1

I have in my MySQL database these two tables:

CREATE TABLE IF NOT EXISTS `articles` (
  `id` bigint(20) NOT NULL,
  `url` varchar(255) collate utf8_bin NOT NULL,
  `img` varchar(255) collate utf8_bin NOT NULL,
  `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `url_key` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `type_code` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `likes_count` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `type` (`type`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `articles_types` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `url_key` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `name` (`code`),
  KEY `type` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

And I'm trying to run this query:

SELECT a.* FROM `articles` a INNER JOIN articles_types at ON at.`code`=a.type ORDER BY p.likes_count DESC LIMIT 1

Which returns 1 row, in 1sec.

The table articles consists of aproximately over 70k rows, articles_types only about 70 rows.

Is there any chance to optimize the structure (maybe engine?) of that tables, or optimize the query itself to make it faster?

+3  A: 

You should examine the output of EXPLAIN on your query

It's likely the order by that gets you, so create an index on articles.likes_count

nos
You're absolutely right. The problem here is the ORDERing - but even after adding the index to articles.likes_count, it takes 1-2sec.Without sorting results (without ORDER BY) its very fast - e.g. 0.0004sec.
Radek Šimko
It helped to use LEFT JOIN instead of INNER JOIN. Perhaps it may happend, that will appears an article with unknown category, so I'll have to check that in application.
Radek Šimko