简体   繁体   English

如果我真的不需要它们,是否应该获取所有数据库表字段?

[英]Should I get all database table fields if I don't really need them?

I have a class Employee which has some fields: 我有一个Employee类,其中包含一些字段:

EmployeeID, FirstName, LastName, Age, Rank 员工编号,名字,姓氏,年龄,等级

And I want to just get all employees from database and print their names in ComboBox, so what I really need is: 我只想从数据库中获取所有员工,并在ComboBox中打印他们的姓名,所以我真正需要的是:

EmployeeID, FirstName, LastName 员工编号,名字,姓氏

My question is: Should I always get all fields from db to let's say fill all of my class fields, or just the ones I need?. 我的问题是:我是否应该总是从db获取所有字段,比如说填写我所有的类字段,还是仅填写我需要的那些字段?

PS. PS。 Guys some time ago I asked a question which hasn't been really answered: Should a class have string fields for values from SQL JOIN from dictionary tables I would be really happy if you could maybe take a look at it and share your thoughts. 不久前,我问了一个尚未真正回答的问题: 一个类是否应具有字典表中来自SQL JOIN的值的字符串字段,如果您可以看看它并分享您的想法,我将非常高兴。 Thanks. 谢谢。

Should I always get all fields from db to let's say fill all of my class fields, or just the ones I need? 我是否应该总是从db获取所有字段,比如说填写我所有的类字段,还是只填写我需要的那些字段?

Yes. 是。 You should get what you want! 您应该得到想要的东西!

Dont get all the columns from the tables as that would be unnecessary for you. 不要从表中获取所有列,因为这对您来说是不必要的。

Even if you take all the columns from the table and if you are not using them then that is of no use and also you are increasing the work of your SQL Server. 即使您从表中取出所有列,即使您没有使用它们,也没有用,而且您也在增加SQL Server的工作量。

On a side note:- 附带说明:-

This may depend upon your need also ie, if you want to reduce the number of database hits then you may fetch all the records at once. 这也可能取决于您的需要,即,如果您要减少数据库命中的次数,则可以一次获取所有记录。 So in short it may depend upon how frequently you are accessing your database. 简而言之,这可能取决于您访问数据库的频率。

The strait forward answer is "No, you should only fetch the fields you need." 海峡前进的答案是“不,您应该只获取所需的字段”。

But there is always a trade off, do you want to have more or less duplicate code for different views of the same table. 但是总要权衡取舍,是否要为同一表的不同视图提供或多或少的重复代码。 It depends upon how often you fetch the data, how many rows, the size of the fields. 这取决于您获取数据的频率,多少行,字段的大小。

In general client-server programming the better way is to send only needed data from server to client, so general answer to your question is: "you should get only needed fields from database, this will reduce network traffic and packet parsing time" 在一般的客户端-服务器编程中,更好的方法是仅将所需的数据从服务器发送到客户端,因此,对您的问题的一般回答是: “您应该仅从数据库中获取所需的字段,这将减少网络流量和数据包解析时间”

but, if you're using DAO approach, ie each object in your code presents table from the db - you have to fetch all, because you need fully initialized object 但是,如果您使用的是DAO方法,即代码中的每个对象都来自db,则必须获取所有表,因为您需要完全初始化的对象

contradiction? 矛盾? No, if you want to keep your DAO objects, but fetch only needed data you will need to have 2 objects: 不,如果您想保留DAO对象,但仅获取所需的数据,则将需要具有2个对象:

  1. FullEmployeeInfo - this object will have all fields from DB, this object will be used during DB update/full info presentation FullEmployeeInfo-此对象将具有数据库中的所有字段,此对象将在数据库更新/完整信息演示期间使用
  2. PartialEmployeeInfo - this object will have only part of fields and will not be used to update data in DB , it will be just readonly object PartialEmployeeInfo-该对象将只有一部分字段,并且将不用于更新DB中的数据 ,它只是只读对象

so, for fast selection and displaying in combobox you will use 2nd one with query which returns only partial information 因此,为了快速选择并在组合框中显示,您将使用第二个查询,该查询仅返回部分信息

Normally it's best to get only what you need - reducing the amount of data you fetch reduces the time taken to fetch it, so improves performance. 通常,最好只获取所需的内容-减少获取的数据量可以减少获取数据的时间,从而提高性能。 It also means that your application is more resilient to change (eg if you replaced Age with DOB you'd need to update your application from taking an integer called Age to taking a date called DOB assuming you requested all fields - if you only requested those you needed (ie not age / dob) that change would not affect you. 这也意味着您的应用程序具有更强的变更适应性(例如,如果您要求所有字段,则需要将应用程序从使用Age的整数更新为使用DOB的日期来更新应用程序,前提是您要求所有字段-如果仅请求了这些字段您需要(即不需要年龄/出生日期)更改不会影响您。

That said, if you're populating a class it may be a good idea to fetch all columns at once - since if the class holds all of this information it's possible it may be accessed at some later point, and rather than fetching individual columns as they're needed, one up front fetch followed by pulling the data from cache would be faster overall. 就是说,如果您要填充一个类,一次获取所有列可能是一个好主意-因为如果该类拥有所有这些信息,则有可能稍后再访问它,而不是像这样获取单个列它们是必需的,总体而言,先进行一次提取然后再从缓存中提取数据会更快。

So it's a case of weighing up the performance impacts and any other factors of concern (eg maintenance effort) based on how you expect your application to be used. 因此,这是根据您对应用程序的使用情况来权衡性能影响和任何其他相关因素(例如,维护工作)的情况。 There is no blanket rule; 没有总括规则。 only guidelines as to what factors should influence your decision. 只有有关哪些因素会影响您的决定的指南。

My opinion There are 2 ways of looking at this if this is the only time you will ever use this class Employee data and you will never need all the values for the table then just have the class select only the fields you need the performance will be faster But in case you will use these values somewhere else in the program then i would select all the values because later u might have to change or make a new one to get the values u need. 我的看法有两种查看此问题的方法,如果这是您唯一一次使用此类Employee数据,而您将永远不需要表的所有值,那么只需让该类仅选择您需要性能的字段即可。更快,但是如果您在程序中的其他位置使用这些值,则我会选择所有值,因为以后您可能必须更改或制作新值才能获得所需的值。 and I personally like having one class do 2 jobs rater then having 2 classes. 我个人喜欢上一堂课做2个职位评估者,然后上两堂课。

You need to consider what you've contractually agreed to with the architecture you've implemented. 您需要考虑已实现架构的合同约定。 Let me explain... 让我解释...

It depends on what you are using as your data transport mechanism. 这取决于您用作数据传输机制的方式。

If you are using an ADO data table, then simply fetch what you need. 如果您使用的是ADO数据表,则只需获取所需的内容。 The consumer is expecting a data table and should test that the columns it expects are present. 使用者期望有一个数据表,并且应该测试期望的列是否存在。 Note that this is not contractually sound - a column can be missed out and you'll get a runtime error. 请注意,这在合同上并不合理-可能会漏掉一列,并且会出现运行时错误。 Columns can get swapped around and you can potentially cause a runtime error. 列可能会交换,您可能会导致运行时错误。

However if you are filling a data object then you need to get everything . 但是,如果要填充数据对象, 则需要获取所有内容 This is because you've contractually agreed to provide the consumer of that object with all the data that should be in that data object. 这是因为您已按照合同约定向该对象的使用者提供应该在该数据对象中的所有数据。 Of course this means you will have to consider the design of your data objects - they shouldn't just mirror the data table or view that the data is extracted from. 当然,这意味着您将不得不考虑数据对象的设计-它们不应该只是镜像数据表或查看数据是从中提取的。 If the source of the data is 80 columns wide and you can't restructure that data source then you should consider having a lightweight and heavyweight version of your data object, and only use the heavyweight version when you have to. 如果数据源为80列宽,并且您无法重组该数据源,则应考虑拥有数据对象的轻量级和重量级版本,并且仅在必须时才使用重量级版本。

You should also consider the amount of rows you are retrieving at any one time - are you being very selective, or are you grabbing everything including rows you'll never use? 您还应该考虑任何时候要检索的行数-您是否很有选择性,还是要获取包​​括您永远不会使用的行在内的所有内容?

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

相关问题 事件不是字段 - 我不明白 - Events aren't fields - I don't get it 当我点击销毁其中一个时,如何确保我的所有块都不会被销毁? - How do I make sure that all of my blocks don't get destroyed when I click to destroy one of them? 如何在插入一些数据时从数据库中获取 ID,但我没有插入稍后需要的 ID - How can I get ID from database while inserting some data, but I don't insert that ID that I need later 我需要删除所有内容。 与模式不符 - I need to remove all . that don't match a pattern 为什么我不需要C#数据类型别名的“使用系统”,而需要.NET类型的别名? - Why I don't need “using System” for C# data type aliases but need them for .NET types? 我需要所有的Android SDK,如何安装它们? - Do I need all of the Android SDKs and how should I install them? 如何加速C#/ Linq查询? [我不需要获取数据,我需要得到条件] - How to accelerate C#/Linq query? [i don't need to get data, i need to get condition] 我不太了解Page.FindControl - I don't really understand Page.FindControl 我真的不明白C#的参考 - I really don't understand the reference of C# 收到一个我不理解的非常奇怪的逻辑错误 - Getting a really strange logic error which I don't understand
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM