javascript - 用户修改一个列表型的数据,后台和数据库互动时应该怎么操作好?
比如每个用户有一个自己的收藏列表,用户可以增加和删除列表中的任一项。
那么用HTTP请求和数据库互动时,以下哪种是最科学的呢?
1,每次增删都发起一次HTTP请求,在数据库上作相应操作(这个HTTP请求太多我自己觉得不科学)
2,把数据库上的列表数据下载下来呈现给用户后,只在前端的数据模型中增删,等到用户可能要关闭页面时用一次HTTP请求来同步,使数据库上的数据更新。不过我主要想问的是这里又有两种具体的互动方法,请大家赐教:
2a,在前端数据模型操作时,记录下哪些被增加了哪些被删除了。在更新数据库做Insert into... where value in和Delete ... where value in 这两次操作
2b,前端只记录数据模型。在更新数据库时直接truncate原来的表,把新的表数据插入。
请问2a和2b哪种比较好?
另外,第2步中同步的时机一般应该是什么时候呢?是用interval来定时同步呢?还是在用户进行一个特定的操作后来同步?谢谢。
-------1218更新
不好意思,上面忘记说了,我指的是用angularJS这种情况下,可以用数据模型来暂存数据的
回复内容:
比如每个用户有一个自己的收藏列表,用户可以增加和删除列表中的任一项。
那么用HTTP请求和数据库互动时,以下哪种是最科学的呢?
1,每次增删都发起一次HTTP请求,在数据库上作相应操作(这个HTTP请求太多我自己觉得不科学)
2,把数据库上的列表数据下载下来呈现给用户后,只在前端的数据模型中增删,等到用户可能要关闭页面时用一次HTTP请求来同步,使数据库上的数据更新。不过我主要想问的是这里又有两种具体的互动方法,请大家赐教:
2a,在前端数据模型操作时,记录下哪些被增加了哪些被删除了。在更新数据库做Insert into... where value in和Delete ... where value in 这两次操作
2b,前端只记录数据模型。在更新数据库时直接truncate原来的表,把新的表数据插入。
请问2a和2b哪种比较好?
另外,第2步中同步的时机一般应该是什么时候呢?是用interval来定时同步呢?还是在用户进行一个特定的操作后来同步?谢谢。
-------1218更新
不好意思,上面忘记说了,我指的是用angularJS这种情况下,可以用数据模型来暂存数据的
1.你为什么认为1是不科学的呢?天知道客户端哪边会发生什么事情~~当用户添加了N个收藏,猫猫过来把电源搞掉了-关机了,这个咋整哦~~~
2.用户希望它的每一步操作都是有效而真实的~~
3.删除动作,不要DELETE,UPDATE原纪录,做逻辑删除
4.truncate表这么暴力的招都用上啦,不要啊~~~~
只能每次发请求这是http
肯定要每次发送请求啊,你在前端怎么保存数据?如果不将用户的操作存入数据库,当用户不小心刷新页面或者关闭页面,他的操作在前端根本没有办法记录下来。
这种就看你要怎么设计了,如果你列表下面有个提交或者保存按钮,用户就明白N种操作完成必须提交或保存才能生效;如果你没有这种提示性信号,只是在每个列表后有个删除,然后有个随意增加的,用户理解成操作即保存,那就不能缓存处理了呗,只能实时性操作数据库
truncate这个有点不太科学,除非每个用户都有一个自已的收藏表
可以找找 window.onbeforeunload
这个事件 在事件中先判断用户收藏的数据是否有改动,有则http请求处理
最近我也在烦这个http请求次数的问题,看了你的描述之后,突然有个想法。。。但不知道可行不可行。。。
首先是获取数据的时候将数据存入一个数据模型里,然后让页面从这个数据模型里面拿取数据。
当页面发生修改事件时,将修改后的数据存入数据模型,然后再从模型拿出修改页面
然后开个定时器,隔一定时间就判断数据模型里面的数据是否发生改动,是就发生请求,不是就不发送。
最后当用户离开该页面的时候最后再判断一次。。。
。。。
或许可以当第一个修改事件发生的时候开启定时器。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.
