Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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
在sql中搜索前300行_Sql_Sql Server_Search - Fatal编程技术网

在sql中搜索前300行

在sql中搜索前300行,sql,sql-server,search,Sql,Sql Server,Search,我正在使用MS SQL Server Management Studio R2 假设我有一个记录数无限的tableX表, 该表有一列colX,整个表中只有2条记录的colX不为null 查询应该是什么样子的 我曾经 select top 10 * from tableX where colX isnot null 但是执行这个查询花了很长时间 有没有办法只搜索前300行? 谢谢 有没有办法只搜索前300行 是的,有: select top 300 * from tableX where col

我正在使用MS SQL Server Management Studio R2

假设我有一个记录数无限的tableX表, 该表有一列colX,整个表中只有2条记录的colX不为null

查询应该是什么样子的

我曾经

select top 10 * from tableX where colX isnot null
但是执行这个查询花了很长时间

有没有办法只搜索前300行? 谢谢

有没有办法只搜索前300行

是的,有:

select top 300 * from tableX where colX isnot null order by id asc

这假设您在tableX上有一个名为id的列,它是一个标识列。基本上,我们可能需要更多信息:-

谢谢你的帮助,我用一种巧妙的方法解决了这个问题

我确实做了一张新桌子

Declare tempTable table(...columns...)
然后

insert into tempTable (select top 300* from tableX)
此后,

select * from tempTable where colX is not null

这些对我都不管用。我曾经

query.setMaxResults(300)

它与SQL中的LIMIT相同,但在Java代码中对我有效。

您是否已经在colX上有索引?看起来像一个索引。搜索整个结果集的速度很慢,因此您会询问如何仅搜索300条记录。你应该问为什么最初的搜索开始时会变慢@马克班尼斯特的建议将是一个良好的开端。这可能是次要的,但如果你能更准确地描述,可能会有所帮助。否则,如果我们从字面上理解你的话,我会说永远与无限记录计数有很好的相关性。这让问题一直在发生,我的朋友,问题似乎是SQL获取所有记录,然后它过滤,我只想搜索前300条记录。非常感谢:好的,但您的解决方案不需要使用临时表:select*from tableX,其中colX不为null,id位于select top 300 id from tableX中。