我认为我有一个SQL查询应该是确定性的。我一直得到不确定的结果。我检查了计划,发现优化器正在使用多处理器。如果我将max_query_tasks设置为2或更小,那么它将成为确定性的。下面是创建测试表和测试查询的代码片段,以演示该问题。是的,我知道这些表格并不是最好的。我画了这个来帮助说明问题。
DROP TABLE IF EXISTS my_build_ids;
DROP TABLE IF EXISTS mod_dates;
--create the tables
CREATE TABLE my_build_ids (
"build_id" INTEGER NOT NULL DEFAULT AUTOINCREMENT UNIQUE,
"build_customer" VARCHAR(30) NOT NULL,
"build_bom_id" VARCHAR(30) NULL,
PRIMARY KEY ( "build_id" ASC )
);
CREATE TABLE mod_dates(
build_id INTEGER,
mod_date DATETIME
);
--add the data
INSERT INTO my_build_ids (build_id, build_customer, build_bom_id) VALUES
(1, 'customer a', 'bom #1b'),(2, 'customer a', 'bom #1a'),
(4, 'customer b', 'bom #2b'),(5, 'customer b', 'bom #2a')
;
BEGIN
DECLARE currentIdx INT;
SET currentIdx = 1;
PRINT(currentIdx);
WHILE currentIdx <= 10000 LOOP
INSERT INTO mod_dates (build_id, mod_date) VALUES (currentIdx % 10, DATEADD(day, (ABS(RAND()) * 500), '2015-01-01'));
SET currentIdx = currentIdx + 1
END LOOP
END;
INSERT INTO mod_dates (build_id, mod_date) VALUES (1, '2999-01-01'); --this should be returned as the max
--now get the max mod date
SELECT max_mod_date FROM
(SELECT DISTINCT
FIRST_VALUE(build_id ) OVER (PARTITION BY build_customer ORDER BY build_bom_id DESC ) AS build_id
FROM my_build_ids
) build_ids
LEFT JOIN
(SELECT build_id, MAX(mod_date) As max_mod_date
FROM mod_dates
GROUP BY build_id
) TimeInPhase
ON build_ids.build_id = TimeInPhase.build_id
WHERE build_ids.build_id = 1
OPTION (max_query_tasks=2) --setting this to 2 or less makes this a deterministic query
;