TimesTenアプリケーションでは、SQL文を1回のみ準備してそれを複数回実行するほうが、実行のたびに文を繰り返し準備するより効率的です。ttIsqlには、準備済のSQL文を使用するための一連の組込みコマンドが用意されています。次に、これらのコマンドの概要を示します。
ttIsqlユーティリティの準備済の文コマンドは、SQL文のパラメータ・マーカーも処理します。準備済のSQL文にパラメータ・マーカーが含まれている場合は、ttIsqlの実行時に、文の各パラメータに値を入力するように自動的に求められます。
次の例では、ttIsqlの準備済の文コマンドを使用して、INTEGERおよびCHAR列を含む表のINSERT文を準備します。文は、準備された後、文の2つのパラメータに対してそれぞれ異なる値を使用して2回実行されます。また、ttIsqlのtimingコマンドを使用して、各コマンドに関連付けられている主要ODBC関数コールの実行に必要な経過時間を表示します。
Command> connect "DSN=MY_DSN;Overwrite=1"; Connection successful: DSN=MY_DSN;DataStore=E:\ds\MY_DSN;Overwrite=1;DRIVER=E:\WINNT\Sys tem32\TTdv60.dll; (Default setting AutoCommit=1) Command> timing 1; Command> CREATE TABLE T1 (KEY INTEGER NOT NULL PRIMARY KEY, VALUE CHAR (64)); Execution time (SQLExecDirect) = 0.1337 seconds. Command> prepare INSERT INTO T1 VALUES (?,?); Execution time (SQLPrepare) = 0.0668 seconds. Command> exec; All parameter values must be terminated with a semicolon character. Type '?;' for help on entering parameter values. Type '*;' to abort the parameter entry process. Enter Parameter 1 (INTEGER) >1; Enter Parameter 2 (CHAR) >'abc'; 1 row inserted. Execution time (SQLExecute) = 0.0757 seconds. Command> exec; All parameter values must be terminated with a semicolon character. Type '?;' for help on entering parameter values. Type '*;' to abort the parameter entry process. Enter Parameter 1 (INTEGER) >2; Enter Parameter 2 (CHAR) >'def'; 1 row inserted. Execution time (SQLExecute) = 0.0306 seconds. Command> free; Command> SELECT * FROM T1; < 1, abc > < 2, def > 2 rows found. Execution time (SQLExecDirect) = 0.0316 seconds. Command> disconnect; Disconnecting... Execution time (SQLDisconnect) = 1.7091 seconds. Command> Command>前述の例では、準備するSQL文が準備済のコマンドの直後に指定されています。ttIsqlでSQL文を準備すると、常に一意のコマンドIDが準備済の文に割り当てられます。ttIsqlでは、複数の準備済の文の追跡にこのIDが使用されます。ttIsqlセッションには、最大256の準備済の文が同時に存在できます。freeコマンドを実行すると、準備済のSQL文に対するコマンドIDの関連付けが自動的に解除されます。
準備済の文コマンドを使用した場合にttIsqlによって生成されるコマンドIDを表示するには、文を準備する前にverbosityコマンドを使用して冗長性レベルを4に設定するか、またはdescribe *コマンドを使用して準備済のすべての文をそれらのIDとともに表示します。
コマンドIDは、ttIsqlの準備済の文コマンドを使用する場合に明示的に参照できます。ttIsqlの準備済の文コマンドの構文の詳細は、『Oracle TimesTen In-Memory Database APIおよびSQLリファレンス・ガイド』を参照するか、またはttIsqlコマンド・プロンプトでhelpと入力してください。
次の例では、1つのINTEGERパラメータを含む述語が指定されているSELECT文を準備および実行します。fetchoneコマンドは、この文によって生成された結果行をフェッチするために使用します。showplanコマンドは、この文の実行時にTimesTen問合せオプティマイザで使用される実行計画を表示するために使用します。また、冗長性レベルは、準備済の文を追跡するためにttIsqlで使用されるコマンドIDが表示されるように4に設定します。
Command> connect "DSN=MY_DSN;Overwrite=1"; Connection successful: DSN=MY_DSN;DataStore=E:\ds\MY_DSN;Overwrite=1;DRIVER=E:\WINNT\Sys tem32\TTdv60.dll; (Default setting AutoCommit=1) The command succeeded. Command> CREATE TABLE T1 (KEY INTEGER NOT NULL PRIMARY KEY, VALUE CHAR (64)); The command succeeded. Command> INSERT INTO T1 VALUES (1, 'abc'); 1 row inserted. The command succeeded. Command> autocommit 0; The command succeeded. Command> showplan 1; The command succeeded. Command> verbosity 4; The command succeeded. Command> prepare SELECT * FROM T1 WHERE KEY=?; Assigning new prepared command id = 0. Query Optimizer Plan: STEP: 1 LEVEL: 1 OPERATION: RowLkHashScan TBLNAME: T1 IXNAME: T1 PRED: T1.KEY = qmark_1 OTHERPRED: <NULL> The command succeeded. Command> exec; Executing prepared command id = 0. All parameter values must be terminated with a semicolon character. Type '?;' for help on entering parameter values. Type '*;' to abort the parameter entry process. Enter Parameter 1 (INTEGER) >1; The command succeeded. Command> fetchone; Fetching prepared command id = 0. < 1, abc > 1 row found. The command succeeded. Command> close; Closing prepared command id = 0. The command succeeded. Command> free; Freeing prepared command id = 0. The command succeeded. Command> commit; The command succeeded. Command> disconnect; Disconnecting... The command succeeded. Command>