Php 如何选择mysql两列取最后200 id订单描述,然后通过计数器描述获得5个订单以获得最佳性能
我的桌子上有一百万条记录。 我只想提取最后200行中计数器最多的5行 我用这个,但我认为这是两次选择。。这可能不适合最佳性能塑料建议Php 如何选择mysql两列取最后200 id订单描述,然后通过计数器描述获得5个订单以获得最佳性能,php,mysql,Php,Mysql,我的桌子上有一百万条记录。 我只想提取最后200行中计数器最多的5行 我用这个,但我认为这是两次选择。。这可能不适合最佳性能塑料建议 "select * from ( select sid,title,catid,counter from table_stories where catid=1 order by sid desc limit 200 ) astemps order by counter desc l
"select * from (
select sid,title,catid,counter from table_stories
where catid=1 order by sid desc limit 200
) astemps order by counter desc limit 5"
非常感谢。。
关于这是您的问题:
select *
from (select sid, title, catid, counter
from table_stories
where catid = 1
order by sid desc
limit 200
) astemps
order by counter desc
limit 5;
为了获得最佳性能,您需要在
表\u故事(catid,sid desc)
上建立索引。您可以将标题
和计数器
放入索引,但它们不会有多大帮助。不幸的是,MySQL可能没有利用的降序键来代替排序。我觉得查询很好。要获得良好的性能,请确保在这些列上有索引。使用EXPLAIN[query]
查看MySQL是否使用索引。