每个页面视图的成本更高 - 数据库写入或文件写入? [英] What's more costly on every page view - Database Writes or File Writes?

查看:162
本文介绍了每个页面视图的成本更高 - 数据库写入或文件写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您需要在应用程序的每个页面视图上记录一些数据时,什么是最有效的解决方案 - 您应该写入文件还是写入数据库?

What is the most efficient solution when you need to record some data on every page view in your application - should you write to a file or write to the database?

或者也许两个 - 也许你应该缓存数据在内存或文件,并只写到数据库(或文件系统,如果你使用内存缓存)偶尔?

Or maybe neither - perhaps you should cache the data in memory or a file and only write it to the database (or file system if you use a memory cache) occasionally?

推荐答案

如果它只是记录少量数据,没有后续的查找,直接文件I / O几乎可以保证更多高效。你失去了DBMS的所有优点,虽然 - 索引,事务完整性(一般来说,真的,ACID),并发访问等。

If it's purely recording a small amount of data with no subsequent lookups, straight file I/O is almost guaranteed to be more efficient. You're losing all the advantages of a DBMS though -- indexing, transactional integrity (really, ACID in general), concurrent access, etc..

这听起来像你在谈论什么相当于简单的日志记录。如果是这种情况,并且您不需要对生成的数据进行频繁的复杂查询,如果性能是一个严重问题,您可能最好使用直接文件I / O。注意并发写入问题。

It almost sounds like you're talking about what amounts to simple logging. If that's the case, and you don't need to do frequent complex queries on the resulting data, you're probably better off with straight file I/O if performance is a serious issue. Be careful of concurrent-write issues, though.

如果RDBMS的属性是可取的,你可能会考虑使用SQLite,这对于简单的加载将获得更好的性能比大多数具有较少开销的RDBMS,以一些优点为代价(高度并发访问和通过网络到其他机器的可用性是一些大型)。它仍然不会像一般情况下的直接文件I / O一样快。

If the properties of an RDBMS are desirable, you might think about using SQLite, which for simplistic loads will get you better performance than most RDBMSs with less overhead, at the cost of some of the benefits (highly concurrent access and availability over the network to other machines are a couple of the "biggies"). It still wouldn't be as fast as straight file I/O in the general case, though.

您以后提到它是为了页面视图跟踪导致我问:是否递增计数器,而不是记录有关页面视图的数据?如果是这样,我强烈建议去像SQLite(做一些像UPDATE tbl SET counter = counter + 1)。你真的不想进入时间问题涉及手工做这 - 如果你不这样做,你会开始失去计数同时访问(A读100,B读100 ,A写入101,B写入101; B应该写入102,但是没有办法知道)。

Your later mention of it being for page view tracking causes me to ask: Are you incrementing a counter, rather than logging data about the page view? If so, I'd strongly suggest going with something like SQLite (doing something like UPDATE tbl SET counter = counter+1). You really don't want to get into the timing issues involved in doing this by hand -- if you don't do it right, you'll start losing counts on simultaneous access (A reads "100", B reads "100", A writes "101", B writes "101"; B should have written 102, but has no way of knowing that).

这篇关于每个页面视图的成本更高 - 数据库写入或文件写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆