我有一个复杂的查询需要从总共4个表中的字段。我有一个内部联接语句,它有一个OR
子句,这大大减缓了查询的速度。如何摆脱内部连接OR条件
这是我的查询:
SELECT
pending_corrections.sightinguid AS 'pending_corrections_sightinguid',
vehicle_ownership.id AS 'fk_vehicle_owner',
@bill_id AS 'fk_bills',
@nullValue AS 'fk_final_sightings_sightinguid',
TRIM(pending_corrections.corrected_plate) AS 'vrn',
pending_corrections.seenDate AS 'seen_date',
cameras.in_out AS 'in_out',
vehicle_vrn.fk_sysno AS 'fk_sysno',
cameras.zone AS 'fk_zones',
'0' AS 'auto_generated'
FROM
(pending_corrections
INNER JOIN cameras ON pending_corrections.camerauid = cameras.camera_id)
INNER JOIN
vehicle_vrn ON (pending_corrections.corrected_plate = vehicle_vrn.vrn500
OR pending_corrections.corrected_plate = vehicle_vrn.vrnno)
INNER JOIN
vehicle_ownership ON vehicle_vrn.fk_sysno = vehicle_ownership.fk_sysno
WHERE
pending_corrections.corrected_plate <> ''
AND pending_corrections.corrected_plate IS NOT NULL
AND pending_corrections.unable_to_correct <> '1'
AND pending_corrections.seenDate >= @dateFrom
AND pending_corrections.seenDate <= @dateTo
AND (cameras.in_out = 1 OR cameras.in_out = 0)
AND cameras.zone IN (SELECT
zone_number
FROM
zones
WHERE
fk_site = @siteId)
AND seenDate >= vehicle_vrn.vrn_start_date
AND (seenDate <= vehicle_vrn.vrn_end_date
OR vehicle_vrn.vrn_end_date IS NULL
OR vehicle_vrn.vrn_end_date = '0001-01-01 00:00:00')
AND seenDate >= vehicle_ownership.ownership_start_date
AND (seenDate <= vehicle_ownership.ownership_end_date
OR vehicle_ownership.ownership_end_date IS NULL
OR vehicle_ownership.ownership_end_date = '0001-01-01 00:00:00')
ORDER BY pending_corrections.corrected_plate , pending_corrections.seenDate ASC;
我怎样才能实现的加入一个相同的效果,但没有OR
? OR
子句的原因是因为pending_corrections.corrected_plate
值必须与vehicle_vrn
表中的vrn500
或vrnno
列匹配。
这是您的最后一个问题的副本,然后您在发布答案后更改它。来吧。 – Paparazzi
不是@Frisbee - 这个问题更具体,因为它只处理内部连接的OR部分,而不是整个查询本身。 –
从复制品“我怎样才能达到相同的效果,但在一个连接中没有OR?” – Paparazzi