SQL Async Multiple Queries Combine Results — Maximum Results (Top XYZ)

后端 未结 4 1549
忘了有多久
忘了有多久 2021-01-26 08:47

I have a search function set up, where I run multiple queries simultaneously. The top 1000 results of each query are written to a table. (These run async--I am just leaving out

相关标签:
4条回答
  • 2021-01-26 08:55
    insert into #tt2 
    SELECT TOP 1000 [sID]
    FROM [docSVsys]
    where (select count(*) from #tt2) < 1000
    

    But I would still use .NET and TPL.

    This will still process all tables but it should process 0 rows once it gets to 1000

    0 讨论(0)
  • 2021-01-26 08:58

    The following SQL limits the number of rows to 4 from any subsequent query

    SET ROWCOUNT 4

    SET @Rows = @@ROWCOUNT

    Get the number of rows form the preceeding select

    so something like

    SELECT TOP etc...

    SET @Rows = @@ROWCOUNT

    SET ROWCOUNT 1000 - @Rows

    Will probably error if @row goes below zero

    but you an trap this with IF and goto PROC_LABLE

    0 讨论(0)
  • 2021-01-26 09:10

    The following will likely give you a plan that achieves your desired result of not processing any rows after the 1,000th one has been found.

    WITH CTE
         AS (SELECT Text
             FROM   A
             WHERE  CONTAINS(Text, '"searchString"')
             UNION ALL
             SELECT Text
             FROM   B
             WHERE  CONTAINS(Text, '"searchString"')
             UNION ALL
             SELECT Text
             FROM   C
             WHERE  CONTAINS(Text, '"searchString"'))
    INSERT INTO Results
    SELECT TOP 1000 Text
    FROM   CTE 
    
    0 讨论(0)
  • 2021-01-26 09:18

    If you setup your result table with an identity column you can achieve your goal using this query (let 'counter' be that column. don't forget to put an index on it)

    declare @remaining int
    select @remaining = 1000 - (max(counter) - min(counter) + 1) from result
    if @result>0
      insert into Result (Text) select top (@remaining) Text from MyTable
    

    Also, if you have a list of table names, you can use a while loop and exit if @result is 0.

    0 讨论(0)
提交回复
热议问题