分页:服务器端还是客户端?

时间:2022-05-07 15:01:16

What is it best to handle pagination? Server side or doing it dynamically using javascript?

处理分页最好的是什么?服务器端还是使用javascript动态执行?

I'm working on a project which is heavy on the ajax and pulling in data dynamically, so I've been working on a javascript pagination system that uses the dom - but I'm starting to think it would be better to handle it all server side.

我正在研究一个对ajax很重要并且动态提取数据的项目,所以我一直在研究一个使用dom的javascript分页系统 - 但我开始认为处理它会更好服务器端。

What are everyone's thoughts?

每个人的想法是什么?

8 个解决方案

#1


64  

The right answer depends on your priorities and the size of the data set to be paginated.

正确的答案取决于您的优先级和要分页的数据集的大小。

Server side pagination is best for:

服务器端分页最适合:

  • Large data set
  • 大数据集
  • Faster initial page load
  • 更快的初始页面加载
  • Accessibility for those not running javascript
  • 那些没有运行javascript的人的辅助功能

Client side pagination is best for:

客户端分页最适合:

  • Small data set
  • 小数据集
  • Faster subsequent page loads
  • 更快的后续页面加载

So if you're paginating for primarily cosmetic reasons, it makes more sense to handle it client side. And if you're paginating to reduce initial load time, server side is the obvious choice.

因此,如果您主要是出于美观原因进行分页,那么处理客户端更有意义。如果你要分页以减少初始加载时间,那么服务器端是显而易见的选择。

Of course, client side's advantage on subsequent page load times diminishes if you utilize Ajax to load subsequent pages.

当然,如果您使用Ajax加载后续页面,客户端在后续页面加载时间方面的优势会减少。

#2


7  

Doing it on client side will make your user download all the data at first which might not be needed, and will remove the primary benefit of pagination.

在客户端执行此操作将使您的用户首先下载可能不需要的所有数据,并将删除分页的主要好处。

The best way to do so for such kind of AJAX apps is to make AJAX call the server for next page and add update the current page using client side script.

对于这种类型的AJAX应用程序,最好的方法是让AJAX为下一页调用服务器,并使用客户端脚本添加更新当前页面。

#3


5  

If you have large pages and a large number of pages you are better of requesting pages in chunks from the server via AJAX. So let the server do the pagination, based of your request URL.

如果你有大页面和大量页面,你最好通过AJAX从服务器请求页面。因此,让服务器根据您的请求URL进行分页。

You can also pre-fetch the next few pages the user will likely view to make the interface seem more responsive.

您还可以预先获取用户可能会查看的下几页,以使界面看起来更具响应性。

If there are only few pages, grabbing it all up-front and paginating on the client may be a better choice.

如果只有很少的页面,那么在客户端预先抓取并分页可能是更好的选择。

#4


4  

Even with small data sizes the best choice would be server side pagination. You will not have to worry later if your web application scales further.

即使数据量很小,最好的选择也是服务器端分页。如果您的Web应用程序进一步扩展,您将不必担心。

And for larger data sizes the answer is obvious.

对于更大的数据大小,答案是显而易见的。

#5


3  

Server side - send to the client just enough content for the current view.

服务器端 - 向客户端发送当前视图的足够内容。

#6


3  

Do you mean that your JavaScript has all the data in memory, and shows one page a time? Or that it downloads each page from the server as it's needed, using AJAX?

你是说你的JavaScript拥有内存中的所有数据,并且一次显示一页?或者它使用AJAX从服务器下载每个页面?

If it's the latter, you also may need to think about sorting. If you sort using JavaScript, you'll only be able to sort one page at a time, which doesn't make much sense. So your sorting should be done on the server.

如果是后者,您还可能需要考虑排序。如果您使用JavaScript排序,那么您一次只能对一个页面进行排序,这没有多大意义。所以你的排序应该在服务器上完成。

#7


2  

In a practical world of limits, I would page on the server side to conserve all the resources associated with sending the data. Also, the server needs to protect itself from a malicious/malfunctioning client asking for a HUGE page.

在实际的限制世界中,我会在服务器端寻呼以节省与发送数据相关的所有资源。此外,服务器需要保护自己免受恶意/故障客户端要求巨大页面的影响。

Once that code is happily chugging along, I would add "smarts" to the client to get the "next" and "previous" page and hold that in memory. When the user pages to the next page, update your cache.

一旦该代码快乐地开始,我就会向客户端添加“智能”以获取“下一个”和“上一个”页面并将其保存在内存中。当用户访问下一页时,请更新缓存。

If the client software does this sort of page caching, do consider how fast your data ages (is likely to change) and if you should check that your cached page of data is still valid. Maybe re-request it if it ages more than 2 minutes. Maybe have a "dirty" flag in it. Something like that. Hope you find this helpful. :)

如果客户端软件执行此类页面缓存,请考虑数据的老化速度(可能会发生变化),以及是否应检查缓存的数据页面是否仍然有效。如果它超过2分钟,也许重新请求它。也许它有一个“脏”的旗帜。这样的事情。希望你觉得这很有帮助。 :)

#8


2  

I prefer server side pagination. However, when implementing it, you need to make sure that you're optimizing your SQL properly. For instance, I believe in MySQL, if you use the LIMIT option it doesn't use the index so you need to rewrite your sql to use the index properly.

我更喜欢服务器端分页。但是,在实现它时,您需要确保正确地优化SQL。例如,我相信MySQL,如果你使用LIMIT选项它不使用索引,所以你需要重写你的sql以正确使用索引。

G-Man

G-人

#1


64  

The right answer depends on your priorities and the size of the data set to be paginated.

正确的答案取决于您的优先级和要分页的数据集的大小。

Server side pagination is best for:

服务器端分页最适合:

  • Large data set
  • 大数据集
  • Faster initial page load
  • 更快的初始页面加载
  • Accessibility for those not running javascript
  • 那些没有运行javascript的人的辅助功能

Client side pagination is best for:

客户端分页最适合:

  • Small data set
  • 小数据集
  • Faster subsequent page loads
  • 更快的后续页面加载

So if you're paginating for primarily cosmetic reasons, it makes more sense to handle it client side. And if you're paginating to reduce initial load time, server side is the obvious choice.

因此,如果您主要是出于美观原因进行分页,那么处理客户端更有意义。如果你要分页以减少初始加载时间,那么服务器端是显而易见的选择。

Of course, client side's advantage on subsequent page load times diminishes if you utilize Ajax to load subsequent pages.

当然,如果您使用Ajax加载后续页面,客户端在后续页面加载时间方面的优势会减少。

#2


7  

Doing it on client side will make your user download all the data at first which might not be needed, and will remove the primary benefit of pagination.

在客户端执行此操作将使您的用户首先下载可能不需要的所有数据,并将删除分页的主要好处。

The best way to do so for such kind of AJAX apps is to make AJAX call the server for next page and add update the current page using client side script.

对于这种类型的AJAX应用程序,最好的方法是让AJAX为下一页调用服务器,并使用客户端脚本添加更新当前页面。

#3


5  

If you have large pages and a large number of pages you are better of requesting pages in chunks from the server via AJAX. So let the server do the pagination, based of your request URL.

如果你有大页面和大量页面,你最好通过AJAX从服务器请求页面。因此,让服务器根据您的请求URL进行分页。

You can also pre-fetch the next few pages the user will likely view to make the interface seem more responsive.

您还可以预先获取用户可能会查看的下几页,以使界面看起来更具响应性。

If there are only few pages, grabbing it all up-front and paginating on the client may be a better choice.

如果只有很少的页面,那么在客户端预先抓取并分页可能是更好的选择。

#4


4  

Even with small data sizes the best choice would be server side pagination. You will not have to worry later if your web application scales further.

即使数据量很小,最好的选择也是服务器端分页。如果您的Web应用程序进一步扩展,您将不必担心。

And for larger data sizes the answer is obvious.

对于更大的数据大小,答案是显而易见的。

#5


3  

Server side - send to the client just enough content for the current view.

服务器端 - 向客户端发送当前视图的足够内容。

#6


3  

Do you mean that your JavaScript has all the data in memory, and shows one page a time? Or that it downloads each page from the server as it's needed, using AJAX?

你是说你的JavaScript拥有内存中的所有数据,并且一次显示一页?或者它使用AJAX从服务器下载每个页面?

If it's the latter, you also may need to think about sorting. If you sort using JavaScript, you'll only be able to sort one page at a time, which doesn't make much sense. So your sorting should be done on the server.

如果是后者,您还可能需要考虑排序。如果您使用JavaScript排序,那么您一次只能对一个页面进行排序,这没有多大意义。所以你的排序应该在服务器上完成。

#7


2  

In a practical world of limits, I would page on the server side to conserve all the resources associated with sending the data. Also, the server needs to protect itself from a malicious/malfunctioning client asking for a HUGE page.

在实际的限制世界中,我会在服务器端寻呼以节省与发送数据相关的所有资源。此外,服务器需要保护自己免受恶意/故障客户端要求巨大页面的影响。

Once that code is happily chugging along, I would add "smarts" to the client to get the "next" and "previous" page and hold that in memory. When the user pages to the next page, update your cache.

一旦该代码快乐地开始,我就会向客户端添加“智能”以获取“下一个”和“上一个”页面并将其保存在内存中。当用户访问下一页时,请更新缓存。

If the client software does this sort of page caching, do consider how fast your data ages (is likely to change) and if you should check that your cached page of data is still valid. Maybe re-request it if it ages more than 2 minutes. Maybe have a "dirty" flag in it. Something like that. Hope you find this helpful. :)

如果客户端软件执行此类页面缓存,请考虑数据的老化速度(可能会发生变化),以及是否应检查缓存的数据页面是否仍然有效。如果它超过2分钟,也许重新请求它。也许它有一个“脏”的旗帜。这样的事情。希望你觉得这很有帮助。 :)

#8


2  

I prefer server side pagination. However, when implementing it, you need to make sure that you're optimizing your SQL properly. For instance, I believe in MySQL, if you use the LIMIT option it doesn't use the index so you need to rewrite your sql to use the index properly.

我更喜欢服务器端分页。但是,在实现它时,您需要确保正确地优化SQL。例如,我相信MySQL,如果你使用LIMIT选项它不使用索引,所以你需要重写你的sql以正确使用索引。

G-Man

G-人