Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL index for deleted_at columns

Is it a best practice or beneficial to add an index on the deleted_at column in a SQL database? Lots of my queries use WHERE deleted_at IS NULL, will adding a deleted_at index speed up my queries or slow them down?

like image 424
ramz15 Avatar asked Nov 02 '22 09:11

ramz15


1 Answers

@Barmar answered this question best:

"Whether you should index it depends on how well it partitions your table. If most rows are not deleted, indexing it won't help very much. Also, if you have other columns that you index, and you test them along with deleted_at, then you'll need a composite index of the other columns with deleted_at to get the benefit."

like image 167
ramz15 Avatar answered Nov 09 '22 17:11

ramz15