This request:
SELECT COUNT(*)
FROM foo
WHERE expires_at < NOW()
can be satisfied only with the index, not referring to the table itself. You can see it from using indexthe plan.
This request:
SELECT COUNT(*)
FROM foo
WHERE expires_at < NOW()
AND some_id <> 5
you need to look in the table to find the value some_id.
Since table searches are quite expensive, it is more efficient to use table scans and filter records.
expires_at, some_id, , , expires_at, some_id.
SQL Server , included fields.
CREATE INDEX ix_foo_expires__someid ON foo (expires_at) INCLUDE (some_id)
expires_at, some_id ( ).
MySQL, , .