3

asp.net mvc Webアプリケーション内にfolloiwngメソッドがあり、データアクセスレイヤーとしてEntity Frameworkを使用しています:-

        public IEnumerable<AaaUserContactInfo> getcontactinfo(long[] id)
        {
var organizationsiteids = (from accountsitemapping in entities.AccountSiteMappings
where id.Any(accountid => accountsitemapping.ACCOUNTID == accountid)
select accountsitemapping.SITEID).ToList();

var usersdepts = from userdept in entities.UserDepartments
join deptdefinition in entities.DepartmentDefinitions on userdept.DEPTID equals deptdefinition.DEPTID

where organizationsiteids.Any(accountid => deptdefinition.SITEID == accountid)

var contactsinfos = from contactinfo in entities.AaaUserContactInfoes 
                                join userdept in usersdepts on  contactinfo.USER_ID equals userdept.USERID

                                 select contactinfo;

            return contactsinfos;

しかし、レコードの数が膨大な場合は、次のエラーが発生します:-

SQLステートメントの一部がネストされすぎています。クエリを書き直すか、より小さなクエリに分割します。説明:現在のWebリクエストの実行中に未処理の例外が発生しました。エラーとエラーがコードのどこで発生したかについての詳細は、スタックトレースを確認してください。

例外の詳細:System.Data.SqlClient.SqlException:SQLステートメントの一部がネストされすぎています。クエリを書き直すか、より小さなクエリに分割します。

ソースエラー:

現在のWebリクエストの実行中に、未処理の例外が生成されました。例外の発生源と場所に関する情報は、以下の例外スタックトレースを使用して識別できます。

スタックトレース:

[SqlException(0x80131904):SQLステートメントの一部がネストされすぎています。クエリを書き直すか、より小さなクエリに分割します。]
System.Data.SqlClient.SqlConnection.OnError(SqlException例外、ブール値breakConnection、アクション1完了、Int32タイムアウト、タスクとタスク、ブール値asyncWrite)+577 System.Data.SqlClient.SqlCommand .RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior runBehavior、Boolean returnStream、String method)+107 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior Behavior、String method)+288 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior Behavior)+1801 wrapCloseInAction) +388
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +82
System.Data.SqlClient.SqlDataReader.get_MetaData() +135
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6665229
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +6667096
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource




System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand、CommandBehavior Behavior)+689

返されるレコードの数が少ない場合、コードは正常に機能しますが、何が問題になる可能性がありますか?

4

3 に答える 3

3

上記のコメントの重複した質問への回答に従って、最初のクエリの where 句として次を試してください。これがすべての問題を引き起こしている可能性が最も高いためです。

where id.Contains(accountsitemapping.ACCOUNTID)

于 2013-01-06T18:17:38.230 に答える
1

そこにあるすべての結合を削除して、個別にルックアップを実行したくなるでしょう。

そのようにすると逆効果に思えるかもしれませんが、もしあなたが

var organizationsiteids = (from accountsitemapping in entities.AccountSiteMappings
where id.Any(accountid => accountsitemapping.ACCOUNTID == accountid)
select accountsitemapping.SITEID).ToList();

次に、それをメモリに保持し、そのコレクションをループして、それを使用して DB から残りの詳細を取得します。これにより、クエリが簡素化され、エラーが解決される可能性があります。

ただし、実際の質問への回答として、生成される SQL は、多くの結合条件を持つ舞台裏で SQL 結合を生成したり、SQL サーバーが処理するネストされたクエリを生成したりする可能性があります。

これを理解するためにできることは 2 つあります。

1) 使用されている SQL サーバーにアクセスできる場合は、SQL サーバー プロファイラー ツールを使用して、生成されている SQL コードを確認します。または2)(これを行う方法を頭の中で思い出すことはできません)が、EF/L2SまたはVisual Studioのデバッグ/出力ウィンドウにSQLを出力するために使用するものを取得します。

最後にもう 1 つ、やりたいことがあるかもしれません。

LinqPad ( http://www.linqpad.net/ ) をダウンロードし、それを使用してコード内のクエリを再現します。これにより、サンドボックスで Linq ステートメントを座って操作できるようになり、何が何であるかを理解するのに役立ちます。進んでいます。

生成された SQL を投稿していただけない限り (オプション 1 が推奨される方法です)、私は今晩インターネットからログオフしています :-)

于 2013-01-05T20:09:26.410 に答える