简体   繁体   English

我应该在后端(Rails API)还是前端(React/Redux)上查询和过滤

[英]Should I query and filter on the back-end (Rails API) or front-end (React/Redux)

I have an app that allows users to sort and filter through 30,000 items of data.我有一个应用程序,它允许用户对 30,000 项数据进行排序和过滤。 Right now I make fetch requests from Redux actions to my rails API, with the queries being handled by scope methods on my rails end.现在我从 Redux 操作向我的 rails API 发出 fetch 请求,查询由我的 rails 端的范围方法处理。 My instructor is recommending that I move all my querying to my front-end for efficiency, but I'm wondering if it really will be more performant to manage a Redux state object with 30,000 objects in it, each with 50 of their own attributes.我的导师建议我将所有查询移到我的前端以提高效率,但我想知道管理一个包含 30,000 个对象的 Redux 状态对象是否真的会更高效,每个对象都有 50 个自己的属性。

(A couple extra notes: Right now I've only run the app locally and I'm doing the pagination server-side so it runs lightning fast, but I'm a bit nervous about when I launch it somewhere like Heroku. Also, I know that if I move my querying to the front-end I'll have more options to save the query state in the URL with react-router, but I've already sort of hacked a way around that with my existing set-up.) (一些额外的注意事项:现在我只在本地运行该应用程序,并且我在服务器端进行分页,因此它运行速度快如闪电,但是当我在像 Heroku 这样的地方启动它时我有点紧张。另外,我知道,如果我将查询移到前端,我将有更多选项可以使用 react-router 将查询状态保存在 URL 中,但是我已经用我现有的设置解决了这个问题.)

Let's have a look at the pros and cons of each approach:让我们来看看每种方法的优缺点:

Querying on Front End前端查询

  • 👍 Querying does not need another network request 👍 查询不需要另一个网络请求
  • 👎 Network requests are slower because there is more data to send 👎 网络请求较慢,因为有更多的数据要发送
  • 👎 App must store much more data in memory 👎 应用程序必须在内存中存储更多数据
  • 👎 Querying is not necessarily more efficient because the client has to do the filtering and it usually does not have the mechanisms to do so effectively (caching and indexing). 👎 查询不一定更有效,因为客户端必须进行过滤,并且它通常没有有效执行此操作的机制(缓存和索引)。

Querying on Back End后端查询

  • 👍 Less data to send to client 👍 发送给客户端的数据更少
  • 👍 Querying can be quite fast if database indexes are set up properly 👍 如果数据库索引设置正确,查询会非常快
  • 👍 App is more lightweight, it only holds the data it needs to display 👍 App 更轻量级,只保存需要显示的数据
  • 👎 Each query will require a network request 👎 每个查询都需要一个网络请求

The pros of querying on Back End heavily outweighs that on Front End.在后端查询的优点远远超过在前端查询的优点。 I would have to disagree with your instructor's opinion.我不得不不同意你导师的意见。 Imagine you want to search for something on Google and Google sends all relevant results you want to your browser and does the pagination and sorting within your browser, your browser would feel extremely sluggish.想象一下,您想在 Google 上搜索某些内容,而 Google 将您想要的所有相关结果发送到您的浏览器,并在您的浏览器中进行分页和排序,您的浏览器会感觉非常缓慢。 With proper caching and adding database indexes to your data, network requests will not be a huge disadvantage.通过适当的缓存并向您的数据添加数据库索引,网络请求将不会成为一个巨大的缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM