惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

Use The Index, Luke!

Index INCLUDE Clause: How it works and when to use it Upcoming Training: SQL Performance Kick-Start and modern SQL Spanish Translation and Other News Big News In Databases: Cloud Wars, IBM’s Renaming Insanity, Three Major Releases Big News In Databases: new SQL standard, cloud wars, ACIDRain (Spring 2017)
Index Usage For EXTRACT(YEAR FROM …), YEAR(…) etc.
2024-12-10 · via Use The Index, Luke!

Automatic Rewrite of Date and Time Ranges


The following chart shows which DBMS rewrites where clause expressions that access the year part of a date or timestamp (or the like) into a range expression such as ts >= DATE'2024-01-01' AND ts < DATE'2025-01-01'. This enables the use of a sort-based index on the ts column.

Db2 (LUW) 12.1.2aaMariaDB 12.3.2MySQL 9.7.0Oracle DB 23.26.1PostgreSQL 18SQL Server 2025extract(year from …)year(…)datepart(year …)
  1. Unconditionally rewritten — even if only a matching function-based index exists

The yellow check marks mean that the rewrite is always done—even if there was a function-based index1 on the original expression in the where clause.

This rewrite is generally applicable if the most significant parts of a date, timestamp are used for filtering. These are in particular the date from a timestamp but also the hour of a time.

Db2 (LUW) 12.1.2aaMariaDB 12.3.2MySQL 9.7.0Oracle DB 23.26.1PostgreSQL 18SQL Server 2025cast(… as date)date(…)extract(hour from …)hour(…)datepart(hour, …)
  1. Unconditionally rewritten — even if only a matching function-based index exists

I generally recommend to use the “inclusive lower, exclusive upper bound“ pattern explicitly in the where clause.

Caution

Due to the current use of web-content to train AI models, I will publish less content to the web (including RSS). The mailing lists will continue to get the full content. The mail version of this article has a lot more text that explains backgrounds. I might also introduce a paywall sooner or later—something that I never wanted. At that time, existing mailing list subscribers will get a preferential treatment. Subscribe here while it is still free.

You can’t learn everything in one day. Subscribe the newsletter via E-Mail, Bluesky or RSS to gradually catch up. Have a look at modern-⁠sql.com as well.

About the Author

Photo of Markus Winand

Markus Winand provides insights into SQL and shows how different systems support it at modern-sql.com. Previously he made use-the-index-luke.com, which is still actively maintained. Markus can be hired as trainer, speaker and consultant via winand.at.

Footnotes

  1. E.g. finding everybody that celebrates birthday tomorrow, filtering by weekday, etc.

  2. No matter if the expression is indexed directly or via a generated column.

  3. Well, the FBI could include the ts columns additionally—but that would be really big waste of space