Menu

📰
0

Laravel whereDate() Silently Kills Your Index

DEV Community: laravel·Ivan Mykhavko·about 1 month ago
#O24M5AxW
#dev#class#code#highlight#created_at#article
Reading 0:00
15s threshold

whereDate('created_at', $date) looks clean, but on a big table it quietly drops your index and does a full scan. The Problem Say you want notifications created on a specific day. The obvious call: UserNotification :: query () -> whereDate ( 'created_at' , '2026-04-23' ) -> get (); Laravel generates this SQL: SELECT * FROM user_notifications WHERE DATE ( created_at ) = '2026-04-23' See DATE(created_at) ? MySQL has to compute that function for every row before comparing. Your created_at index is useless, EXPLAIN shows a full table scan: +----+-------------+--------------------+------+---------+ | id | select_type | table | type | rows | +----+-------------+--------------------+------+---------+ | 1 | SIMPLE | user_notifications | ALL | 5000000 | +----+-------------+--------------------+------+---------+ On 5k rows you won't notice. On 5M rows you will.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More