

# 説明プランを使用して Babelfish クエリのパフォーマンスを向上させる
<a name="working-with-babelfish-usage-notes-features.using.explain"></a>

バージョン 2.1.0 以降、Babelfish には、PostgreSQL オプティマイザを透過的に使用して、TDS ポート上で T-SQL クエリの推定および実際のクエリプランを生成する 2 つの関数が含まれています。これらの関数は、SQL Server データベースで SET STATISTICS PROFILE または SET SHOWPLAN\$1ALL を使用して、実行速度が遅いクエリを識別して改善するのと同様です。

**注記**  
関数、制御フロー、およびカーソルからのクエリプランの取得は、現在サポートされていません。

この表では、SQL Server、Babelfish、および PostgreSQL のクエリプランの説明関数を比較できます。


|  SQL Server  | Babelfish  | PostgreSQL  | 
| --- | --- | --- | 
| SHOWPLAN\$1ALL  | BABELFISH\$1SHOWPLAN\$1ALL  | EXPLAIN  | 
| STATISTICS PROFILE  | BABELFISH\$1STATISTICS PROFILE  | EXPLAIN ANALYZE  | 
| SQL Server オプティマイザを使用する  | PostgreSQL オプティマイザを使用する  | PostgreSQL オプティマイザを使用する  | 
| SQL Server の入力および出力形式  | SQL Server の入力および PostgreSQL 出力形式  | PostgreSQL の入力および出力形式  | 
| セッションに設定  | セッションに設定  | 特定のステートメントに適用する  | 
| 以下をサポートしています。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/AuroraUserGuide/working-with-babelfish-usage-notes-features.using.explain.html)  | 以下をサポートしています。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/AuroraUserGuide/working-with-babelfish-usage-notes-features.using.explain.html)  | 以下をサポートしています。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/AuroraUserGuide/working-with-babelfish-usage-notes-features.using.explain.html)  | 

Babelfish 関数を次のように使用します。
+ BABELFISH\$1SHOWPLAN\$1ALL [ON\$1OFF] — ON に設定すると、推定されたクエリ実行プランが生成されます。この関数は PostgreSQL `EXPLAIN` コマンドの動作を実装します。このコマンドを使用して、特定のクエリの説明プランを取得します。
+ SET BABELFISH\$1STATISTICS PROFILE [ON\$1OFF] — 実際のクエリ実行プランの場合は ON に設定します。この関数は PostgreSQL の `EXPLAIN ANALYZE` コマンドの動作を実装します。

PostgreSQL `EXPLAIN` および `EXPLAIN ANALYZE` の詳細については、PostgreSQL ドキュメントの [EXPLAIN](https://www.postgresql.org/docs/current/sql-explain.html) を参照してください。

**注記**  
バージョン 2.2.0 以降、`escape_hatch_showplan_all` パラメータを *ignore* に設定して、`SHOWPLAN_ALL` と `STATISTICS PROFILE` SET コマンドの SQL Server 構文で *BABELFISH\$1* プレフィックスが使用されないようにできます。

例えば、次のコマンドシーケンスは、クエリプランをオンにし、クエリを実行せずに SELECT ステートメントの推定クエリ実行プランを返します。この例では、`sqlcmd` コマンドラインツールを使って SQL Server のサンプル `northwind` データベースを使用し、TDS ポートをクエリします。

```
1> SET BABELFISH_SHOWPLAN_ALL ON
2> GO
1> SELECT t.territoryid, e.employeeid FROM
2> dbo.employeeterritories e, dbo.territories t
3> WHERE e.territoryid=e.territoryid ORDER BY t.territoryid;
4> GO

QUERY PLAN                                                                                                                                                                                                                                      
------------------------------------------------------------------------------------                                                                                                              
Query Text: SELECT t.territoryid, e.employeeid FROM
dbo.employeeterritories e, dbo.territories t
WHERE e.territoryid=e.territoryid ORDER BY t.territoryid
Sort  (cost=6231.74..6399.22 rows=66992 width=10)
  Sort Key: t.territoryid NULLS FIRST
  ->  Nested Loop  (cost=0.00..861.76 rows=66992 width=10)  
        ->  Seq Scan on employeeterritories e  (cost=0.00..22.70 rows=1264 width=4)
              Filter: ((territoryid)::"varchar" IS NOT NULL)
        ->  Materialize  (cost=0.00..1.79 rows=53 width=6)
              ->  Seq Scan on territories t  (cost=0.00..1.53 rows=53 width=6)
```

クエリの確認と調整が完了したら、次に示すように関数をオフにします。

```
1> SET BABELFISH_SHOWPLAN_ALL OFF
```

BABELFISH\$1STATISTICS PROFILE を ON に設定すると、実行された各クエリは通常の結果セットと、それに続く実際のクエリ実行プランを示す追加の結果セットを返します。Babelfish は、SELECT ステートメントを呼び出すときに最速の結果セットを提供するクエリプランを生成します。

```
1> SET BABELFISH_STATISTICS PROFILE ON
1>
2> GO
1> SELECT e.employeeid, t.territoryid FROM
2> dbo.employeeterritories e, dbo.territories t
3> WHERE t.territoryid=e.territoryid ORDER BY t.territoryid;
4> GO
```

結果セットとクエリプランが返されます (この例に表示されているのはクエリプランのみです)。

```
QUERY PLAN                                                                                                                                                                                                                                
---------------------------------------------------------------------------
Query Text: SELECT e.employeeid, t.territoryid FROM
dbo.employeeterritories e, dbo.territories t
WHERE t.territoryid=e.territoryid ORDER BY t.territoryid
Sort  (cost=42.44..43.28 rows=337 width=10)
  Sort Key: t.territoryid NULLS FIRST                                                                                                                                               
  ->  Hash Join  (cost=2.19..28.29 rows=337 width=10)
       Hash Cond: ((e.territoryid)::"varchar" = (t.territoryid)::"varchar")
        ->  Seq Scan on employeeterritories e  (cost=0.00..22.70 rows=1270 width=36)
        ->  Hash  (cost=1.53..1.53 rows=53 width=6)
             ->  Seq Scan on territories t  (cost=0.00..1.53 rows=53 width=6)
```

クエリと PostgreSQL オプティマイザによって返される結果を分析する方法の詳細については、「[explain.depesz.com](https://www.depesz.com/2013/04/16/explaining-the-unexplainable/)」を参照してください。PostgreSQL EXPLAIN および EXPLAIN ANALYZE の詳細については、PostgreSQL ドキュメントの「[EXPLAIN](https://www.postgresql.org/docs/current/sql-explain.html)」を参照してください。

## Babelfish 説明オプションを制御するパラメータ
<a name="working-with-babelfish-usage-notes-features.using.explain.parameters"></a>

次の表に示すパラメータを使用して、クエリプランに表示される情報のタイプを制御できます。


| パラメータ  | 説明 | 
| --- | --- | 
| babelfishpg\$1tsql.explain\$1buffers | オプティマイザのバッファ使用状況情報をオン (またはオフ) にするブール値。(デフォルト: off) (許容値: off、on)  | 
| babelfishpg\$1tsql.explain\$1costs | オプティマイザの推定起動および総コスト情報をオン (またはオフ) にするブール値。(デフォルト: on) (許容値: off、on)  | 
| babelfishpg\$1tsql.explain\$1format | `EXPLAIN` プランの出力形式を指定します。(デフォルト: text) (許容値: text, xml, json, yaml)  | 
| babelfishpg\$1tsql.explain\$1settings | 構成パラメータに関する情報を EXPLAIN プランの出力に含めることをオン (またはオフ) するブール値。(デフォルト: off) (許容値: off、on)  | 
| babelfishpg\$1tsql.explain\$1summary | クエリプランの後の合計時間などのサマリー情報をオン (またはオフ) にするブール値。(デフォルト: on) (許容値: off、on)  | 
| babelfishpg\$1tsql.explain\$1timing | 出力の各ノードでの実際の起動時間と滞在時間をオン (またはオフ) にするブール値。(デフォルト: on) (許容値: off、on)  | 
| babelfishpg\$1tsql.explain\$1verbose | 説明プランの最も詳細なバージョンをオン (またはオフ) にするブール値。(デフォルト: off) (許容値: off、on)  | 
| babelfishpg\$1tsql.explain\$1wal | 説明プランの一部として WAL レコード情報の生成をオン (またはオフ) にするブール値。(デフォルト: off) (許容値: off、on)  | 

PostgreSQL クライアントまたは SQL Server クライアントを使用して、システム上の BabelFish 関連パラメータの値を確認できます。次のコマンドを実行して、現在のパラメータ値を取得します。

```
1> execute sp_babelfish_configure '%explain%';
2> GO
```

次の出力では、この特定の Babelfish DB クラスターのすべての設定がデフォルト値になっていることがわかります。この例ではすべての出力が表示されているわけではありません。

```
             name                   setting                     short_desc
---------------------------------- -------- --------------------------------------------------------
babelfishpg_tsql.explain_buffers   off      Include information on buffer usage
babelfishpg_tsql.explain_costs     on       Include information on estimated startup and total cost
babelfishpg_tsql.explain_format    text     Specify the output format, which can be TEXT, XML, JSON, or YAML
babelfishpg_tsql.explain_settings  off      Include information on configuration parameters
babelfishpg_tsql.explain_summary   on       Include summary information (e.g.,totaled timing information) after the query plan 
babelfishpg_tsql.explain_timing    on       Include actual startup time and time spent in each node in the output
babelfishpg_tsql.explain_verbose   off      Display additional information regarding the plan
babelfishpg_tsql.explain_wal       off      Include information on WAL record generation

(8 rows affected)
```

次の例に示すように、`sp_babelfish_configure` を使用して、これらのパラメータの設定を変更できます。

```
1> execute sp_babelfish_configure 'explain_verbose', 'on';
2> GO
```

クラスター全体のレベルで設定を永続化するには、次の例に示されているように、 キーワード *server* を含めます。

```
1> execute sp_babelfish_configure 'explain_verbose', 'on', 'server';
2> GO
```