Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

c# - Why is Linq query setting my Arithabort options to false?

I have code like this:

using (var db = new MyDataContext()) {
  db.ExecuteStoreCommand("Set Arithabort on");
  var q = AFairlyComplexQuery(db); // returns an IQueryable<>
  var result = q.ToList();  // Line 4
  return result;
}

I was finding that this query was timing out. I ran SQL Profiler and grabbed the SQL and ran it in SSMS, and it came back in 7 seconds. From past experience, I've learned that this is invariably being caused by the Arithabort option being set to 0, which is why I run that first command. But it's still timing out.

I put a breakpoint on Line 4. When I hit the breakpoint, I went to SSMS, and ran the following query:

SELECT arithabort, * FROM sys.dm_exec_sessions s
WHERE program_name LIKE 'MyProg%'

As expected, Arithabort is set to 1. Then I stepped over Line 4, and immediately went back to SSMS to run that query... and suddenly Arithabort has been set back to 0!

Why? How to fix this?

EDIT: Well, I found a workaround, and it's more or less an answer, but not very satisfying.

using (var db = new MyDataContext()) {
  db.Connection.Open(); // INSERTING THIS LINE PRESERVES ARITHABORT
  db.ExecuteStoreCommand("Set Arithabort on");
  var q = AFairlyComplexQuery(db); // returns an IQueryable<>
  var result = q.ToList();  // Line 4
  return result;
}

I inserted a line db.Connection.Open(). Now Arithabort maintains its original value. But that doesn't explain (a) why this is, and (b) why it's still running roughly 10 times longer than it takes in SSMS...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems to be a known issue that EF sets Arithabort off. And for some reason, ExecuteStoreCommand doesn't work, while the following code does work:

var cmd = ((EntityConnection)db.Connection).StoreConnection.CreateCommand();
cmd.Connection.Open();
cmd.CommandText = "set arithabort on";
cmd.ExecuteNonQuery();

See this answer to this question for more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...