I have a query like
select C.customerId, C.firstName, C.LastName, R.IsActive from
Customer as C
Inner join Rider as R ON R.customerId = C.customerid
where R.rideid = 'xyz' and
R.rideareacode = 'abc' and
R.isActive = 1
This is taking too much like 90 seconds or more. But if I remove the R.IsActive = 1 condition query is taking only a second.
IsActive has only two values 1 or 0, also the Customer and Rider table are big (80k rows)
I tried indexing the IsActive column of my Rider table but still the query is taking too much, I also tried indexing (rideid, rideareacode, isActive) still it is taking too long.
Index_keys for Rider table :
IsActive (nonclustered)
rideid, rideareacode, isActive, userid (nonclustered, unique)
riderid (nonclustered, unique, primary key)
There is no indexing for the customers table.
2
Answers
R
needsINDEX(rideid, rideareacode, isActive)
, with the columns in any order.Even better is this "covering index":
Besides modifying the indexes, you can also add the conditions to the ON clause: