UPDATE : For anyone reading this , as @dnoeth stated below indexing makes a huge difference when it comes to query time. In some cases it cut row scans down to 1 row. You just have to pick the flavor of indexing that suits you best . Clustered vs Non Clustered
Index example:
E.g: create index ix_descr on my_db.media (description asc)
I have a query that is taking a long time to run, it has has quite a number of functions in the where clause. What is the best approach to optimizing my query ?
Moving functions out of where clause and replacing them with joins,etc ?
I've given a few simple examples below, FIG 2 and 3 are my optimatization attempts, let me know your thoughts.
Note* these aren't the actual functions I am using, I use dbo.DecryptBlob(m.mediaId) etc. I chose to use the functions below to make the question easier to grasp.
FIG 1.)
SELECT
description, id
FROM
my_db.media
where
length(description) = 10
and description like "S%"
and trim(left(m.description,2)) = 'St'
POSSIBLE OPTIMIZATIONS
FIG 2.) (Using join and group by)
SELECT
m.description, m.mediaId
FROM
my_db.media m
inner JOIN
my_db.media m1 ON length(m.description) = 10
and m.description like "S%"
and trim(left(m.description,2)) = 'St'
group by m.mediaId
FIG 3.) (Using join and distinct)
SELECT distinct
m.description, m.mediaId
FROM
my_db.media m
inner JOIN
my_db.media m1 ON length(m.description) = 10
and m.description like "S%"
and trim(left(m.description,2)) = 'St'