Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql 加入元朗';如果不这样做,子查询就糟透了,那又怎样?_Tsql_Sql Server 2008_Query Optimization - Fatal编程技术网

Tsql 加入元朗';如果不这样做,子查询就糟透了,那又怎样?

Tsql 加入元朗';如果不这样做,子查询就糟透了,那又怎样?,tsql,sql-server-2008,query-optimization,Tsql,Sql Server 2008,Query Optimization,首先,很抱歉这个非描述性的标题,我只是太急了,所以我想不出更好的 第二: 我有一部分数据库,如下图所示: 我在系统上有贡献者,每个贡献者都写了很多源代码,一个源代码可以有很多工作贡献者。用户可以订阅任意数量的贡献者和任意数量的源。现在,我想做的只是检索特定用户的所有文章。这些文章要么来自于贡献者,要么来自于用户订阅的来源。为了方便起见,当用户订阅源时,我只需将所有源参与者复制到users\u contributors表中。一个棘手的问题是,当我检索用户的文章时,我检索了他和他的贡献者所写的所有文

首先,很抱歉这个非描述性的标题,我只是太急了,所以我想不出更好的
第二:
我有一部分数据库,如下图所示:
我在系统上有贡献者,每个贡献者都写了很多源代码,一个源代码可以有很多工作贡献者。用户可以订阅任意数量的贡献者和任意数量的源。现在,我想做的只是检索特定用户的所有文章。这些文章要么来自于贡献者,要么来自于用户订阅的来源。为了方便起见,当用户订阅源时,我只需将所有源参与者复制到users\u contributors表中。一个棘手的问题是,当我检索用户的文章时,我检索了他和他的贡献者所写的所有文章,以及在他所关注的源代码中发布的所有文章,这些文章在系统中没有有效的贡献者。(即contributorID为空)
我创建了以下查询:

 Select Articles.ArticleID, Articles.ContributorId, Contributors.Name, 
    Sources.Name, Articles.ArticleTitle
From Articles 
            Inner Join Contributors On Articles.ContributorId = Contributors.ContributorId
            Inner Join Sources On Articles.SourceId = Sources.SourceID          
Where Articles.ContributorId in (
    Select ContributorId from Users_Contributors
    Where UserID = 3
    )
    OR (

        Articles.SourceId in (
            Select SourceId from Users_Sources
            Where UserID = 3
            )

            and 
            Articles.ContributorId is null
    )
上述查询的问题是,它不会返回任何contributorID为null的文章。我知道这是因为contributors表上的连接。在这种情况下我该怎么办

>强>应考虑非正规化吗?< /强>
  • 此查询的每个表上要索引的prober字段是什么 要快速运行(返回行集) 大约10000人?
  • 我需要在这个查询上支持分页,将使用“With{}”子句吗 适合我,还是我应该 考虑另一个策略?< /强>


    提前谢谢
    Ps:我正在使用SQL Server 2008


  • 你为什么不加入呢

    Inner Join Contributors On Articles.ContributorId = Contributors.ContributorID
    
    外部连接

    Left Join Contributors On Articles.ContributorId = Contributors.ContributorID
    
    这将导致它返回所有项目,无论是否有匹配的SourceID(包括ContributorID为null的情况)

    如果需要分页,请使用此选项(从
    80
    100
    )获取页面:


    哎呀,我打错了。。这是在文章中加入贡献者。贡献者ID=贡献者。贡献者ID。。我编辑了,请再次检查!我需要加入贡献者和源,以便获得贡献者名称和源名称。所以我不确定工会是否适合我。以下是我的具体需求:我需要按Articles.OrderDate订购退货记录,并支持对这些记录进行分页。另外,在联合的第二部分,我必须加入sources表以获得SourceName(因为它不会为null)。@7alwagy:联合应该可以工作。它在sql server 2008中受支持。看。@7alwagy:你用什么画图表?
    SELECT  a.*, s.Name AS SourceName, NULL AS ContributorName
    FROM    User_Sources us
    JOIN    Articles a
    ON      a.SourceID = us.SourceID
    JOIN    Source s
    ON      s.SourceID = us.SourceID
    WHERE   us.UserID = 3
            AND a.ContributorID IS NULL
    UNION
    SELECT  a.*, s.Name AS SourceName, c.Name AS ContributorName
    FROM    User_Contributor uc
    JOIN    Articles a
    ON      a.ContributorID = uc.ContributorID
    JOIN    Contirbutors c
    ON      c.ContributorID = uc.ContributorID
    JOIN    Sources s
    ON      s.SourceID = a.SourceID
    WHERE   uc.UserID = 3
    
    WITH    q AS (
            SELECT  TOP 100 
                    a.*, s.Name AS SourceName, NULL AS ContributorName
            FROM    User_Sources us
            JOIN    Articles a
            ON      a.SourceID = us.SourceID
            JOIN    Source s
            ON      s.SourceID = us.SourceID
            WHERE   us.UserID = 3
                    AND a.ContributorID IS NULL
            ORDER BY
                    OrderDate
            UNION
            SELECT  TOP 100
                    a.*, s.Name AS SourceName, c.Name AS ContributorName
            FROM    User_Contributor uc
            JOIN    Articles a
            ON      a.ContributorID = uc.ContributorID
            JOIN    Contirbutors c
            ON      c.ContributorID = uc.ContributorID
            JOIN    Sources s
            ON      s.SourceID = a.SourceID
            WHERE   uc.UserID = 3
            ORDER BY
                    OrderDate
            ),
            page AS
            (
            SELECT  TOP 100 *, ROW_NUMBER() OVER (ORDER BY OrderDate) AS rn
            FROM    q
            )
    SELECT  *
    FROM    page
    WHERE   rn >= 80