SQL 异步多个查询合并结果 -- 最大结果(顶部 XYZ) [英] SQL Async Multiple Queries Combine Results -- Maximum Results (Top XYZ)

查看:29
本文介绍了SQL 异步多个查询合并结果 -- 最大结果(顶部 XYZ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个搜索功能,可以同时运行多个查询.每个查询的前 1000 个结果写入一个表.(这些运行异步——我只是省略了我用来执行此操作的代码)

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 the code that I am using to do that)

Insert into Results
Select Top 1000 Text from A where Contains(Text,'"searchString"')

Insert into Results
Select Top 1000 Text from B where Contains(Text,'"searchString"')

Insert into Results
Select Top 1000 Text from C where Contains(Text,'"searchString"')

然后,我从该表中选择前 1000 个结果.

Then, I select the top 1000 results from that table.

Select Top 1000 * from Results

是否有一种好方法可以在流程的早期有效检查是否已有 1000 个结果,如果有,则取消其他查询并尽快选择 1000 个结果.

Would there be a good way to efficiently check, at any point earlier in the process, if there are already 1000 results, and, if there are, cancelling the other queries and selecting the 1000 results ASAP.

推荐答案

以下内容可能会为您提供一个计划,该计划可实现您在找到第 1,000 个行后不处理任何行的预期结果.

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 

这篇关于SQL 异步多个查询合并结果 -- 最大结果(顶部 XYZ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆