我有一个SQL脚本,它针对各种条件查看30-40个“代码”,并查找来自大型2表(在PK上加入)源大约3000万行的匹配记录。脚本完成后,我最终得到了一个近800多万的查找表。记录(25 mil * ~35代码)。
每个代码都有自己的select语句。他们中的大多数人只是使用不同的条件来查看相同的来源。一些代码的查询加入另一个源表以获取lookup_value。
例如:
INSERT INTO LookupTable
SELECT 01 AS code, t1.lookup_value, t1.PK, t2.PK
FROM Table1 t1
JOIN Table2 t2 ON t1.FK = t2.PK
WHERE <code 1 condition> = true
INSERT INTO LookupTable
SELECT 02 AS code, t1.lookup_value, t1.PK, t2.PK
FROM Table1 t1
JOIN Table2 t2 ON t1.FK = t2.PK
WHERE <code 2 condition> = true
INSERT INTO LookupTable
SELECT 03 AS code, CASE WHEN t1.lookup_value IN 'A1','B1','C1' THEN 1 ELSE 0 END, t1.PK, t2.PK
FROM Table1 t1
JOIN Table2 t2 ON t1.FK = t2.PK
WHERE <code 3 condition> = true
INSERT INTO LookupTable
SELECT 04 AS code, CASE WHEN t3.lookup_value IN 'A1','B1','C1' THEN 1 ELSE 0 END, t1.PK, t2.PK
FROM Table1 t1
JOIN Table2 t2 ON t1.FK = t2.PK
JOIN Table3 t3 ON t1.FK = t3.PK
WHERE <code 4 condition> = true
-- ... <continues 30 more times>
(对于某些代码,还有多个查询语句,临时表等,但如果我能处理上述内容,我可以自己解决这些问题。)
现在,脚本大约需要6个小时才能在强大的服务器上运行。这是一个预先存在的系统,我没有选择重新设计它的工作方式;庞大的查找表是必需的。
我不禁想到通过相同的30密耳扫描。行,30多次是一种低效的方法!对于更好的性能,或者至少是更易于管理的代码的想法?我一直在考虑创建.Net CLR TVF或索引视图,但我对新想法持开放态度!
答案 0 :(得分:1)
修改更新后的答案
使用common table expression将您的3000万条记录表预先解析为临时结果集,然后从中进行多次插入选择......
WITH Table_CTE (code, lookup_value, pk1, pk2)
AS
-- Define the CTE query.
(
SELECT
CASE WHEN <code 1 condition> THEN 01
CASE WHEN <code 2 condition> THEN 02
CASE WHEN <code 3 condition> THEN 03
CASE WHEN <code 4 condition> THEN 04
...
END code, t1.lookup_value, t1.PK as pk1, t2.PK as pk2
FROM Table1 t1
JOIN Table2 t2 ON t1.FK = t2.PK
JOIN Table3 t3 ON t1.FK = t3.PK
)
INSERT INTO LookupTable
SELECT code, lookup_value, pk1, pk2
FROM Table_CTE
WHERE code = '01'
INSERT INTO LookupTable
SELECT code, lookup_value, pk1, pk2
FROM Table_CTE
WHERE code = '02'
INSERT INTO LookupTable
SELECT code, lookup_value, pk1, pk2
FROM Table_CTE
WHERE code = '03'
答案 1 :(得分:0)
我认为任何条件只能满足一次。
然后这是一个简单的(小心,未经测试!):
INSERT INTO LookupTable
SELECT
code = c.val
t1.lookup_value,
t1.PK,
t2.PK
FROM
Table1 t1
JOIN Table2 t2
ON t1.FK = t2.PK
CROSS APPLY (
SELECT val = CASE
WHEN <code 1 condition> THEN 01
WHEN <code 2 condition> THEN 02
...
WHEN <code 34 condition> THEN 34
ELSE 00 END
) v
WHERE c.val != 99
如果我的假设!=真,那么我相信你(或某人)应该重新考虑设计。