Apex から SOQL クエリを実行する場合、アプリケーションサーバーの CPU 時間を消費しない操作 (データベースでのクエリの実行時間など) は、Apex CPU 時間制限の計算に含まれません。
一方で、クエリ結果をアプリケーションサーバーで処理する時間は Apex CPU 時間制限の計算に含まれます。
詳細については Apex ガバナ制限 を参照してください。
一括処理ジョブの開始中に取得される関連クエリまたはサブクエリのデータ量によって、SOQL クエリがアプリケーションサーバーの Apex CPU 時間制限に大きく影響を与える場合があります。
複雑なリレーションを持つ SOQL クエリの例を次に示します。一括処理ジョブの大量のデータと組み合わせると、Apex CPU 時間に大きな影響を与え、制限を超える原因になる可能性があります。
下記の例では、複数のリレーション (参照関係項目) を横断して、関連する複数の子オブジェクトの項目を start メソッド内で直接クエリします。
SELECT
child1__r.Id, child1__r.Name,
child2__r.Id, child2__r.Name, child2__r.Status__c,
child2__r.abc__c, child2__r.xyz__c, child2__r.Date__c,
child2__r.CurrencyIsoCode, child2__r.text__c, child2__r.Birthdate__c,
child3__r.Id, child3__r.Name, child3__r.Status__c, child3__r.Date__c,
child3__r.CurrencyIsoCode, child3__r.text__c
FROM Parent__c
WHERE
CreatedDate >= :date OR child1__r.CreatedDate > :date OR child2__r.CreatedDate > :date
また下記の例では、開始メソッド内でサブクエリ(子リレーションクエリ)を使用しており、同様にバッチの関連レコード全体に負荷を与えます。
SELECT
Id, Name,
(SELECT Id, Name, Status__c, Date__c, CurrencyIsoCode, text__c FROM Child__r)
FROM Parent__c
WHERE
CreatedDate >= :date OR child1__r.CreatedDate > :date OR child2__r.CreatedDate > :date
Apex の CPU 時間制限を超えないように、バッチジョブの開始方法を再構築する方法について説明します。
リレーションまたはサブクエリの複雑さとデータ量が原因で一括処理ジョブの開始メソッドで Apex CPU time limit exceeded メッセージが表示された場合、処理を start メソッドにまとめず、execute メソッドの各バッチに分散します。
下記例では、start メソッドの SOQL クエリに関連項目やサブクエリを含めず、処理中のプライマリレコードの ID のみを取得します。
その後 execute メソッドで、現在処理中のバッチに含まれるプライマリレコードに関連する必要なレコードのみを SOQL クエリで取得します。
SELECT Id
FROM Parent__c
WHERE
CreatedDate >= :date OR child1__r.CreatedDate > :date OR child2__r.CreatedDate > :date
SELECT Name, Status__c, Date__c, CurrencyIsoCode, text__c
FROM Child__c
WHERE Parent__c IN :scope
002503080

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.