Reputation: 36573
What kind of query hints can I use to improve the performance of an OR query.
Individually the two queries below run instantly and return 0 rows.
SELECT * FROM AuditInfo ai
WHERE (ai.AppointmentIdForChange = 60231)
and ai.objectname = 'dbo.Appointments'
SELECT * FROM AuditInfo ai
WHERE (ai.AppointmentIdForKey = 60231)
and ai.objectname = 'dbo.Appointments'
AuditInfo is a view.
However the following query runs forever (10+ minutes)
SELECT * FROM AuditInfo ai
WHERE (ai.AppointmentIdForKey= 60231 OR ai.AppointmentIdForChange = 60231)
and ai.objectname = 'dbo.Appointments'
How can I improve this? Are there query hints I can use?
In the estimated execution plan, I can see the individual queries use Key Lookups, whereas the query with the OR statement uses Index Seeks (which takes 74% of the cost).
UPDATE:
The AuditInfo view is defined as follows:
ALTER VIEW AuditInfo
AS
SELECT
ae.Id
,ae.AuditDateTime AS DateTime
,ae.ChangeTypeId
,ae.ObjectName
,aek.KeyName
,aek.KeyValue
,aek.KeyValueNumeric
,ae.UserId
,ae.HostName
,ae.ServerName
,aec.OldValue
,(SELECT TOP 1 aecNewValue.OldValue AS Value
FROM dbo.AuditEntries aeNewValue
JOIN dbo.AuditEntryKeyValues aekNewValue ON aeNewValue.Id = aekNewValue.AuditEntryId AND aek.KeyName = aekNewValue.KeyName AND aek.KeyValueNumeric = aekNewValue.KeyValueNumeric
JOIN dbo.AuditEntryChanges aecNewValue ON aeNewValue.Id = aecNewValue.AuditEntryId AND aec.ColumnName = aecNewValue.ColumnName
WHERE aeNewValue.ChangeTypeId IN (0, 1) AND aeNewValue.AuditDateTime > ae.AuditDateTime AND aeNewValue.ObjectName = ae.ObjectName
ORDER BY aeNewValue.AuditDateTime
) AS NewValue
,appointmentForKey.AppointmentId AS AppointmentIdForKey, appointmentForChange.AppointmentId AS AppointmentIdForChange
FROM dbo.AuditEntries ae
JOIN dbo.AuditEntryKeyValues aek on ae.Id = aek.AuditEntryId
LEFT JOIN dbo.AuditEntryChanges aec on ae.Id = aec.AuditEntryId AND aec.ColumnName IN ('PatientId', 'AppointmentId')
-- Get AppointmentId
LEFT JOIN dbo.Appointments appointmentForKey ON aek.KeyName = 'AppointmentId' AND aek.KeyValueNumeric = appointmentForKey.AppointmentId
LEFT JOIN dbo.Appointments appointmentForChange ON aec.ColumnName = 'AppointmentId' AND aec.OldValueNumeric = appointmentForChange.AppointmentId
GROUP BY ae.Id, ae.AuditDateTime, ae.UserId, ae.ChangeTypeId, ae.ObjectName, ae.HostName, ae.ServerName, aek.KeyName, aek.KeyValue, aek.KeyValueNumeric, aec.OldValueNumeric, aec.OldValue, aec.ColumnName
,appointmentForKey.AppointmentId, appointmentForChange.AppointmentId
Upvotes: 2
Views: 323
Reputation: 45096
I know you ask for query hints but you could just run it as two queries with a union
SELECT * FROM AuditInfo ai
WHERE (ai.AppointmentIdForChange = 60231)
and ai.objectname = 'dbo.Appointments'
UNION
SELECT * FROM AuditInfo ai
WHERE (ai.AppointmentIdForKey = 60231)
and ai.objectname = 'dbo.Appointments'
As for will it perform tomorrow.
I will take a UNION over a JOIN or an OR for stability.
With a Union SQL does them one at a time and the query optimizer has less chance of getting stupid.
Upvotes: 1