我尝试在临时表中插入数据,但无法让它工作。
我尝试从每次选择中选择 1000 个项目。
INSERT INTO #Data(user_id, created_at)
SELECT TOP(1000) u.id, c.created_at
FROM comment as c
inner join outfit as o on o.id = c.outfit_id and o.deleted = 0 and o.user_id = 645 AND c.deleted = 0
inner join user as u on u.id = c.user_id and u.is_active = 1
UNION
SELECT TOP(1000) u.id, l.created_at
FROM like as l
inner join outfit as o on o.id = l.outfit_id and o.deleted = 0 and o.user_id = 645
inner join user as u on u.id = l.user_id and u.is_active = 1
UNION
SELECT TOP(1000) u.id, f.created_at
FROM friend_user as f
inner join user as u on u.id = f.user_id and u.is_active = 1
where f.friend_id = 645 AND f.approved = 1
UNION
SELECT TOP(1000) u.id, c.created_at
FROM comment_tagged_user AS T
INNER JOIN comment as c ON c.id = T.comment_id and T.user_id = 645 AND c.deleted = 0
inner join outfit as o on o.id = c.outfit_id and o.deleted = 0
inner join user as u on u.id = c.user_id and u.is_active = 1
ORDER BY o.created_at DESC
现在我尝试选择上面相同 sql 的总共 1000 行。 (我删除了每个 SELECT 上的 TOP 1000)
INSERT INTO #Data(user_id, created_at)
SELECT TOP 1000 * FROM (
SELECT u.id, c.created_at
FROM comment as c
inner join outfit as o on o.id = c.outfit_id and o.deleted = 0 and o.user_id = 645 AND c.deleted = 0
inner join user as u on u.id = c.user_id and u.is_active = 1
UNION
SELECT u.id, l.created_at
FROM like_like as l
inner join outfit as o on o.id = l.outfit_id and o.deleted = 0 and o.user_id = 645
inner join user as u on u.id = l.user_id and u.is_active = 1
UNION
SELECT u.id, f.created_at
FROM friend_user as f
inner join user as u on u.id = f.user_id and u.is_active = 1
where f.friend_id = 645 AND f.approved = 1
UNION
SELECT u.id, c.created_at
FROM comment_tagged_user AS T
INNER JOIN comment as c ON c.id = T.comment_id and T.user_id = 645 AND c.deleted = 0
inner join outfit as o on o.id = c.outfit_id and o.deleted = 0
inner join user as u on u.id = c.user_id and u.is_active = 1
) AS Data ORDER BY created_at DESC
但是结果并不一样。这就是我得到的:
左图来自第一个sql。第二张图显示了正确的结果。但是,第二条SQL需要7.8秒,第一条SQL只需要0.7秒。
那么,我在第一个sql中做错了什么?我不应该在列表的开头看到相同的结果吗? 我使用 Azure Sql
最佳答案
对于 4000 行,您要对 4000 行进行排序,仅检索了 4 个表中每个表的前 1000 行,因此在第一个查询中,返回每个表的前 1000 行(不进行排序),然后对这 4000 行结果执行排序。在第二个查询中,返回所有结果行,然后执行排序,仅返回前 1000 行。
这解释了输出的差异和性能的差异。
关于sql - 使用联合优化 sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053519/