栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MySQL性能优化:按日期时间字段排序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQL性能优化:按日期时间字段排序

postings (is_active, post_date)
(按此顺序)上创建一个复合索引。

它将用于按进行过滤

is_active
和排序
post_date

MySQL
应该在中显示
REF
对此索引的访问方法
EXPLAIN EXTENDED

请注意,您在上有一个

RANGE
过滤条件
user_offtopic_count
,这就是为什么在过滤和按其他字段排序时都不能在该字段上使用索引的原因。

根据您选择的程度

user_offtopic_count
(即,满足多少行
user_offtopic_count <10
),创建索引
user_offtopic_count
并对post_dates进行排序可能会更有用。

为此,在上创建一个复合索引,

postings (is_active,user_offtopic_count)
并确保
RANGE
使用对该索引的访问方法。

哪个索引会更快取决于您的数据分布。创建两个索引,

FORCE
然后看看哪个更快:

CREATE INDEX ix_active_offtopic ON postings (is_active, user_offtopic_count);CREATE INDEX ix_active_date ON postings (is_active, post_date);SELECt     `postings`.`id`,     UNIX_TIMESTAMP(postings.post_date) as post_date,     `postings`.`link`,     `postings`.`title`,     `postings`.`author`,     `postings`.`excerpt`,     `postings`.`long_excerpt`,     `feeds`.`title` AS feed_title,     `feeds`.`website` AS feed_websiteFROM     `postings` FORCE INDEX (ix_active_offtopic)JOIN     `feeds` ON     `feeds`.`id` = `postings`.`feed_id`WHERe     `feeds`.`type` = 1 AND     `postings`.`user_offtopic_count` < 10 AND     `postings`.`is_active` = 1ORDER BY     `postings`.`post_date` descLIMIT     15SELECt     `postings`.`id`,     UNIX_TIMESTAMP(postings.post_date) as post_date,     `postings`.`link`,     `postings`.`title`,     `postings`.`author`,     `postings`.`excerpt`,     `postings`.`long_excerpt`,     `feeds`.`title` AS feed_title,     `feeds`.`website` AS feed_websiteFROM     `postings` FORCE INDEX (ix_active_date)JOIN     `feeds` ON     `feeds`.`id` = `postings`.`feed_id`WHERe     `feeds`.`type` = 1 AND     `postings`.`user_offtopic_count` < 10 AND     `postings`.`is_active` = 1ORDER BY     `postings`.`post_date` descLIMIT     15


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383264.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号