可以限制返回的结果,但保留“分页”;到餐桌?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SQL 中的 LIMIT 子句有两部分——限制和偏移量。
获取前 10 行:
获取后 10 行:
获取后 10 行:
只要每次对结果集进行相同的排序,您绝对不必(也不想)先这样做要求数据库向您发送全部 2000 行。
要与此结合显示分页,您仍然需要知道与查询匹配的总行数。有两种方法可以处理这个问题 -
1)通过单独的查询请求行数
2)在查询中使用 SQL_CALC_FOUND_ROWS 提示,这将告诉 MySQL 在仅返回您要求的 10 行之前计算有多少行与查询匹配。然后,您发出
SELECT FOUND_ROWS()
查询来获取该结果。2 更好,因为它不会为每个页面加载添加额外的查询。
The LIMIT clause in SQL has two parts -- the limit and the offset.
To get the first 10 rows:
To get the next 10 rows:
To get the next 10 rows:
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
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.2 is preferable since it does not add an extra query to each page load.