读XML文件或查询数据库哪个更快

时间:2021-12-06 02:58:02

I am designing a CMS in C# and need to decide where to save configuration settings for the site. Also considering defining my base html templates and then processing them server side to create the pages ahead of time.

我正在用c#设计一个CMS,需要决定在哪里保存站点的配置设置。还要考虑定义我的基本html模板,然后在服务器端处理它们,以便提前创建页面。

So what is generally faster/less overhead for the server reading an XML file or querying that same information from a local database?

那么,对于从本地数据库读取XML文件或查询相同信息的服务器来说,什么是更快/更少的开销呢?

6 个解决方案

#1


9  

It seems unlikely to me that this will be a bottleneck in your code. How often are you planning on reading configuration settings? Configuration is typically fairly small, and read infrequently. There are more important things to consider about where and how to store it:

在我看来,这似乎不太可能成为代码中的瓶颈。您计划阅读配置设置的频率是多少?配置通常是相当小的,并且很少被读取。还有一些更重要的事情需要考虑,关于在哪里以及如何存储它:

  • Bootstrapping: you can probably rely on your app having access to the local file system, and you can hard-code a configuration filename... but if all your configuration is in the database, where do you configure which database to talk to?

    引导:您可以依赖于应用程序访问本地文件系统,并且可以硬编码配置文件名……但是,如果您的所有配置都在数据库中,您在哪里配置要与之对话的数据库呢?

  • Ease of tweaking and deployment: editing a configuration file on the server by hand may be faster than making a change in the database... but if you have multiple servers, do you want to tweak a file on every one of them?

    易于调整和部署:手工编辑服务器上的配置文件可能比修改数据库要快……但是,如果您有多个服务器,您想要对每个服务器都修改一个文件吗?

  • Simplicity of code reading/processing the configuration: what would your configuration look like? Is it naturally hierarchical? If so, XML is likely to be a good fit. If it's more like a set of name/value pairs, then a simple table is a good fit. Of course, you can store XML within a database - you don't have to tie the storage location and the storage format decisions together. Editing an XML document on the database may well be harder than either editing an XML file or changing individual values... but you can always make life easier with tools for this sort of thing.

    简单的代码读取/处理配置:您的配置是什么样子的?自然分层吗?如果是这样,XML可能是一个很好的选择。如果它更像一组名称/值对,那么简单的表就是一个很好的选择。当然,您可以在数据库中存储XML——您不必将存储位置和存储格式决策联系在一起。在数据库上编辑XML文档可能比编辑XML文件或更改单个值都要困难……但是你可以用工具让生活更轻松。

#2


3  

Just for server settings - really doesn't matter. You're only going to read them once. Even if it takes a couple of seconds, still will be unnoticable.

仅仅是服务器设置——真的不重要。你只会读一次。即使花了几秒钟的时间,也不会被注意到。

First measure, then optimize.

第一次测量,然后优化。

#3


2  

How long is a piece of string? I can write a database query that's much slower than reading the same data from an XML file, but I can also write an XML file that's much slower to query than reading a database.

一根绳子有多长?我可以编写一个比从XML文件中读取相同数据慢得多的数据库查询,但我也可以编写一个比读取数据库慢得多的XML文件。

I would say if you're displaying "mostly" static content and you're worried about performance, then it's probably a better idea to implement it in whatever way you think would be the simplest, then use a caching mechanism to make it performant - this way, the first access might be "slow" but subsequent accesses will be much, much faster.

我想说如果你显示“主要”静态内容和你担心的性能,那么它可能是一个更好的主意来实现它,按照你自己的想法是最简单的,然后使用缓存机制,使其性能——这种方式,第一个访问可能是“慢”,但随后的访问将多,快得多。

Typically, if you're generating HTML content, write the completed HTML to disk and send that to the browser, instead of populating it from the database/XML files on subsequent requests. If you have your backend process delete the cached files whenever it does an update to the content, then the server can automatically detect when the file doesn't exist and re-generate it again.

通常,如果您正在生成HTML内容,那么将完成的HTML写到磁盘并将其发送到浏览器,而不是在后续请求时从数据库/XML文件中填充它。如果您的后端进程在对内容进行更新时删除缓存的文件,那么服务器可以自动检测文件何时不存在并重新生成它。

#4


1  

This depends on the strategy you are going to employ to access the data.

这取决于访问数据的策略。

If you go the database route, are you going to cache your results? There could be a lot of network chatter if you constantly pull out the details from the db.

如果使用数据库路径,是否要缓存结果?如果你不断地从数据库中提取细节,可能会有很多网络聊天。

In terms of simplicity you really can be agnostic to the data source using Linq..

就简单性而言,使用Linq您确实可以与数据源无关。

Faster? Once the stuff is in memory there should be no difference. Configuration information, as another poster pointed out is typically fairly static. Why not create a console app and quantify the differences, by using a profiler.

更快呢?一旦这些东西在记忆中,就没有区别了。正如另一篇海报所指出的,配置信息通常是相当静态的。为什么不创建一个控制台应用程序,通过使用分析器来量化差异呢?

http://www.red-gate.com/products/ants_performance_profiler/index.htm?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=antsperformanceprofiler&gclid=CLXmzbLXnKMCFQs-lAodSXTGsQ

http://www.red-gate.com/products/ants_performance_profiler/index.htm?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=antsperformanceprofiler&gclid=CLXmzbLXnKMCFQs-lAodSXTGsQ

#5


0  

if your most important point is speed, then use a database. xml is very slow.

如果最重要的一点是速度,那么使用数据库。xml是非常缓慢的。

but, if your data is very "complicated" or has many different relations and attributes, consider using xml

但是,如果您的数据非常“复杂”或具有许多不同的关系和属性,请考虑使用xml

#6


0  

I'd just use a database by default here. It's faster and requires less code.

我这里默认使用数据库。它更快,需要的代码更少。

#1


9  

It seems unlikely to me that this will be a bottleneck in your code. How often are you planning on reading configuration settings? Configuration is typically fairly small, and read infrequently. There are more important things to consider about where and how to store it:

在我看来,这似乎不太可能成为代码中的瓶颈。您计划阅读配置设置的频率是多少?配置通常是相当小的,并且很少被读取。还有一些更重要的事情需要考虑,关于在哪里以及如何存储它:

  • Bootstrapping: you can probably rely on your app having access to the local file system, and you can hard-code a configuration filename... but if all your configuration is in the database, where do you configure which database to talk to?

    引导:您可以依赖于应用程序访问本地文件系统,并且可以硬编码配置文件名……但是,如果您的所有配置都在数据库中,您在哪里配置要与之对话的数据库呢?

  • Ease of tweaking and deployment: editing a configuration file on the server by hand may be faster than making a change in the database... but if you have multiple servers, do you want to tweak a file on every one of them?

    易于调整和部署:手工编辑服务器上的配置文件可能比修改数据库要快……但是,如果您有多个服务器,您想要对每个服务器都修改一个文件吗?

  • Simplicity of code reading/processing the configuration: what would your configuration look like? Is it naturally hierarchical? If so, XML is likely to be a good fit. If it's more like a set of name/value pairs, then a simple table is a good fit. Of course, you can store XML within a database - you don't have to tie the storage location and the storage format decisions together. Editing an XML document on the database may well be harder than either editing an XML file or changing individual values... but you can always make life easier with tools for this sort of thing.

    简单的代码读取/处理配置:您的配置是什么样子的?自然分层吗?如果是这样,XML可能是一个很好的选择。如果它更像一组名称/值对,那么简单的表就是一个很好的选择。当然,您可以在数据库中存储XML——您不必将存储位置和存储格式决策联系在一起。在数据库上编辑XML文档可能比编辑XML文件或更改单个值都要困难……但是你可以用工具让生活更轻松。

#2


3  

Just for server settings - really doesn't matter. You're only going to read them once. Even if it takes a couple of seconds, still will be unnoticable.

仅仅是服务器设置——真的不重要。你只会读一次。即使花了几秒钟的时间,也不会被注意到。

First measure, then optimize.

第一次测量,然后优化。

#3


2  

How long is a piece of string? I can write a database query that's much slower than reading the same data from an XML file, but I can also write an XML file that's much slower to query than reading a database.

一根绳子有多长?我可以编写一个比从XML文件中读取相同数据慢得多的数据库查询,但我也可以编写一个比读取数据库慢得多的XML文件。

I would say if you're displaying "mostly" static content and you're worried about performance, then it's probably a better idea to implement it in whatever way you think would be the simplest, then use a caching mechanism to make it performant - this way, the first access might be "slow" but subsequent accesses will be much, much faster.

我想说如果你显示“主要”静态内容和你担心的性能,那么它可能是一个更好的主意来实现它,按照你自己的想法是最简单的,然后使用缓存机制,使其性能——这种方式,第一个访问可能是“慢”,但随后的访问将多,快得多。

Typically, if you're generating HTML content, write the completed HTML to disk and send that to the browser, instead of populating it from the database/XML files on subsequent requests. If you have your backend process delete the cached files whenever it does an update to the content, then the server can automatically detect when the file doesn't exist and re-generate it again.

通常,如果您正在生成HTML内容,那么将完成的HTML写到磁盘并将其发送到浏览器,而不是在后续请求时从数据库/XML文件中填充它。如果您的后端进程在对内容进行更新时删除缓存的文件,那么服务器可以自动检测文件何时不存在并重新生成它。

#4


1  

This depends on the strategy you are going to employ to access the data.

这取决于访问数据的策略。

If you go the database route, are you going to cache your results? There could be a lot of network chatter if you constantly pull out the details from the db.

如果使用数据库路径,是否要缓存结果?如果你不断地从数据库中提取细节,可能会有很多网络聊天。

In terms of simplicity you really can be agnostic to the data source using Linq..

就简单性而言,使用Linq您确实可以与数据源无关。

Faster? Once the stuff is in memory there should be no difference. Configuration information, as another poster pointed out is typically fairly static. Why not create a console app and quantify the differences, by using a profiler.

更快呢?一旦这些东西在记忆中,就没有区别了。正如另一篇海报所指出的,配置信息通常是相当静态的。为什么不创建一个控制台应用程序,通过使用分析器来量化差异呢?

http://www.red-gate.com/products/ants_performance_profiler/index.htm?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=antsperformanceprofiler&gclid=CLXmzbLXnKMCFQs-lAodSXTGsQ

http://www.red-gate.com/products/ants_performance_profiler/index.htm?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=antsperformanceprofiler&gclid=CLXmzbLXnKMCFQs-lAodSXTGsQ

#5


0  

if your most important point is speed, then use a database. xml is very slow.

如果最重要的一点是速度,那么使用数据库。xml是非常缓慢的。

but, if your data is very "complicated" or has many different relations and attributes, consider using xml

但是,如果您的数据非常“复杂”或具有许多不同的关系和属性,请考虑使用xml

#6


0  

I'd just use a database by default here. It's faster and requires less code.

我这里默认使用数据库。它更快,需要的代码更少。