But I Need To Compare Two Date Columns In Different Tables

A Certain Ratio

I have a lot to say about this demo on SQL Server 2019, which I’ll get to in a future post.

For now, I’m going to concentrate on ways to make this situation suck a little less for you wherever you are.

Let’s talk.

Pinky Blue

Let’s get a couple indexes going:

And look at a maybe kinda sorta stupid query.

We wanna find questions where a comment was left a year after they were posted, and the comment was upvoted.


What We Know

From yesterday’s post, we know that even if we put our date columns first in the index, we wouldn’t have anything to seek to.

Unlike yesterday’s post, these columns are in two different tables. We can’t make a good computed column to calculate that.

The indexes that I created help us focus on the SARGable parts of the where clause and the join conditions.

That query takes about 2 seconds.


Switch Hitter

You might be tempted to try something like this, but it won’t turn out much better unless you change your indexes.

Moving CreationDate to the second column helps the first query quite a bit.

Chock full of Jacques


You could try an indexed view here, too.

Mexican Ham Radio

But, you know. That might be overkill.


Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *