可以限制返回的结果,但保留“分页”;到餐桌?

发布于 2024-10-03 01:00:51 字数 277 浏览 2 评论 0原文

我正在使用 jquery 和 DataTables 插件构建一个 php 网站。我的页面的布局与分页工作所需的一样,但在处理大型数据集时,我注意到服务器正在提取所有返回的行,而不是每个“页面”中规定的 10 行(可以更多)限制。

是否可以限制查询的结果,但仍将这些结果的 ID 号保留在内存中,以便当点击第 2 页(或结果号更改)时仅寻找新数据?

这样做有意义吗?

我只是不想查询返回 2000 行的数据库,然后使用“前端插件”使其看起来像其他结果在从一开始就真实地出现在页面上时被隐藏一样。

I am building a php site using jquery and the DataTables plugin. My page is laid out just as it needs to be with pagination working but in dealing with large datasets, I have noticed the server is pulling ALL returned rows as opposed to the 10 row (can be more) limit stated within each 'page'.

Is it possible to limit the results of a query and yet keep say the ID numbers of those results in memory so when page 2 is hit (or the result number is changed) only new data is sought after?

Does it even make sense to do it this way?

I just dont want to query a DB with 2000 rows returned then have a 'front-end-plugin' make it look like the other results are hidden when they are truthfully on the page from the start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-10-10 01:00:51

SQL 中的 LIMIT 子句有两部分——限制和偏移量。

获取前 10 行:

SELECT ... LIMIT 0,10

获取后 10 行:

SELECT ... LIMIT 10,10

获取后 10 行:

SELECT ... LIMIT 20,10

只要每次对结果集进行相同的排序,您绝对不必(也不想)先这样做要求数据库向您发送全部 2000 行。

要与此结合显示分页,您仍然需要知道与查询匹配的总行数。有两种方法可以处理这个问题 -

1)通过单独的查询请求行数

SELECT COUNT(*) FROM table WHERE ...

2)在查询中使用 SQL_CALC_FOUND_ROWS 提示,这将告诉 MySQL 在仅返回您要求的 10 行之前计算有多少行与查询匹配。然后,您发出 SELECT FOUND_ROWS() 查询来获取该结果。

SELECT SQL_CALC_FOUND_ROWS column1, column2 ... LIMIT 0,10

2 更好,因为它不会为每个页面加载添加额外的查询。

The LIMIT clause in SQL has two parts -- the limit and the offset.

To get the first 10 rows:

SELECT ... LIMIT 0,10

To get the next 10 rows:

SELECT ... LIMIT 10,10

To get the next 10 rows:

SELECT ... LIMIT 20,10

As long as you ORDER the result set the same each time, you absolutely don't have to (and don't want to) first ask the database to send you all 2000 rows.

To display paging in conjunction with this, you still need to know how many total rows match your query. There are two ways to handle that --

1) Ask for a row count with a separate query

SELECT COUNT(*) FROM table WHERE ...

2) Use the SQL_CALC_FOUND_ROWS hint in your query, which will tell MySQL to calculate how many rows match the query before returning only the 10 you asked for. You then issue a SELECT FOUND_ROWS() query to get that result.

SELECT SQL_CALC_FOUND_ROWS column1, column2 ... LIMIT 0,10

2 is preferable since it does not add an extra query to each page load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文