mysql - 如何优化 ORDER 条件以防止文件排序?

标签 mysql counter-cache

当我想在 MySQL 中按条件排序查询时,如何避免文件排序?

ORDER BY (videos_count > 0) DESC, name

缓存 has_videos bool 列以及计数会更好吗?

最佳答案

如何将查询分为两部分,首先是至少包含一个视频的查询,然后(仅在需要时)其余部分:

select *  from video_table  
where video_count > 0
order by name

如果您需要超越这些:

select *  from video_table  
where video_count = 0
order by name

关于mysql - 如何优化 ORDER 条件以防止文件排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082194/

相关文章:

ruby-on-rails - Rails 6.1.4 counter_cache 问题

ruby-on-rails - Rails 3 和 Rspec : counter cache column being updated to 2 when expected 1

ruby-on-rails - 将计数器缓存添加到 rails 中的自连接表

ruby-on-rails - 访问 ruby​​ 计数器缓存

MySQL-PHP 从一列连接更多值

php - 多行id相同时如何删除一行

mysql - 意外操作数应包含 1 列错误

mysql - 左外部联接不适用于具有重复 ID 的列

Mysql查询帮助显示小于给定日期的结果

CakePHP 3 : CounterCache with BelongsToMany