简体   繁体   中英

MYSQL - order by indexed column

i have this table with 500,000 row

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 ;

i want to make a query like this

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 

this query some time work as i want exactly ( see this ) , but after some opinions, this is incorrect, i searched on the web for solution, i found

this : http://venublog.com/2007/11/29/mysql-how-to-avoid-filesort/ and this: http://www.getsymphony.com/discuss/issues/view/657/ and i add index on views_point then tried this query

select t1.* 
from  listings t1 
left outer join  listings t2 on (t1.views_point=t2.views_point) 
order by t1.views_point 
limit 10

and this the explain

在此处输入图片说明

but i can not add this condition

`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

i don't know, which one should i assign to (t1 or t2) ?

in the other hand what about this way

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

if any one will not be good,can any one tell me please how the big blogs get the posts by hits?

You should split your listings index to separate indices.

Apart from this, given that you sort by t1 , it is almost certain that you should specify t1 for all conditions.

EDIT: Use condition as

`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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM