简体   繁体   English

使用linq时具有多个数据库连接的最简单方法是什么

[英]what is the easiest way to have multiple db connections when using linq

Optimally I would like linq to use X database connections instead of 1 to speed things up. 理想情况下,我希望linq使用X数据库连接而不是1以加快处理速度。

The only way now is to open X connections, and create X databasecontexts, and associate them. 现在唯一的方法是打开X个连接,创建X个数据库上下文,并将它们关联。 It would be easier if somehow I could tell linq to use X connections. 如果我能以某种方式告诉linq使用X连接会更容易。

Is this possible? 这可能吗? Or is there another way to speed up database queries? 还是有另一种方法来加快数据库查询?

thanks 谢谢

edit: changed the title since it was misleading (according to one of the answerers, and I agree) 编辑:更改标题,因为它具有误导性(根据答复者之一,我同意)

I think the title is misleading. 我认为标题有误导性。 Connection Pooling: You don't need to do that. 连接池:您不需要这样做。 Linq2SQL uses an ADO.NET SqlConnection under the hood which is connection pooled by default. Linq2SQL在默认情况下已连接池的内部使用ADO.NET SqlConnection

What you are trying to accomplish seems to me like trying to query the database in parallel. 在我看来,您想要完成的工作就像在尝试并行查询数据库。 This can only beneficial if you are trying to execute different queries or a query that can be very well parallelized. 仅当您尝试执行不同的查询或可以很好并行化的查询时,这才有用。 Such a query also should not heavily rely on locks/transaction. 这样的查询也不应过分依赖锁/事务。 Slow queries generally tend not to be of that kind. 缓慢的查询通常不属于这种类型。

Loading the top 50% of the same query with one connection and the bottom 50% with another won't bring you any benefits, the Database Server is the bottleneck there. 使用一个连接加载同一查询的前50%,而使用另一连接加载后50%不会给您带来任何好处,数据库服务器是其中的瓶颈。

There are a lot of other reasons why LinqToSql SQL queries perform bad. LinqToSql SQL查询执行错误的原因还有很多。 One of them is known as the Select N+1 problem. 其中之一被称为“选择N + 1”问题。 Ayende has a great post about fighting it in NHibernate, equivalent principles apply in L2S. Ayende在NHibernate中有一篇出色的文章 ,它在L2S中使用了等效的原理。

If all of the above does not help you, you might want to take a look at SQL Server MARS that allows you to query a database in parallel on one connection. 如果以上所有内容均无济于事,您可能想看看SQL Server MARS ,它使您可以在一个连接上并行查询数据库。

暂无
暂无

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

相关问题 使用LINQ to SQL用尽数据库连接 - Running out of DB connections using LINQ to SQL 在linq中将DateTime与sql server ce进行比较的最简单方法是什么? - What is the easiest way to compare DateTime in linq with sql server ce? 当我们可以有数百个对象时,在 C# 中初始化多个实例及其属性的最简单方法是什么? - What is the easiest way to initialize mutliple instances and its properties in C#, when we could have hundreds of objects? 固定 pivot 在 C#(linq?)中转换此数据的最简单方法是什么? - What is the easiest way to fixed pivot transform this data in C# (linq?)? 使LINQ一次处理所有收集的最简单方法是什么? - What it the easiest way to make LINQ to process all collection at once? 使用 .net 检查事件是否已记录在事件日志中的最简单方法是什么? - What is the easiest way using .net to check if events have been logged in the eventlog? 使用LINQ to XML更改命名空间值的最简单方法是什么? - What's the easiest method to change the namespace value using LINQ to XML? 在Winforms中,有一个“矩阵”(单元格行)输入控件的最简单方法是什么? - In Winforms what is the easiest way to have a “matrix” (rows of cells) input control? 如果我有字符和长度,那么构建字符串的最简单方法是什么 - What is the easiest way to build a string if i have the character and length 在 .NET 中进行定时回调的最简单方法是什么? - What's the easiest way to have a timed callback in .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM