面向对象的方法在数据库调用方面效率低下 [英] Object oriented method is inefficient with database calling

查看:44
本文介绍了面向对象的方法在数据库调用方面效率低下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一辆带有属性的类车:颜色,品牌,型号,年份,DriverID

和一个带有属性的Driver类:DriverID,名称


driverID PRIVATE属性是来自驱动程序

table(t_driver)的驱动程序的id。这有一个名为Driver的PUBLIC属性访问器

加载一个汽车类时,你会访问数据库获取Color,make,Model,

年,DriverID并加载Car类。当PUBLIC属性Driver被

访问时,它将使用PRIVATE属性DriverID来调用DB并加载

Driver类。


在此示例中,将进行2次单独的DB调用以加载Car和

驱动程序类。我有DB /网络人员在工作,说这些电话

应该立即完成,以减少网络流量和数据库电话。


任何评论为为什么我的方式不是OO声音或火力我可以使用

告诉我的同龄人?


非常感谢!

解决方案

TS< ma ********** @ nospam.nospamwrote:


Say我有一个属性的类车:颜色,品牌,型号,年份,DriverID

和一个具有属性的Driver类:DriverID,名称


driverID PRIVATE property是驱动程序的id,比如说一个驱动程序

table(t_driver)。这有一个名为Driver的PUBLIC属性访问器

加载一个汽车类时,你会访问数据库获取Color,make,Model,

年,DriverID并加载Car类。当PUBLIC属性Driver被

访问时,它将使用PRIVATE属性DriverID来调用DB并加载

Driver类。


在此示例中,将进行2次单独的DB调用以加载Car和

驱动程序类。我有DB /网络人员在工作,说这些电话

应该立即完成,以减少网络流量和数据库电话。


任何评论为为什么我的方式不是OO声音或火力我可以使用

告诉我的同行?



根据所涉及的ORM系统,您通常可以急切地获取

的东西 - 发出一个查询,它将加载汽车和驱动程序

同时使用加入。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件





我想你应该采取不同的策略取决于
记录数据库查询到表t_car返回的数量。


如果对表t_car的数据库查询返回大量的记录a / b
时间,我想你最好为每条记录加载一个Car对象,并将

Driver属性留在Car对象中。如您所说,当访问公共

属性驱动程序时,使用私有属性DriverID访问数据库并加载一个Driver对象。

在上面的场景中,如果你在加载Car

对象时加载Driver对象,那么网络流量就会非常大,因为它有一个很棒的

查询后交易Car对象,你将为每个Car对象加载一个Driver

对象。


我不觉得您应该使用连接来查询此实例中的数据库

,因为连接操作也会消耗大量资源。


优点当公共属性驱动程序访问
时加载Driver对象是因为这样可以避免查询数据库的数量太多

a时间。


另一方面,如果您查询表t_car并获得一个记录,

您可以为记录加载Car对象d然后通过Car对象中的DriverID字段的值访问表格

t_driver并加载一个Driver

对象。这不会导致网络流量很大。


希望这会有所帮助。

如果您有任何不清楚的地方,请随时告诉我。


此致,

Linda Liu

Microsoft在线社区支持


=== ===============================================

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


此帖子按原样提供。没有保证,也没有授予任何权利。


Linda Liu [MSFT]< v - **** @ online.microsoft.comwrote:


我认为你应该采取不同的策略,取决于数据库查询到表t_car返回的数量
记录。


如果对表t_car的数据库查询返回了大量的记录时间,我想你最好为每条记录加载一个Car对象并离开

除了Car对象中的Driver属性。如您所说,当访问公共

属性驱动程序时,使用私有属性DriverID访问数据库并加载一个Driver对象。

在上面的场景中,如果你在加载Car

对象时加载Driver对象,那么网络流量就会非常大,因为它有一个很棒的

查询后交易Car对象,你将为每个Car对象加载一个Driver

对象。


我不觉得您应该使用连接来查询此实例中的数据库

,因为连接操作也会消耗大量资源。



你认为最好做501查询来检索500辆汽车而不是
而不是1有加入?我不同意。如果您非常确定

您将需要使用该驱动程序,那么加入这两个表是

逻辑方法IMO。


当公共属性驱动程序访问
时加载Driver对象的优点是,这样可以避免查询数据库的数量太多

a时间。


另一方面,如果您查询表t_car并获得一次记录,

您可以加载Car对象为了记录然后通过Car对象中的DriverID字段的值访问表格

t_driver并加载一个Driver

对象。这不会导致网络流量繁重。



虽然会引起更多疑问。我原本以为

实际上意味着网络流量更大,而不是更少的查询返回

相同的更大数据量的数据总量。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


Say i have a class car with properties: Color, Make, Model, Year, DriverID
And a Driver class with properties: DriverID, Name

The driverID PRIVATE property is the id of the driver from say a driver
table (t_driver). This has a PUBLIC property accessor called Driver

My understanding of OO using the composition model is that when you want to
load up a car class, you would access the DB to get Color, make, Model,
Year, DriverID and load the Car class. When the PUBLIC property Driver is
accessed, it would use the PRIVATE property DriverID to call the DB and load
the Driver class.

In this example, 2 separate DB calls would be made to load the Car and
Driver classes. I have DB/network people at work saying that these calls
should be done at once to cut down on network traffic and DB calls.

Any comments as to why my way is not OO sound or firepower I could use to
tell to my peers?

Thanks so much!

解决方案

TS <ma**********@nospam.nospamwrote:

Say i have a class car with properties: Color, Make, Model, Year, DriverID
And a Driver class with properties: DriverID, Name

The driverID PRIVATE property is the id of the driver from say a driver
table (t_driver). This has a PUBLIC property accessor called Driver

My understanding of OO using the composition model is that when you want to
load up a car class, you would access the DB to get Color, make, Model,
Year, DriverID and load the Car class. When the PUBLIC property Driver is
accessed, it would use the PRIVATE property DriverID to call the DB and load
the Driver class.

In this example, 2 separate DB calls would be made to load the Car and
Driver classes. I have DB/network people at work saying that these calls
should be done at once to cut down on network traffic and DB calls.

Any comments as to why my way is not OO sound or firepower I could use to
tell to my peers?

Depending on the ORM system involved, you can often eagerly fetch
things - issue a query which will load the car and the driver at the
same time using a join.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Hi,

I think you should take different strategies depending on the amount of
records a database query to the table t_car returns.

If a database query to the table t_car returns a great deal of records a
time, I think you''d better load a Car object for each record and leave the
Driver property in the Car object aside. As you said, when the public
property Driver is accessed, use the private property DriverID to access
the database and load a Driver object.

In the above scenario, if you load the Driver object when you load the Car
object, the network traffic would be very heavy because there''re a great
deal of Car objects after the query and you are going to load a Driver
object for each Car object.

I don''t think you should use a join to query the database in this instance
either, because the join operation will consume a lot of resources as well.

The advantage of loading a Driver object when the public property Driver is
accessed is that this avoids the amount of querying database being too much
a time.

On the other hand, if you query the table t_car and get one record a time,
you could load a Car object for the record and then access the table
t_driver by the value of DriverID field in the Car object and load a Driver
object. This won''t cause network traffic being heavy.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Linda Liu [MSFT] <v-****@online.microsoft.comwrote:

I think you should take different strategies depending on the amount of
records a database query to the table t_car returns.

If a database query to the table t_car returns a great deal of records a
time, I think you''d better load a Car object for each record and leave the
Driver property in the Car object aside. As you said, when the public
property Driver is accessed, use the private property DriverID to access
the database and load a Driver object.

In the above scenario, if you load the Driver object when you load the Car
object, the network traffic would be very heavy because there''re a great
deal of Car objects after the query and you are going to load a Driver
object for each Car object.

I don''t think you should use a join to query the database in this instance
either, because the join operation will consume a lot of resources as well.

You think it''s better to do 501 queries to retrieve 500 cars rather
than 1 which has a join? I have to disagree. If you''re pretty sure
you''re going to need to use the driver, then joining the two tables is
the logical approach IMO.

The advantage of loading a Driver object when the public property Driver is
accessed is that this avoids the amount of querying database being too much
a time.

On the other hand, if you query the table t_car and get one record a time,
you could load a Car object for the record and then access the table
t_driver by the value of DriverID field in the Car object and load a Driver
object. This won''t cause network traffic being heavy.

It''ll cause far more queries though. I would have thought that would
actually mean heavier network traffic than fewer queries returning the
same total amount of data in effectively bigger chunks.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于面向对象的方法在数据库调用方面效率低下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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