我的这张表有500,000行
CREATE TABLE IF NOT EXISTS `listings` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`source_id` int(10) unsigned NOT NULL,
`cat_id` mediumint(5) NOT NULL,
`title` mediumtext NOT NULL,
`views_point` int(10) unsigned NOT NULL DEFAULT '0',
`publishedon_hourly` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `views_point` (`views_point`),
KEY `listings` (`publishedon_hourly`,`published`,`cat_id`,`source_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED AUTO_INCREMENT=365513 ;
我想像这样进行查询
SELECT *
FROM listings
WHERE (
`publishedon_hourly`
BETWEEN UNIX_TIMESTAMP( '2015-09-5 12:00:00' )AND UNIX_TIMESTAMP( '2015-10-5 12:00:00' ) ) AND ( published =1 ) AND cat_id
IN ( 1, 2, 3, 4, 5 )
ORDER BY views_point DESC
LIMIT 10
这个查询一段时间工作正如我想要的那样(see this),但经过一些意见,这是不正确的,我在网上搜索解决方案,我找到了
这:http://venublog.com/2007/11/29/mysql-how-to-avoid-filesort/
而这:http://www.getsymphony.com/discuss/issues/view/657/
然后我在views_point
添加索引然后尝试了这个查询
select t1.*
from listings t1
left outer join listings t2 on (t1.views_point=t2.views_point)
order by t1.views_point
limit 10
这个解释
但我无法添加此条件
`publishedon_hourly` BETWEEN UNIX_TIMESTAMP( '2015-09-5 12:00:00' ) AND UNIX_TIMESTAMP( '2015-09-5 12:00:00' ) ) AND ( published =1 ) AND cat_id = 5
我不知道,我应该指定哪一个(t1或t2)?
另一方面怎么样呢
SELECT *
FROM listings
WHERE (
`publishedon_hourly` BETWEEN UNIX_TIMESTAMP( '2015-09-5 00:00:00' ) AND UNIX_TIMESTAMP( '2015-09-5 23:00:00' ))
AND (published =1)
and views_point is not null
ORDER BY views_point DESC limit 20
如果任何一个人不好,任何人都可以告诉我大博客如何通过点击获得帖子?
答案 0 :(得分:1)
您应该将listings
索引拆分为单独的索引。
除此之外,鉴于您按t1
排序,几乎可以肯定您应为所有条件指定t1
。
编辑: 使用条件为
`t1`.`publishedon_hourly` BETWEEN UNIX_TIMESTAMP( '2015-09-5 12:00:00' ) AND UNIX_TIMESTAMP( '2015-09-5 12:00:00' ) ) AND ( `t1`.`published` = 1 ) AND `t1`.`cat_id` = 5