使用工会

时间:2015-08-17 14:52:36

标签: sql sql-server azure

我尝试在临时表中插入数据,但我无法使其工作。

我尝试从每个选择中选择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

但结果并不相同。这就是我得到的:

enter image description here enter image description here

左图是来自第一个sql。第二张图像显示正确的结果。但是,第二个SQL需要7,8s,第一个SQL只需0.7s。

那么,我在第一个sql中做错了什么?我不应该在列表的开头看到相同的结果吗? 我使用Azure Sql

2 个答案:

答案 0 :(得分:2)

对于4000行,在仅检索到4个表中的每个表的前1000个后,您将对4000行进行排序,因此在第1个查询中,返回每个表的前1000行,没有排序,然后在该4000行结果上执行排序。在第二个查询中,返回所有结果行,然后执行排序,以仅返回前1000行。

这解释了输出的差异和性能的差异。

答案 1 :(得分:1)

在第一个查询中,联合的每个部分都没有排序。如果您想要订购它们,您必须将它们放在如下的子查询中:

INSERT INTO #Data (user_id, created_at)
select top 1000 * 
from
(
    (
    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
    order by created_at DESC
    )
    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
    order by created_at DESC
    )
    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
    order by created_at DESC
    )
    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 created_at DESC
    )
) Data
ORDER BY created_at DESC

此查询应返回与第二个查询相同的结果。