1

I have the following query to get the most viewed articles that submitted in last month for example.

explain 
SELECT * 
FROM article 
WHERE date > 1315391769 
ORDER BY views DESC 
LIMIT 10 

how do I choose the right index for this query? or how do I re-write it to avoid scanning a lot of rows or file sorting?

This is the table scheme with the current indexes I tried:

CREATE TABLE `article` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(50) NOT NULL,
  `body` text NOT NULL,
  `date` int(32) NOT NULL,
  `views` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `date` (`date`),
  KEY `views` (`views`),
  KEY `date_2` (`date`,`views`),
  KEY `views_2` (`views`,`date`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=200003 ;

-- 
-- Dumping data for table `article`
-- 

INSERT INTO `article` VALUES (1, 'title test113', 'test body118', 1317912183, 5017);
INSERT INTO `article` VALUES (2, 'title test193', 'test body193', 1313441124, 5943);
INSERT INTO `article` VALUES (3, 'title test112', 'test body116', 1312773586, 653);
INSERT INTO `article` VALUES (4, 'title test378', 'test body374', 1316786646, 4589);
INSERT INTO `article` VALUES (5, 'title test335', 'test body3310', 1319173694, 6224);

Notice: I also tried mysql date instead of Unix timestamp but I got the same result.

This is the output of EXPLAIN:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  article     range   date,date_2     date    4   NULL    107245  Using where; Using filesort
5
  • @ I posted only 5 rows as sample. Commented Oct 27, 2011 at 23:31
  • what is an int(32) ? is it some kinda signed bigger int ? and for that matter, what is an int(11) ?? dev.mysql.com/doc/refman/5.0/en/numeric-types.html Commented Oct 27, 2011 at 23:48
  • It should be 11 not 32. I already change it to MySQL time and MySQL datetime and I got the same result. Commented Oct 28, 2011 at 0:01
  • EXPLAIN is rather useless when the table has so few rows. Try having a few thousands or tens of thousands. Commented Oct 28, 2011 at 0:18
  • @ypercube there are more than 200000 rows Commented Oct 28, 2011 at 0:20

3 Answers 3

1

For this kind of queries, only either (date) or (views) index can be used and not the compound (date,views). What MySQL chooses to use may or may not be optimal. And what is optimal for February may not be for April data!

  • You can try forcing one of the two and measure preformance.

  • You could add a calculated column with Year-Month data, using a CHAR(6) like 201104 for April 2011 (that column can be updated using INSERT and UPDATE triggers) or an int like 24136 (24136 = 2011*12 + 4).

Then your condition would be:

WHERE YearMonth = '201109'            --- for September

or

WHERE YearMonth = 2011*12+9      

and a (YearMonth, views) index could be used.

Sign up to request clarification or add additional context in comments.

Comments

1

By forcing index, it is achieved.

explain 
SELECT * 
FROM article force index (`views`)
WHERE date>=1315391768  
ORDER BY views DESC 
LIMIT 10 

Explain plan:

"id"    "select_type"   "table"     "type"       "possible_keys"    "key"   "key_len"     "ref" "rows"  "Extra"
 "1"    "SIMPLE"       "article"    "index"           \N           "views"    "4"           \N  "5" "Using where"

Comments

1

A single key date_2 (date,views) should give optimal performance for the query in question. See here I don't think the other indexes are going to help. Since the query is so simple I can't think of any other optimization!

1 Comment

No, user_ksa is correct. Either (date) or (views) index can be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.