简体   繁体   English

我应该执行许多sql查询还是一个大型查询,并在服务器上进行处理?

[英]Should I do many sql queries or one large query and do the processing on the server?

The situation is as follows: I have a large-ish dataset with a couple thousand entries that I populate from an Excel file. 情况如下:我有一个很大的数据集,其中包含从Excel文件填充的数千个条目。 For each entry I have to match it to another field on a certain table in the database (this table contains only a couple hundred entries). 对于每个条目,我必须将其与数据库中某个表上的另一个字段进行匹配(此表仅包含数百个条目)。

What's the best way to go about doing it? 最好的方法是什么? I can make a query for each entry in the dataset but this seems fairly wasteful; 我可以查询数据集中的每个条目,但这看起来很浪费。 on the other hand I can just select the fields I need from all the entries in the table, put them on a Dictionary or some other data structure and match them on IIS, thus making effectively only one query but doing all the processing on the webserver. 另一方面,我可以从表中的所有条目中选择所需的字段,将它们放在Dictionary或其他数据结构上,并在IIS上进行匹配,这样就可以有效地仅执行一个查询,但可以在Web服务器上进行所有处理。

Dataset : ~1000 to ~3000 entries 数据集:〜1000至〜3000个条目

Table in the DB: ~300 entries DB中的表:〜300个条目

Using asp.net on IIS but the database is a MS access file. 在IIS上使用asp.net,但数据库是MS访问文件。

Is either of these better the other? 这些中的一个更好吗? Is there a third, better way I haven't thought of? 有我没有想到的第三种更好的方法吗?

This is too long for a comment. 这个评论太长了。

Databases are designed to do many things that are useful for data processing. 数据库旨在执行许多对数据处理有用的事情。 A lot of benefits for transactional processing are contained in the acronym ACID -- atomicity, consistency, isolation, durability. 缩写ACID包含事务处理的许多好处-原子性,一致性,隔离性,持久性。 In other words, databases behave the way you would expect when you store something in them. 换句话说,数据库的行为与您在其中存储内容时的期望方式相同。 The data is there, relationships are enforced, it will be there tomorrow. 数据在那里,关系得到加强,明天就会在那里。

The features that you want are on the querying side. 所需的功能在查询方面。 Databases in general (although perhaps not MS Access in particular) allow a relatively standard interface to powerful processing. 通常,数据库(尽管可能不是特别是MS Access)允许相对标准的接口来进行强大的处理。 Database engines know how to optimize queries. 数据库引擎知道如何优化查询。 Database engines know how to manage memory. 数据库引擎知道如何管理内存。 Database engines know how to manager hierarchical memory, with disk, RAM, and cache. 数据库引擎知道如何使用磁盘,RAM和缓存来管理分层内存。 Databases know how to take advantage of indexes, row partitions, and other optimizations. 数据库知道如何利用索引,行分区和其他优化。 (You can get this functionality by using a free version of a more advanced database, such as SQL Server, Oracle, Postgres, or even MySQL.) (您可以使用免费版本的更高级的数据库来获得此功能,例如SQL Server,Oracle,Postgres甚至MySQL。)

You are talking about thousands of rows of data. 您正在谈论数千行数据。 Databases can easily work with millions of rows. 数据库可以轻松处理数百万行。 You are talking about two tables. 您正在谈论两个表。 Databases can easily manage many more tables and queries using a dozen. 数据库可以轻松管理十二个表和查询。

So, no, you should not load your data into in-memory structures on the application side. 因此,不,您不应该将数据加载到应用程序端的内存结构中。 You should do the processing in the database and bring back the results you want. 您应该在数据库中进行处理,并返回所需的结果。 Then, you can format the results on the application side, to take advantage of what applications do best: interface to the user. 然后,您可以在应用程序端设置结果的格式,以利用最擅长的应用程序:与用户交互。

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

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