优化MySQL排序查询

发布于 2024-12-12 02:40:15 字数 1412 浏览 0 评论 0原文

例如,我有以下查询来获取上个月提交的浏览次数最多的文章。

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

如何为此查询选择正确的索引?或者如何重写它以避免扫描大量行或文件排序?

这是我尝试过的当前索引的表方案:

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);

注意:我还尝试了 mysql date 而不是 Unix timestamp,但得到了相同的结果。

这是 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-12-19 02:40:15

对于此类查询,只能使用(date)(views)索引,而不能使用复合(date,views)。 MySQL 选择使用的内容可能是最优的,也可能不是最优的。 2 月份的最佳数据可能不适用于 4 月份的数据!

  • 您可以尝试强制两者之一并衡量性能。

  • 您可以使用 CHAR(6)(例如 2011 年 4 月的 201104)添加包含 Year-Month 数据的计算列(该列可以使用 INSERT 和 UPDATE 触发器进行更新)或 int24136 (24136 = 2011*12 + 4)。

那么您的条件将是:

WHERE YearMonth = '201109'            --- for September

WHERE YearMonth = 2011*12+9      

并且可以使用 (YearMonth,views) 索引。

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.

白云不回头 2024-12-19 02:40:15

通过强制索引,就实现了。

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

解释一下计划:

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

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"
牵你的手,一向走下去 2024-12-19 02:40:15

单个键 date_2dateviews)应该为相关查询提供最佳性能。请参阅此处 我不认为另一个索引会有所帮助。由于查询如此简单,我想不出任何其他优化!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文