如果我运行此查询,则所有[data]。*值都为NULL。
忽略*
,它只是一个测试
SELECT
[map].*,
[data].*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON (([data].level1_id = [map].level1_id) OR ([data].level1_id = 0))
AND (([data].level2_id = [map].level2_id) OR ([data].level2_id = 0))
AND (([data].level3_id = [map].level3_id) OR ([data].level3_id = 0))
AND (([data].level4_id = [map].level4_id) OR ([data].level4_id = 0))
AND (([data].level5_id = [map].level5_id) OR ([data].level5_id = 0))
AND (([data].level6_id = [map].level6_id) OR ([data].level6_id = 0))
AND ([data].metric_id = 22)
WHERE
[map].level1_id = 22
然而,如果我在每个1=1 AND
之前添加[data].levelX_id = 0
,它会正常工作!?!?
SELECT
[map].*,
[data].*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON (([data].level1_id = [map].level1_id) OR (1=1 AND [data].level1_id = 0))
AND (([data].level2_id = [map].level2_id) OR (1=1 AND [data].level2_id = 0))
AND (([data].level3_id = [map].level3_id) OR (1=1 AND [data].level3_id = 0))
AND (([data].level4_id = [map].level4_id) OR (1=1 AND [data].level4_id = 0))
AND (([data].level5_id = [map].level5_id) OR (1=1 AND [data].level5_id = 0))
AND (([data].level6_id = [map].level6_id) OR (1=1 AND [data].level6_id = 0))
AND ([data].metric_id = 22)
WHERE
[map].level1_id = 22
我几乎可以把它作为一种软糖来使它发挥作用,但它现在需要永远运行。 (在6分钟后从低于一秒到'仍在运行'。)
我被束缚了吗?或者SQL CE是它的摇杆?
修改
如果我改变条件:
AND (([data].level6_id = [map].level6_id) OR ([data].level6_id = 0))
到
AND (([data].level6_id = 0) OR ([data].level6_id = [map].level6_id))
然后我得到了不同的行为。
如果我使用IN
代替:
AND ([data].level6_id IN ([map].level6_id,0))
我仍然有不同的行为。
没有感觉到的行为,当然也不是我编码的行为。
答案 0 :(得分:3)
我猜这些可能会慢一点,但我让这两个查询都有效:
SELECT
*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON ([data].level1_id = [map].level1_id OR [data].level1_id*1 = 0)
AND ([data].level2_id = [map].level2_id OR [data].level2_id*1 = 0)
AND ([data].level3_id = [map].level3_id OR [data].level3_id*1 = 0)
AND ([data].level4_id = [map].level4_id OR [data].level4_id*1 = 0)
AND ([data].level5_id = [map].level5_id OR [data].level5_id*1 = 0)
AND ([data].level6_id = [map].level6_id OR [data].level6_id*1 = 0)
;
和
SELECT
*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON ([data].level1_id = [map].level1_id OR cast([data].level1_id as int) = 0)
AND ([data].level2_id = [map].level2_id OR cast([data].level2_id as int) = 0)
AND ([data].level3_id = [map].level3_id OR cast([data].level3_id as int) = 0)
AND ([data].level4_id = [map].level4_id OR cast([data].level4_id as int) = 0)
AND ([data].level5_id = [map].level5_id OR cast([data].level5_id as int) = 0)
AND ([data].level6_id = [map].level6_id OR cast([data].level6_id as int) = 0)
;
编辑使用Compact 4,如果它有所作为。
答案 1 :(得分:1)
我的解决方案是放弃SQL-CE并在客户端内存中加入。
唯一可行的选项是最后一个选项,由于对两个表的完全扫描,实际数据的性能令人震惊。
CREATE TABLE f_normalised_report_data (
level1_id INT,
level2_id INT,
level3_id INT,
level4_id INT,
level5_id INT,
level6_id INT,
metric_id INT,
value MONEY,
PRIMARY KEY (
level1_id,
level2_id,
level3_id,
level4_id,
level5_id,
level6_id,
metric_id
)
)
;
CREATE TABLE f_normalised_report_hierarchy (
level1_id INT,
level2_id INT,
level3_id INT,
level4_id INT,
level5_id INT,
level6_id INT,
PRIMARY KEY (
level1_id,
level2_id,
level3_id,
level4_id,
level5_id,
level6_id
)
)
;
INSERT INTO f_normalised_report_hierarchy SELECT 1, 2, 3, 4, 5, 6;
INSERT INTO f_normalised_report_hierarchy SELECT 1, 2, 3, 4, 5, 7;
INSERT INTO f_normalised_report_data SELECT 1, 2, 3, 0, 5, 6, 22, 999;
INSERT INTO f_normalised_report_data SELECT 1, 2, 3, 0, 5, 7, 22, 911;
SELECT
*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON ([data].level1_id = [map].level1_id OR [data].level1_id = 0)
AND ([data].level2_id = [map].level2_id OR [data].level2_id = 0)
AND ([data].level3_id = [map].level3_id OR [data].level3_id = 0)
AND ([data].level4_id = [map].level4_id OR [data].level4_id = 0)
AND ([data].level5_id = [map].level5_id OR [data].level5_id = 0)
AND ([data].level6_id = [map].level6_id OR [data].level6_id = 0)
;
-- The above query gives me this...
-- 1, 2, 3, 4, 5, 6, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
-- 1, 2, 3, 4, 5, 7, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
SELECT
*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON ([data].level1_id = 0 OR [data].level1_id = [map].level1_id)
AND ([data].level2_id = 0 OR [data].level2_id = [map].level2_id)
AND ([data].level3_id = 0 OR [data].level3_id = [map].level3_id)
AND ([data].level4_id = 0 OR [data].level4_id = [map].level4_id)
AND ([data].level5_id = 0 OR [data].level5_id = [map].level5_id)
AND ([data].level6_id = 0 OR [data].level6_id = [map].level6_id)
;
-- The above query gives me this...
-- 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0, 6, 22, 999
-- 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0, 7, 22, 911
-- 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 0, 6, 22, 999
-- 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 0, 7, 22, 911
SELECT
*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON ([data].level1_id IN ([map].level1_id, 0))
AND ([data].level2_id IN ([map].level2_id, 0))
AND ([data].level3_id IN ([map].level3_id, 0))
AND ([data].level4_id IN ([map].level4_id, 0))
AND ([data].level5_id IN ([map].level5_id, 0))
AND ([data].level6_id IN ([map].level6_id, 0))
;
-- The above query gives me this...
-- 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0, 6, 22, 999
-- 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0, 7, 22, 911
-- 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 0, 6, 22, 999
-- 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 0, 7, 22, 911
SELECT
*
FROM
f_normalised_report_hierarchy AS [map]
LEFT JOIN
f_normalised_report_data AS [data]
ON ([data].level1_id = [map].level1_id OR (1=1 AND [data].level1_id = 0))
AND ([data].level2_id = [map].level2_id OR (1=1 AND [data].level2_id = 0))
AND ([data].level3_id = [map].level3_id OR (1=1 AND [data].level3_id = 0))
AND ([data].level4_id = [map].level4_id OR (1=1 AND [data].level4_id = 0))
AND ([data].level5_id = [map].level5_id OR (1=1 AND [data].level5_id = 0))
AND ([data].level6_id = [map].level6_id OR (1=1 AND [data].level6_id = 0))
;
-- The above query gives me this...
-- 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0, 6, 22, 999
-- 1, 2, 3, 4, 5, 7, 1, 2, 3, 4, 0, 7, 22, 911
--
-- Which is correct, but performance was blown to smitherines.