Optimizing MySQL queries / database structure
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 retu开发者_开发知识库rns 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?
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
精彩评论