I need to query a Sybase database which has a lot of data in it, and would like to set a limit so the DB stops the query after 10 results.
我需要查询其中包含大量数据的Sybase数据库,并希望设置一个限制,以便DB在10个结果后停止查询。
The key thing is performance, so it would be no use if it searched all results and then then returned the last 10 results.
关键是性能,所以如果它搜索所有结果然后返回最后10个结果就没用了。
Thanks in advance
提前致谢
3 个解决方案
#1
19
I believe you can do a SET ROWCOUNT 10
first, then all queries in this session until a further SET ROWCOUNT
will return no more than 10 rows. As a comment points out, this affects all following queries in the session (not just SELECT
s!) until turned off (by setting to 0) or set differently -- this "global" effect makes it less handy than the typical LIMIT
clause of other engines, which is inherently per-query, but, I don't think you can do anything about that.
我相信您可以首先执行SET ROWCOUNT 10,然后在此会话中执行所有查询,直到另一个SET ROWCOUNT将返回不超过10行。正如评论指出的那样,这会影响会话中的所有后续查询(不仅仅是SELECT!),直到关闭(通过设置为0)或设置不同 - 这种“全局”效果使其不如其他的典型LIMIT子句方便引擎,本质上是每个查询,但是,我认为你不能做任何事情。
#2
39
With Sybase 12.5 and later you can use the top predicate in your select statement. This is a non-ANSI feature which MSSQL has had for quite a while.
使用Sybase 12.5及更高版本,您可以在select语句中使用top谓词。这是一个非ANSI特性,MSSQL已经有很长一段时间了。
select top 10 * from people
You can't use top in subqueries, updates, or deletes, and there is no corresponding 'bottom' clause.
您不能在子查询,更新或删除中使用top,并且没有相应的“bottom”子句。
The advantage of top is you don't have to worry about resetting it. This is especially important if you are using a database connection pool.
top的优点是你不必担心重置它。如果您使用数据库连接池,这一点尤为重要。
#3
-1
You can try adding this clause "fetch first n rows only".
您可以尝试添加此子句“仅获取前n行”。
#1
19
I believe you can do a SET ROWCOUNT 10
first, then all queries in this session until a further SET ROWCOUNT
will return no more than 10 rows. As a comment points out, this affects all following queries in the session (not just SELECT
s!) until turned off (by setting to 0) or set differently -- this "global" effect makes it less handy than the typical LIMIT
clause of other engines, which is inherently per-query, but, I don't think you can do anything about that.
我相信您可以首先执行SET ROWCOUNT 10,然后在此会话中执行所有查询,直到另一个SET ROWCOUNT将返回不超过10行。正如评论指出的那样,这会影响会话中的所有后续查询(不仅仅是SELECT!),直到关闭(通过设置为0)或设置不同 - 这种“全局”效果使其不如其他的典型LIMIT子句方便引擎,本质上是每个查询,但是,我认为你不能做任何事情。
#2
39
With Sybase 12.5 and later you can use the top predicate in your select statement. This is a non-ANSI feature which MSSQL has had for quite a while.
使用Sybase 12.5及更高版本,您可以在select语句中使用top谓词。这是一个非ANSI特性,MSSQL已经有很长一段时间了。
select top 10 * from people
You can't use top in subqueries, updates, or deletes, and there is no corresponding 'bottom' clause.
您不能在子查询,更新或删除中使用top,并且没有相应的“bottom”子句。
The advantage of top is you don't have to worry about resetting it. This is especially important if you are using a database connection pool.
top的优点是你不必担心重置它。如果您使用数据库连接池,这一点尤为重要。
#3
-1
You can try adding this clause "fetch first n rows only".
您可以尝试添加此子句“仅获取前n行”。