我有這個表500,000行MYSQL - 爲了通過索引列
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
如果任何一個也不會好,任何一個可以告訴我的大博客,請是如何讓命中的職位?
爲什麼地球上你想加入自己的表? – Victor
簡化您的OP,從OP的CREATE TABLE中刪除未使用或不重要的列,刪除除簡單內容的5條記錄以外的所有數據。不要解釋你在其他實驗中做了什麼,而只是根據這5個樣本記錄,你的預期結果是什麼。 – Alex
我們不知道你應該添加哪一個,除非你解釋你想要得到的結果。 – Barmar