こんにちは、ねこです。

自称プログラマのおばちゃんのブログです。いろいろあるよねぇ~。

MCSA SQL Server 2016 /70-762/DMs 覚えらんねェー!

2019-12-27 07:54:38 | プログラム 勉強

【DMSとモニタリングに関する問題】

赤字は過去問から。

sys.dm_os_wait_stats
Returns information about all the waits encountered by threads that executed. You can use this aggregated view to diagnose performance issues with SQL Server and also with specific queries and batches.
実行したスレッドが遭遇したすべての待機に関する情報を返します。 この集計ビューを使用して、SQL Serverと特定のクエリおよびバッチのパフォーマンスの問題を診断できます。
*#48--[You need to identify previous situations where a modification has prevented queries from selecting data in tables.]

*#163--[You must monitor and optimize the SQL Server to maximize throughput, response time, and overall SQL performance. You need to examine delays in executed threads, including errors with queries and batches.]

A. Create a sys.dm_os_waiting_tasks query.
B. Create a sys.dm_exec_sessions query.
C. Create a PerformanceMonitor Data Collector Set.
D. Create a sys.dm_os_memory_objects query.
E. Create a sp_configure "˜max server memory' query.
F. Create a SQL Profiler trace.
G. Create a sys.dm_os_wait_stats query.
H. Create an Extended Event.

G
Create a SQL Profiler trace.
#49--[You need to monitor the SQL environment and capture the information about the processes that are causing the deadlocks. Captured information must be viewable as the queries are running.]
A. Create a sys.dm_os_waiting_tasks query.
B. Create a sys.dm_exec_sessions query.
C. Create a PerformanceMonitor Data Collector Set.
D. Create a sys.dm_os_memory_objects query.
E. Create a sp_configure "˜max server memory' query.
F. Create a SQL Profiler trace.
G. Create a sys.dm_os_wait_stats query.
H. Create an Extended Event.

F
sys.dm_exec_sessions
Returns one row per authenticated session on SQL Server. sys.dm_exec_sessions is a server-scope view that shows information about all active user connections and internal tasks. This information includes client version, client program name, client login time, login user, current session setting, and more. Use sys.dm_exec_sessions to first view the current system load and to identify a session of interest, and then learn more information about that session by using other dynamic management views or dynamic management functions.
SQL Serverで認証されたセッションごとに1行を返します。 sys.dm_exec_sessionsは、すべてのアクティブなユーザー接続と内部タスクに関する情報を表示するサーバースコープビューです。 この情報には、クライアントバージョン、クライアントプログラム名、クライアントログイン時間、ログインユーザー、現在のセッション設定などが含まれます。 sys.dm_exec_sessionsを使用して、最初に現在のシステム負荷を表示し、目的のセッションを特定してから、他の動的管理ビューまたは動的管理機能を使用して、そのセッションに関する詳細情報を学習します。
*#50--[You must monitor the SQL Server instances in real time and optimize the server to maximize throughput, response time, and overall SQL performance.]
A. Create a sys.dm_os_waiting_tasks query.
B. Create a sys.dm_exec_sessions query.
C. Create a PerformanceMonitor Data Collector Set.
D. Create a sys.dm_os_memory_objects query.
E. Create a sp_configure "˜max server memory' query.
F. Create a SQL Profiler trace.
G. Create a sys.dm_os_wait_stats query.
H. Create an Extended Event.

B
Activity Monitor session in Microsoft SQL Management Studio.
*#76 and #36[You need to evaluate schema locking issues, plan cache memory pressure points, and backup I/O problems.]
A. a System Monitor report
B. a sys.dm_tran_database_transaction dynamic management view query
C. an Extended Events session that uses Query Editor
D. an Activity Monitor session in Microsoft SQL Management Studio.

D
Create an Extended Event.
*#51--[You need to ensure that the performance of each instance is consistent for the same queried and query plans.]
A. Create a sys.dm_os_waiting_tasks query.
B. Create a sys.dm_exec_sessions query.
C. Create a PerformanceMonitor Data Collector Set.
D. Create a sys.dm_os_memory_objects query.
E. Create a sp_configure "˜max server memory' query.
F. Create a SQL Profiler trace.
G. Create a sys.dm_os_wait_stats query.
H. Create an Extended Event.

H
Create a Performance Monitor Data Collector Set
*#52--[You need to collect query performance data while minimizing the performance impact on the SQL Server.]
A. Create a sys.dm_os_waiting_tasks query.
B. Create a sys.dm_exec_sessions query.
C. Create a PerformanceMonitor Data Collector Set.
D. Create a sys.dm_os_memory_objects query.
E. Create a sp_configure "˜max server memory' query.
F. Create a SQL Profiler trace.
G. Create a sys.dm_os_wait_stats query.
H. Create an Extended Event.

C
sys.dm_os_memory_objects
Returns memory objects that are currently allocated by SQL Server. You can use sys.dm_os_memory_objects to analyze memory use and to identify possible memory leaks.
SQL Serverによって現在割り当てられているメモリオブジェクトを返します。 sys.dm_os_memory_objectsを使用して、メモリ使用量を分析し、メモリリークの可能性を特定できます。
*#53--[You need to create a baseline set of metrics to report how the computer running SQL Server operates under normal load. The baseline must include the resource usage associated with the server processes.]
A. Create a sys.dm_os_waiting_tasks query.
B. Create a sys.dm_exec_sessions query.
C. Create a PerformanceMonitor Data Collector Set.
D. Create a sys.dm_os_memory_objects query.
E. Create a sp_configure "˜max server memory' query.
F. Create a SQL Profiler trace.
G. Create a sys.dm_os_wait_stats query.
H. Create an Extended Event.

D


SET SHOWPLAN_ALL ON
*#149[You need to identify the cause by using detailed information about the Transact-SQL statements in the queries. The Transact-SQL statements must not run as part of the analysis.]
クエリ内のTransact-SQLステートメントに関する詳細情報を使用して、原因を特定する必要があります。 Transact-SQLステートメントは、分析の一部として実行しないでください。
SET SHOWPLAN_TEXT ON
*#171[You need to identify the cause by using detailed information about the Transact-SQL statements in the queries. The Transact-SQL statements must not run as part of the analysis.]
クエリ内のTransact-SQLステートメントに関する詳細情報を使用して、原因を特定する必要があります。 Transact-SQLステートメントは、分析の一部として実行しないでください。
sys.dm_exec_query_stats
*#33[You need to determine which query uses the most cumulative CPU.どのクエリが最も累積的なCPUを使用するかを決定する必要があります。]
(答)sys.dm_exec_query_stats と highest_cpu_queries.total_worker_time DESC

Returns aggregate performance statistics for cached query plans in SQL Server. The view contains one row per query statement within the cached plan, and the lifetime of the rows are tied to the plan itself. When a plan is removed from the cache, the corresponding rows are eliminated from this view.
SQL Serverのキャッシュされたクエリプランの集計パフォーマンス統計を返します。 ビューには、キャッシュされたプラン内のクエリステートメントごとに1つの行が含まれ、行の有効期間はプラン自体に関連付けられています。 プランがキャッシュから削除されると、対応する行はこのビューから削除されます。










ここから先はまだプレップで見たことがないです。
sys.dm_external_script_execution_stats
Returns one row for each type of external script request. The external script requests are grouped by the supported external script language. One row is generated for each registered external script functions. Arbitrary external script functions are not recorded unless sent by a parent process, such as
外部スクリプトリクエストのタイプごとに1行を返します。 外部スクリプトリクエストは、サポートされている外部スクリプト言語ごとにグループ化されます。 登録済みの外部スクリプト関数ごとに1行が生成されます。 次のような親プロセスから送信されない限り、任意の外部スクリプト関数は記録されません。
sys.dm_external_script_requests
Returns a row for each active worker account that is running an external script.
外部スクリプトを実行しているアクティブなワーカーアカウントごとに行を返します。
sys.dm_exec_cached_plan_dependent_objects
Returns a row for each Transact-SQL execution plan, common language runtime (CLR) execution plan, and cursor associated with a plan.
各Transact-SQL実行プラン、共通言語ランタイム(CLR)実行プラン、およびプランに関連付けられたカーソルの行を返します。
sys.dm_exec_cached_plans
Returns a row for each query plan that is cached by SQL Server for faster query execution. You can use this dynamic management view to find cached query plans, cached query text, the amount of memory taken by cached plans, and the reuse count of the cached plans.
クエリの実行を高速化するために、SQL Serverによってキャッシュされる各クエリプランの行を返します。 この動的管理ビューを使用して、キャッシュされたクエリプラン、キャッシュされたクエリテキスト、キャッシュされたプランが使用するメモリ量、キャッシュされたプランの再利用カウントを検索できます。
sys.dm_exec_compute_node_errors
Returns errors that occur on PolyBase compute nodes.
PolyBase計算ノードで発生したエラーを返します。
sys.dm_exec_compute_node_status
Holds additional information about the performance and status of all PolyBase nodes. Lists one row per node.
すべてのPolyBaseノードのパフォーマンスとステータスに関する追加情報を保持します。 ノードごとに1行をリストします。
sys.dm_exec_compute_nodes
Holds information about nodes used with PolyBase data management. It lists one row per node.
PolyBaseデータ管理で使用されるノードに関する情報を保持します。 ノードごとに1行がリストされます。
sys.dm_exec_connections
Returns information about the connections established to this instance of SQL Server and the details of each connection. Returns server wide connection information for SQL Server. Returns current database connection information for SQL Database.
SQL Serverのこのインスタンスに対して確立された接続に関する情報と各接続の詳細を返します。 SQL Serverのサーバー全体の接続情報を返します。 SQL Databaseの現在のデータベース接続情報を返します。
sys.dm_exec_cursors
Returns information about the cursors that are open in various databases.
さまざまなデータベースで開いているカーソルに関する情報を返します。
sys.dm_exec_describe_first_result_set
This dynamic management function takes a Transact-SQL statement as a parameter and describes the metadata of the first result set for the statement.
この動的管理関数は、Transact-SQLステートメントをパラメーターとして受け取り、ステートメントの最初の結果セットのメタデータを記述します。
sys.dm_exec_describe_first_result_set_for_object
This dynamic management function takes an @object_id as a parameter and describes the first result metadata for the module with that ID. The @object_id specified can be the ID of a Transact-SQL stored procedure or a Transact-SQL trigger. If it is the ID of any other object (such as a view, table, function, or CLR procedure), an error will be specified in the error columns of the result.
この動的管理関数は、パラメータとして@object_idを受け取り、そのIDを持つモジュールの最初の結果メタデータを記述します。 指定された@object_idは、Transact-SQLストアドプロシージャまたはTransact-SQLトリガーのIDです。 他のオブジェクト(ビュー、テーブル、関数、CLRプロシージャなど)のIDである場合、結果のエラー列にエラーが指定されます。
sys.dm_exec_distributed_request_steps
Holds information about all steps that compose a given PolyBase request or query. It lists one row per query step.
特定のPolyBaseリクエストまたはクエリを構成するすべてのステップに関する情報を保持します。 クエリステップごとに1行がリストされます。
sys.dm_exec_distributed_requests
Holds information about all requests currently or recently active in PolyBase queries.
PolyBaseクエリで現在または最近アクティブなすべてのリクエストに関する情報を保持します。
sys.dm_exec_distributed_sql_requests
Holds information about all SQL query distributions as part of a SQL step in the query. This view shows the data for the last 1000 requests; active requests always have the data present in this view.
クエリのSQLステップの一部として、すべてのSQLクエリ分布に関する情報を保持します。 このビューには、過去1000件のリクエストのデータが表示されます。 アクティブなリクエストには、常にこのビューにデータが存在します。
sys.dm_exec_dms_services
Holds information about all of the DMS services running on the PolyBase compute nodes. It lists one row per service instance.
PolyBase計算ノードで実行されているすべてのDMSサービスに関する情報を保持します。 サービスインスタンスごとに1行がリストされます。
sys.dm_exec_dms_workers
Holds information about all workers completing DMS steps.This view shows the data for the last 1000 requests and active requests; active requests always have the data present in this view.
DMSステップを完了するすべてのワーカーに関する情報を保持します。このビューには、最新の1000リクエストとアクティブリクエストのデータが表示されます。 アクティブなリクエストには、常にこのビューにデータが存在します。
sys.dm_exec_external_operations
Captures information about external PolyBase operations.
外部PolyBase操作に関する情報をキャプチャします。
sys.dm_exec_external_work
Returns information about the workload per worker, on each compute node.
各計算ノードで、ワーカーごとのワークロードに関する情報を返します。
sys.dm_exec_function_stats
Returns aggregate performance statistics for cached functions. The view returns one row for each cached function plan, and the lifetime of the row is as long as the function remains cached. When a function is removed from the cache, the corresponding row is eliminated from this view. At that time, a Performance Statistics SQL trace event is raised similar to sys.dm_exec_query_stats. Returns information about scalar functions, including in-memory functions and CLR scalar functions. Does not return information about table valued functions.
キャッシュされた関数の集計パフォーマンス統計を返します。 ビューは、キャッシュされた関数プランごとに1つの行を返します。行の有効期間は、関数がキャッシュされている限りです。 関数がキャッシュから削除されると、対応する行はこのビューから削除されます。 その時点で、sys.dm_exec_query_statsと同様のパフォーマンス統計SQLトレースイベントが発生します。 インメモリ関数やCLRスカラー関数などのスカラー関数に関する情報を返します。 テーブル値関数に関する情報を返しません。
sys.dm_exec_input_buffer
Returns information about statements submitted to an instance of SQL Server.
SQL Serverのインスタンスに送信されたステートメントに関する情報を返します。
sys.dm_exec_plan_attributes
Returns one row per plan attribute for the plan specified by the plan handle. You can use this table-valued function to get details about a particular plan, such as the cache key values or the number of current simultaneous executions of the plan.
プランハンドルで指定されたプランのプラン属性ごとに1行を返します。 このテーブル値関数を使用して、キャッシュキー値やプランの現在の同時実行数など、特定のプランに関する詳細を取得できます。
sys.dm_exec_procedure_stats
Returns aggregate performance statistics for cached stored procedures. The view returns one row for each cached stored procedure plan, and the lifetime of the row is as long as the stored procedure remains cached. When a stored procedure is removed from the cache, the corresponding row is eliminated from this view. At that time, a Performance Statistics SQL trace event is raised similar to sys.dm_exec_query_stats.
キャッシュされたストアドプロシージャの集計パフォーマンス統計を返します。 ビューは、キャッシュされたストアドプロシージャプランごとに1行を返します。行の有効期間は、ストアドプロシージャがキャッシュされている限りです。 ストアドプロシージャがキャッシュから削除されると、対応する行はこのビューから削除されます。 その時点で、sys.dm_exec_query_statsと同様のパフォーマンス統計SQLトレースイベントが発生します。
sys.dm_exec_query_memory_grants
Returns information about all queries that have requested and are waiting for a memory grant or have been given a memory grant. Queries that do not require a memory grant will not appear in this view. For example, sort and hash join operations have memory grants for query execution, while queries without an ORDER BY clause will not have a memory grant.
要求され、メモリ許可を待機している、またはメモリ許可が与えられているすべてのクエリに関する情報を返します。 メモリ許可を必要としないクエリは、このビューには表示されません。 たとえば、並べ替えおよびハッシュ結合操作にはクエリ実行用のメモリ許可がありますが、ORDER BY句のないクエリにはメモリ許可がありません。
sys.dm_exec_query_optimizer_info
Returns detailed statistics about the operation of the SQL Server query optimizer. You can use this view when tuning a workload to identify query optimization problems or improvements. For example, you can use the total number of optimizations, the elapsed time value, and the final cost value to compare the query optimizations of the current workload and any changes observed during the tuning process. Some counters provide data that is relevant only for SQL Server internal diagnostic use. These counters are marked as "Internal only."
SQL Serverクエリオプティマイザーの操作に関する詳細な統計を返します。 ワークロードを調整するときにこのビューを使用して、クエリ最適化の問題または改善点を特定できます。 たとえば、最適化の合計数、経過時間値、および最終コスト値を使用して、現在のワークロードのクエリ最適化とチューニングプロセス中に観察された変更を比較できます。 一部のカウンターは、SQL Serverの内部診断の使用にのみ関連するデータを提供します。 これらのカウンターは、「内部のみ」としてマークされています。
sys.dm_exec_query_optimizer_memory_gateways
Returns the current status of resource semaphores used to throttle concurrent query optimization.
同時クエリ最適化の調整に使用されるリソースセマフォの現在の状態を返します。
sys.dm_exec_query_plan
Returns the Showplan in XML format for the batch specified by the plan handle. The plan specified by the plan handle can either be cached or currently executing.
プランハンドルで指定されたバッチのShowplanをXML形式で返します。 プランハンドルで指定されたプランは、キャッシュすることも、現在実行中にすることもできます。
sys.dm_exec_query_parallel_workers
Returns worker availability information per node.
ノードごとにワーカーの可用性情報を返します。
sys.dm_exec_query_profiles
Monitors real time query progress while the query is in execution. For example, use this DMV to determine which part of the query is running slow. Join this DMV with other system DMVs using the columns identified in the description field. Or, join this DMV with other performance counters (such as Performance Monitor, xperf) by using the timestamp columns.
クエリの実行中にリアルタイムのクエリの進行状況を監視します。 たとえば、このDMVを使用して、クエリのどの部分の実行が遅いかを判断します。 説明フィールドで指定された列を使用して、このDMVを他のシステムDMVと結合します。 または、タイムスタンプ列を使用して、このDMVを他のパフォーマンスカウンター(パフォーマンスモニター、xperfなど)と結合します。
sys.dm_exec_query_resource_semaphores
Returns the information about the current query-resource semaphore status in SQL Server.
SQL Serverの現在のクエリリソースセマフォステータスに関する情報を返します。
sys.dm_exec_query_statistics_xml
Returns query execution plan for in-flight requests. Use this DMV to retrieve showplan XML with transient statistics.
実行中のリクエストのクエリ実行プランを返します。 このDMVを使用して、一時的な統計を含むshowplan XMLを取得します。
sys.dm_exec_query_stats
Returns aggregate performance statistics for cached query plans in SQL Server. The view contains one row per query statement within the cached plan, and the lifetime of the rows are tied to the plan itself. When a plan is removed from the cache, the corresponding rows are eliminated from this view.
SQL Serverのキャッシュされたクエリプランの集計パフォーマンス統計を返します。 ビューには、キャッシュされたプラン内のクエリステートメントごとに1つの行が含まれ、行の有効期間はプラン自体に関連付けられています。 プランがキャッシュから削除されると、対応する行はこのビューから削除されます。
sys.dm_exec_query_plan_stats
Returns the equivalent of the last known actual execution plan for a previously cached query plan.
以前にキャッシュされたクエリプランの最後の既知の実際の実行プランに相当するものを返します。
sys.dm_exec_requests
Returns information about each request that is executing in SQL Server.
SQL Serverで実行されている各要求に関する情報を返します。
sys.dm_exec_session_wait_stats
Returns information about all the waits encountered by threads that executed for each session. You can use this view to diagnose performance issues with the SQL Server session and also with specific queries and batches. This view returns session the same information that is aggregated for sys.dm_os_wait_stats (Transact-SQL) but provides the session_id number as well.
各セッションで実行されたスレッドが遭遇したすべての待機に関する情報を返します。 このビューを使用して、SQL Serverセッションおよび特定のクエリとバッチのパフォーマンスの問題を診断できます。 このビューは、sys.dm_os_wait_stats(Transact-SQL)について集約されたものと同じ情報をセッションに返しますが、session_id番号も提供します。
sys.dm_exec_text_query_plan
Returns the Showplan in text format for a Transact-SQL batch or for a specific statement within the batch. The query plan specified by the plan handle can either be cached or currently executing. This table-valued function is similar to sys.dm_exec_query_plan (Transact-SQL), but has the following differences:
  • The output of the query plan is returned in text format.
  • The output of the query plan is not limited in size.
  • Individual statements within the batch can be specified.

Transact-SQLバッチまたはバッチ内の特定のステートメントのショープランをテキスト形式で返します。 プランハンドルで指定されたクエリプランは、キャッシュするか、現在実行中のいずれかです。 このテーブル値関数はsys.dm_exec_query_plan(Transact-SQL)に似ていますが、次の違いがあります。
  • クエリプランの出力はテキスト形式で返されます。
  • クエリプランの出力のサイズに制限はありません。
  • バッチ内の個々のステートメントを指定できます。
sys.dm_exec_trigger_stats
Returns aggregate performance statistics for cached triggers. The view contains one row per trigger, and the lifetime of the row is as long as the trigger remains cached. When a trigger is removed from the cache, the corresponding row is eliminated from this view. At that time, a Performance Statistics SQL trace event is raised similar to sys.dm_exec_query_stats.
キャッシュされたトリガーの集計パフォーマンス統計を返します。 ビューにはトリガーごとに1つの行が含まれ、行の有効期間はトリガーがキャッシュされている限りです。 トリガーがキャッシュから削除されると、対応する行がこのビューから削除されます。 その時点で、ys.dm_exec_query_statsと同様のパフォーマンス統計SQLトレースイベントが発生します。
sys.dm_exec_valid_use_hints
Returns USE HINT supported hint names. It lists one hint name per row.Use this DMV to see the list of all supported hints under the USE HINT notation.
USE HINTがサポートするヒント名を返します。 行ごとに1つのヒント名がリストされます。このDMVを使用して、USE HINT表記でサポートされているすべてのヒントのリストを表示します。
sys.dm_exec_xml_handles
Returns information about active handles that have been opened by sp_xml_preparedocument.
sp_xml_preparedocumentによって開かれたアクティブなハンドルに関する情報を返します。
sys.dm_external_script_execution_stats
Returns one row for each type of external script request. The external script requests are grouped by the supported external script language. One row is generated for each registered external script functions. Arbitrary external script functions are not recorded unless sent by a parent process, such as
外部スクリプトリクエストのタイプごとに1行を返します。 外部スクリプトリクエストは、サポートされている外部スクリプト言語ごとにグループ化されます。 登録済みの外部スクリプト関数ごとに1行が生成されます。 次のような親プロセスから送信されない限り、任意の外部スクリプト関数は記録されません。
sys.dm_external_script_requests
Returns a row for each active worker account that is running an external script.
外部スクリプトを実行しているアクティブなワーカーアカウントごとに行を返します。
sys.dm_exec_sql_text
Returns the text of the SQL batch that is identified by the specified sql_handle. This table-valued function replaces the system function fn_get_sql.
指定されたsql_handleによって識別されるSQLバッチのテキストを返します。 このテーブル値関数は、システム関数fn_get_sqlを置き換えます。
 

MCSA SQL Server 2016 /70-762/CASCADE, Implicit transaction, Explicit taransaction

2019-12-27 05:16:51 | プログラム 勉強

インフルダウンしてたら、Cascade、やImplicit transaction、Explicit transactionがわからなくなってました。

【Cascade】

FOREIGN KEY (foreign_key_columns)
    REFERENCES parent_table(parent_key_columns)
    ON UPDATE action
    ON DELETE action;
 
--ON DELETE action
  • ON DELETE NO ACTION: SQL Server raises an error and rolls back the delete action on the row in the parent table.(SQL Serverはエラーを発生させ、親テーブルの行の削除アクションをロールバックします。)
  • ON DELETE CASCADE: SQL Server deletes the rows in the child table that is corresponding to the row deleted from the parent table.(SQL Serverは、親テーブルから削除された行に対応する子テーブルの行を削除します。)
  • ON DELETE SET NULL: SQL Server sets the rows in the child table to NULL if the corresponding rows in the parent table are deleted. To execute this action, the foreign key columns must be nullable.(親テーブルの対応する行が削除されると、SQL Serverは子テーブルの行をNULLに設定します。 このアクションを実行するには、外部キー列がNULL可能である必要があります。)
  • ON DELETE SET DEFAULT SQL Server sets the rows in the child table to their default values if the corresponding rows in the parent table are deleted. To execute this action, the foreign key columns must have default definitions. Note that a nullable column has a default value of NULL if no default value specified.(親テーブルの対応する行が削除された場合、SQLServerは子テーブルの行をデフォルト値に設定します。 このアクションを実行するには、外部キー列にデフォルトの定義が必要です。 デフォルト値が指定されていない場合、ヌル可能列のデフォルト値はNULLであることに注意してください。)

 --ON UPDATE action

  • ON UPDATE NO ACTION: SQL Server raises an error and rolls back the update action on the row in the parent table.(SQL Serverはエラーを発生させ、親テーブルの行の更新アクションをロールバックします。)
  • ON UPDATE CASCADE: SQL Server updates the corresponding rows in the child table when the rows in the parent table are updated.(SQL Serverは、親テーブルの行が更新されると、子テーブルの対応する行を更新します。)
  • ON UPDATE SET NULL: SQL Server sets the rows in the child table to NULL when the corresponding row in the parent table is updated. Note that the foreign key columns must be nullable for this action to execute.(SQL Serverは、親テーブルの対応する行が更新されると、子テーブルの行をNULLに設定します。 このアクションを実行するには、外部キー列がNULL可能でなければならないことに注意してください。)
  • ON UPDATE SET DEFAULT: SQL Server sets the default values for the rows in the child table that have the corresponding rows in the parent table updated.(SQL Serverは、更新された親テーブルの対応する行を持つ子テーブルの行にデフォルト値を設定します。)

 

【70-782からの問題】

*毎度グーグル翻訳ですんません。

さて、こんな問題がありました。病み上がりですっかりぱっぱらぱーになってました。

You are developing a new application that uses a stored procedure. The stored procedure inserts thousands of records as a single batch into the Employees table.
Users report that the application response time has worsened since the stored procedure was updated. You examine disk-related performance counters for the
Microsoft SQL Server instance and observe several high values that include a disk performance issue. You examine wait statistics and observe an unusually high
WRITELOG value.

ストアドプロシージャを使用する新しいアプリケーションを開発しています。 ストアドプロシージャは、何千ものレコードを単一のバッチとしてEmployeesテーブルに挿入します。ユーザーは、ストアドプロシージャが更新されてからアプリケーションの応答時間が悪化したと報告しています。 ディスク関連のパフォーマンスカウンターを調べます。Microsoft SQL Serverインスタンス。ディスクパフォーマンスの問題を含むいくつかの高い値を観察します。 待機統計を調べて、異常に高いWRITELOG値。
You need to improve the application response time.
アプリケーションの応答時間を改善する必要があります。

Solution: You update the application to use implicit transactions when connecting to the database.

解決策:データベースに接続するときに暗黙的なトランザクションを使用するようにアプリケーションを更新します。

Does the solution meet the goal?ソリューションは目標を達成していますか?

 

<答え>いいえ

 

では、↓を見てみましょう。


【暗黙的なトランザクション - Implicit transaction】

 In Implicit transaction mode, the instance of the SQL Server Database Engine automatically starts a new transaction after the current transaction is committed or rolled back for each DML statement. We do not need to define the start of a transaction; each statement level transaction will be committed or rolled back automatically. Implicit transaction can be turned on or off per batch basis. We can use the “SET IMPLICIT_TRANSACTIONS ON/OFF” statement to turn implicit transaction mode off. We can use the “COMMIT TRANSACTION” or “ROLLBACK TRANSACTION” statements to end each transaction.

暗黙的トランザクションモードでは、SQL Serverデータベースエンジンのインスタンスは、DMLステートメントごとに現在のトランザクションがコミットまたはロールバックされた後、自動的に新しいトランザクションを開始します。 トランザクションの開始を定義する必要はありません。 各ステートメントレベルのトランザクションは自動的にコミットまたはロールバックされます。 暗黙的なトランザクションは、バッチごとにオンまたはオフにできます。 「SET IMPLICIT_TRANSACTIONS ON / OFF」ステートメントを使用して、暗黙的なトランザクションモードをオフにすることができます。 「COMMIT TRANSACTION」または「ROLLBACK TRANSACTION」ステートメントを使用して、各トランザクションを終了できます。

【明示的なトランザクション - Explicit transaction】

An explicit transaction is one in which we explicitly define both the starting and ending of the transaction. To start an explicit transaction, we can use “BEGIN TRANSACTION” and then complete the transaction by using “COMMIT TRANSACTION” or “ROLLBACK TRANSACTION”.明示的なトランザクションとは、トランザクションの開始と終了の両方を明示的に定義するトランザクションです。 明示的なトランザクションを開始するには、「BEGIN TRANSACTION」を使用してから、「COMMIT TRANSACTION」または「ROLLBACK TRANSACTION」を使用してトランザクションを完了します。

【暗黙的・明示的の違い】

インジケータ

暗黙的なトランザクション

明示的なトランザクション

スコープ

ステートメントレベル

バッチレベル

ログ生成

トランザクションの連続チェーン

1つのトランザクションログエントリ

Log I/O

ステートメントごとに1つのI / Oを取得します

バッチ全体に1つのI / Oが必要です

WRITELOG Waittype

持続期間

N / Aまたは実ディスクの問題がない限り

応答/期間

いくつかのI / Oアクティビティのために長く

高速で少ないI / Oアクティビティ

T-Log Restore

長い、やり直し/元に戻すフェーズ

短いREDO / UNDOフェーズ

T-Logへの影響

非常に高い

少なくて取るに足らない

書き込み時間

長くて遅い

短くて速い

The application which is utilizing the Implicit Transaction mechanism actually makes the I/O system busy and degrades application response time dramatically. As a result, various disk related performance counters show significantly high values and we start believing that the disk is the bottleneck.

Implicit Transactionメカニズムを利用しているアプリケーションは、実際にI / Oシステムをビジーにし、アプリケーションの応答時間を劇的に低下させます。 その結果、さまざまなディスク関連のパフォーマンスカウンターが非常に高い値を示し、ディスクがボトルネックであると考え始めています。

 

 

。。。今思ったけど、こんなに詳しくノートとる必要なかったかもです。あぁ、はやく鼻と咳をとめたい。。。くらくらする。。。


MCSA SQL Server 2016 /70-762/UDFについて

2019-12-16 13:04:23 | プログラム 勉強

【User Definded Function Table】

いろいろあるよねぇー。。。覚えてますか?

こちらの70-761で勉強した内容も読んでみてください。

Microsoft MCSA SQL Server 2016 (70-761) むぼーな挑戦 6(Stored ProcedureとTable Valued Function)

でも、問題は使い分けです。

まずは、違いを。

 

【UDFs】
- Excuted within T-SQL(T-SQL内で実行)
- Does not support error handling(エラー処理をサポートしていません)
- Does not suppoort data changes(データの変更をサポートしていません)
- Supports input params only(入力パラメーターのみをサポート)
- Does not support dynamic SQL(動的SQLをサポートしていません)
- Used for computation(計算に使用)

大きく分けて二種類、テーブル値関数はリターンでそのままSELECT文を返すインライン値関数と自分で返すテーブルをデザインできる複数ステートメントテーブル値関数があります。

  1. スカラー関数
  2. テーブル値関数
  • インラインテーブル値関数(TABLEデータ型を返します。各関数は、1つのT-SQL文で構成されます。)
  • 複数ステートメントテーブル値関数(定義されたテーブルを返します。関数は、複数のT-SQL文で構成されます。)
 

【Stored Procedures】

- Uses EXECUTE( or EXEC)(EXECUTE(またはEXEC)を使用)
- Supoorts error handling(エラー処理をサポート)
- Supports data changes(データ変更をサポート)
- Supports iput and output params(入力および出力パラメーターをサポート)
- Supports dynamic SQL(動的SQLをサポート)
- Used for business logic(ビジネスロジックに使用)

こうくれば、もう一目瞭然。

これらによって使い分けします。

特に、前回の「AFTER TRIGGER」や「INSERT TRIGGER」などはストアードプロシージャと同じように設計できるので、テーブル側のイベントによって発動させたいビジネスロジックなどは、これが良いかもですね。

大事なことは、『トリガーは値を返しません。

A trigger is designed to check or change data based on a data modification or definition statement; it should't return data to the user. The Transact-SQL statements in a trigger frequently include control-of-flow language.(トリガーは、データの変更または定義ステートメントに基づいてデータをチェックまたは変更するように設計されています。 ユーザーにデータを返すべきではありません。 トリガー内のTransact-SQLステートメントには、多くの場合、フロー制御言語が含まれています。)

 

違い、70-761のテストでいっぱい出ました。

特に最初の「前のページにもどれないよ」っていう問題、全部で5~7問ありましたが、ほぼこれとインデックス関係でしたね。

CBT Nuggetビデオ、あと5つでおしまい。これは水曜日までに全部終わらせます。

この後はネット問題がっつりやって、プレップテスト買います。

 


MCSA SQL Server 2016 /70-762/トリガーについて、その二。まだわからんがな。

2019-12-16 10:29:11 | プログラム 勉強

【AFTER TRIGGER】

難しい。。。こんな問題があります。

HOTSPOT -
Note: This question is part of a series of questions that use the same scenario. For your convenience, the scenario is repeated in each question. Each question presents a different goal and answer choices, but the text of the scenario is exactly the same in each question in this series.
You have a database named Sales that contains the following database tables: Customer, Order, and Products. The Products table and the Order table are shown in the following diagram.(CustomerとOrderとProductsのテーブルがあります。)

*面倒なので、グーグル使って訳します。
The customer table includes a column that stores the data for the last order that the customer placed.
You plan to create a table named Leads. The Leads table is expected to contain approximately 20,000 records. Storage requirements for the Leads table must be minimized.(顧客テーブルには、顧客が行った最後の注文のデータを格納する列が含まれています。Leadsという名前のテーブルを作成する予定です。 Leadsテーブルには、約20,000件のレコードが含まれると予想されます。 Leadsテーブルのストレージ要件は最小限に抑える必要があります。)

You need to create triggers that meet the following requirements:(次の要件を満たすトリガーを作成する必要があります。)
✑ Optimize the performance and data integrity of the tables.(テーブルのパフォーマンスとデータの整合性を最適化する)
✑ Provide a custom error if a user attempts to create an order for a customer that does not exist.(ユーザーが存在しない顧客の注文を作成しようとした場合、カスタムエラーを提供します。)
✑ In the Customers table, update the value for the last order placed.(顧客テーブルで、最後に出された注文の値を更新します。)
✑ Complete all actions as part of the original transaction.(元のトランザクションの一部としてすべてのアクションを完了します。)


In the table below, identify the trigger types that meet the requirements.(次の表で、要件を満たすトリガータイプを特定しなさい。)
NOTE: Make only selection in each column. Each correct selection is worth one point.(各列でのみ選択を行います。 それぞれの正しい選択には1ポイントの価値があります。)
Hot Area:

さぁ~て、答えは何でしょう。。。

カギはここ。

「Provide custom」->(ユーザーが存在しない顧客の注文を作成しようとした場合、カスタムエラーを提供します。)
「Update Customer table」->(顧客テーブルで、最後に出された注文の値を更新します。

「AFTER」という言葉に騙されてはいけません!!!大事な部分は「INSERT」と「UPDATE」です。

なぜならトリガーの内容は変更が完結される前に「発動」するから。

詳しくはこちら。T-SQL AFTER TRIGGER トリガーの超簡単説明

 

<答え>*でもこれまちがってるよねー。『INSTEAD OF INSERT trigger』だと思います。どこにも答えがないから、何とも言えない。

INSTEAD OF INSERT triggers can be defined on a view or table to replace the standard action of the INSERT statement.
AFTER specifies that the DML trigger is fired only when all operations specified in the triggering SQL statement have executed successfully.(INSTEAD OF INSERT(トリガーをビューまたはテーブルで定義して、INSERTステートメントの標準アクションを置き換えることができます。AFTERは、トリガーSQLステートメントで指定されたすべての操作が正常に実行された場合にのみDMLトリガーが起動されることを指定します。)

References:
https://technet.microsoft.com/en-us/library/ms175089(v=sql.105).aspx

 

 

 


MCSA SQL Server 2016 /70-762/トリガーについて

2019-12-10 12:49:54 | プログラム 勉強

【AFTERトリガー】

sales.order itemsテーブルで作成され、このテーブルに対して更新が発生した後に起動するAFTERトリガーの例を次に示します。

CREATE TRIGGER Sales.OrderTotalTrigger
ON Sales.OrderItems
AFTER UPDATE
AS
DECLARE @NewOrderTotal decimal(18, 2);

SELECT @NewOrderTotal = SUM(oi.UnitPrice * oi.Quantity)
FROM Sales.OrderItems oi
JOIN inserted i oON oi.OrderID = i.OrderID
GROUP BY oi.OrderID;

UPDATE Sales.Orders
SET OrderTotal = @NewOrderTotal
FROM inserted
WHERE Sales.Orders.OrderID = inserted.OrderID;
GO

 

【INSTEAD OFトリガー】

更新可能なビューであり、複数のベーステーブルを対象とするビューのみに使用(ねこ的には、この更新可能なビューの意味がさっぱりわからんちん。ビューって、こうしんできへんやったやんなぁ。)

☝ わかた。。。

定義したビューに対してデータを変更することもできます。ビューはデータの入っていない仮想的なテーブルなので、実際にはビューの元になる実テーブルのデータが変更されることになります。また、ビューのデータの変更は実テーブルに定義されている制約を受けます。』ということです。

ありがとうございます。『TECKSCORE』様。

CREATE TRIGGER Production.UpdateableView

ON
Production.ItemsView
INSTEAD OF INSERT
AS
DECLARE @SupplierID int
SELECT @SupplierID = SupplierID FROM Purchasing.Suppliers s
JOIN inserted i ON s.SupplierName = i.Supplier;

INSERT INTO Production.Items (ItemName, SupplierID, UnitPrice)
SELECT ItemName, @SupplierID, UnitPrice FROM inserted;
GO

INSERT INTO Production.ItemsView VALUES ('New Item', 'Nod Publishers', 100)

 

【DDLトリガー(サーバ)】

CREATE TRIGGER dbo.ServerTrigger
ON ALL SERVER
FOR CREATE_DATABASE, ALTER_DATABASE, DROP_DATABASE
AS
SET NOCOUNT ON;

ROLLBACK;
THROW 50001, 'MOdifying databases is not allowed on this server.', 1;
GO

 

【DDLトリガー(データベース)】

 CREATE TRIGGER dbo.DatabaseTrigger

ON DATABASE
FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE
AS
SET NOCOUNT ON;

ROLLBACK;
THROW 50001, Modifying tables in not allowed in this databases.', 1;
GO

 

先ほどの「ビュー」の件、質問としてCBTNaggetsのコメントに残してます。だれか答えてくれればいいなぁ。