Simple SELECT mysql query very slow (using intersect) -


a query used work fine on production server has started becoming extremely slow (in matter of hours).

this it:

select * news_articles published = '1' , news_category_id = '4' order date_edited desc limit 1;

this takes 20-30 seconds execute (the table has ~200.000 rows)

this output of explain:

+----+-------------+---------------+-------------+----------------------------+----------------------------+---------+------+------+--------------------------------------------------------------------------+ | id | select_type | table         | type        | possible_keys              | key                        | key_len | ref  | rows |                                                                    | +----+-------------+---------------+-------------+----------------------------+----------------------------+---------+------+------+--------------------------------------------------------------------------+ |  1 | simple      | news_articles | index_merge | news_category_id,published | news_category_id,published | 5,5     | null | 8409 | using intersect(news_category_id,published); using where; using filesort | +----+-------------+---------------+-------------+----------------------------+----------------------------+---------+------+------+--------------------------------------------------------------------------+ 

playing around it, found hinting specific index (date_edited) makes faster:

select * news_articles use index (date_edited) published = '1' , news_category_id = '4' order date_edited desc limit 1;

this 1 takes milliseconds execute.

explain output 1 is:

+----+-------------+---------------+-------+---------------+-------------+---------+------+------+-------------+ | id | select_type | table         | type  | possible_keys | key         | key_len | ref  | rows |       | +----+-------------+---------------+-------+---------------+-------------+---------+------+------+-------------+ |  1 | simple      | news_articles | index | null          | date_edited | 8       | null |    1 | using | +----+-------------+---------------+-------+---------------+-------------+---------+------+------+-------------+ 

columns news_category_id, published , date_edited indexed.

the storage engine innodb.

this table structure:

create table `news_articles` (   `id` int(11) not null auto_increment,   `title` text not null,   `subtitle` text not null,   `summary` text not null,   `keywords` varchar(500) default null,   `body` mediumtext not null,   `source` varchar(255) default null,   `source_visible` int(11) default null,   `author_information` enum('none','name','signature') not null     default 'name',   `date_added` datetime not null,   `date_edited` datetime not null,   `views` int(11) default '0',   `news_category_id` int(11) default null,   `user_id` int(11) default null,   `c_forwarded` int(11) default '0',   `published` int(11) default '0',   `deleted` int(11) default '0',   `permalink` varchar(255) default null,   primary key (`id`),   key `user_id` (`user_id`),   key `news_category_id` (`news_category_id`),   key `published` (`published`),   key `deleted` (`deleted`),   key `date_edited` (`date_edited`),   constraint `news_articles_ibfk_3` foreign key (`news_category_id`) references `news_categories` (`id`) on delete set null on update cascade,   constraint `news_articles_ibfk_4` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade ) engine=innodb auto_increment=192588 default charset=utf8 

i possibly change queries web application hint using index. considerable work.

is there way tune mysql first query made more efficient without rewriting queries?

just few tips..

1 - seems me fields published , news_category_id integer. if so, please remove single quotes query. can make huge difference when comes performance;

2 - also, i'd field published has no many different values (it 1 - yes , 0 - no, or that). if i'm right, not field index @ all. parse in case still has go through records find looking for; in case move news_category_id first field in clause.

3 - "don't forget left index". affirmation valid select, joins, where, order by. position of columns on table imporant, keep indexed ones on top. indexes friend long know how play them.

hope can in somehow..

select * news_articles published = '1' , news_category_id = '4' order date_edited desc limit 1;


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -