Skip to content
Advertisement

Does adding the guaranteed where condition on sql can improve performance?

Let’s say there’s a table about logging website access.

I want to filter the uber.com rows.

I can guarantee that uber.com access log must exist later than 2009-03-01T00:00:00.000Z.

Is there any performance difference between SQL-A and SQL-B?

Advertisement

Answer

In general, no.

I can think of two situations where there would be a performance impact:

  • There is an index starting with logged_at and no index with domain as the first column.
  • The table is partitioned by logged_at.

You did not mention anything in the question to suggest that either of these might be the case.

For a regular query, you want an index on (domain) or (domain, logged_at). Both queries would use both indexes and have very similar performance.

I should note that the second query incurs a small amount of overhead for the unnecessary date comparison. However, that is likely to be close to unmeasurable if you have a large amount of data.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement