都内で働くSEの技術的なひとりごと / Technical soliloquy of System Engineer working in Tokyo

都内でサラリーマンやってます。SQL Server を中心とした (2023年からは Azure も。) マイクロソフト系(たまに、OSS系などマイクロソフト以外の技術も...)の技術的なことについて書いています。日々の仕事の中で、気になったことを技術要素関係なく気まぐれに選んでいるので記事内容は開発言語、インフラ等ばらばらです。なお、当ブログで発信、発言は私個人のものであり、所属する組織、企業、団体等とは何のかかわりもございません。ブログの内容もきちんと検証して使用してください。英語の勉強のため、英語の

Great feature additions about automatic synchronous statistics update

I was seeing "SQL Database Engine Blog", I found the following article.
blogs.msdn.microsoft.com

Following scenarios are assumed.

Consider the following query execution scenario:

You execute a SELECT query that triggers an automatic synchronous statistics update.
The synchronous statistics update begins execution and your query waits (is essentially blocked) until the fresh statistics are generated.
The query compilation and execution does not resume until the synchronous statistics update operation completes.

There was a time when the query suddenly became slow one day. but you do not know the cause unknown well. You were told by a customer, so you try to confirm without reproducibility. do you have experience like? Lol

Add wait type "WAIT_ON_SYNC_STATISTICS_REFRESH" for automatic statistics updates. Let's try it. Create non-clustered index on the AdventureWorks2017.Sales.SalesOrderHeader TaxAmt column.

f:id:koogucc11:20190505085846p:plain
non-clustered index of TaxAmt

Let's execute the following query in SQL Server Management Studio.

UPDATE
    TOP(10) PERCENT Sales.SalesOrderHeader
SET
    TaxAmt = (TaxAmt * 1)

f:id:koogucc11:20190505090101p:plain
Execution Result of UPDATE statement

Let's execute the following query in SQL Server Management Studio.

SELECT
    *
FROM
    Sales.SalesOrderHeader2
WHERE
    TaxAmt > 3000.00

f:id:koogucc11:20190505090525p:plain
Execution Result of SELECT statement

Let's execute the following query in SQL Server Management Studio.

SELECT
    *
FROM
    sys.dm_os_wait_stats
WHERE
    wait_type = 'WAIT_ON_SYNC_STATISTICS_REFRESH'

f:id:koogucc11:20190505090238p:plain
Execution Result of SELECT statement

"WAIT_ON_SYNC_STATISTICS_REFRESH" value has been updated. You can also determine that "STATMAN" works by checking with SQL Server Profiler.

f:id:koogucc11:20190505090958p:plain
Execution Result of SQL Server Profiler

Very Helpful. Thanx!

We believe these two small changes will help address a significant diagnostic gap. For feedback or questions, please reach out to us at IntelligentQP@microsoft.com.

いい機能追加を見つけたので、説明してみる

 ひさしぶりに SQL Database Engine Blog を眺めていて、下記の記事を見つけました。
blogs.msdn.microsoft.com

 こんなシナリオを想定しています。

Consider the following query execution scenario:

You execute a SELECT query that triggers an automatic synchronous statistics update.
The synchronous statistics update begins execution and your query waits (is essentially blocked) until the fresh statistics are generated.
The query compilation and execution does not resume until the synchronous statistics update operation completes.

 ある日突然クエリが遅くなった時があったけど、原因不明でよくわからないとかありますよね。お客さんから言われたので、確認してみたら再現性なし、みたいな経験ある人いますよね? 笑

 統計自動更新時の待ちの種類 WAIT_ON_SYNC_STATISTICS_REFRESH の追加です。それでは早速試してみましょう。AdventureWorks2017 の Sales.SalesOrderHeader の TaxAmt 列に非クラスタ化インデックスを作成します。

f:id:koogucc11:20190505085846p:plain
TaxAmt列を非クラスタ化インデックス

 下記のクエリを SQL Server Management Studio で実行します。

UPDATE
    TOP(10) PERCENT Sales.SalesOrderHeader
SET
    TaxAmt = (TaxAmt * 1)

f:id:koogucc11:20190505090101p:plain
Update の実行結果

 下記のクエリを SQL Server Management Studio で実行します。

SELECT
    *
FROM
    Sales.SalesOrderHeader2
WHERE
    TaxAmt > 3000.00

f:id:koogucc11:20190505090525p:plain
SELECT の実行結果

 下記のクエリを SQL Server Management Studio で実行します。

SELECT
    *
FROM
    sys.dm_os_wait_stats
WHERE
    wait_type = 'WAIT_ON_SYNC_STATISTICS_REFRESH'

f:id:koogucc11:20190505090238p:plain
WAIT_ON_SYNC_STATISTICS_REFRESH の実行結果

 WAIT_ON_SYNC_STATISTICS_REFRESH の値が更新されています。また、プロファイラーで確認することで STATMAN が動作したことも判断できますね。

f:id:koogucc11:20190505090958p:plain
プロファイラーの結果

 役立ちますね。助かります。

We believe these two small changes will help address a significant diagnostic gap. For feedback or questions, please reach out to us at IntelligentQP@microsoft.com.

 Apple Pencil (第二世代) を買うか悩み中。そうしたら、ケースも買わないと。

Apple Pencil(第2世代)

Apple Pencil(第2世代)

英文書くのに便利だなぁと思って紹介してみる

 今年から英語がかなり必要、というか生きていけない環境になりそうなので急遽勉強をはじめました。英単語の総復習には、milkan を使っています。

英単語アプリ mikan

英単語アプリ mikan

  • mikan Co.,Ltd.
  • 教育
  • 無料

 TOEIC も受験していきたいと思ったので、トレーニンTOEIC ® test も使用しています。

トレーニング TOEIC ® test

トレーニング TOEIC ® test

  • Flipout LLC
  • 教育
  • 無料

 上記以外には、自分のブログに投稿するのも日本語と英語の記事を投稿します。投稿時に英文法をチェックするのに使用しているのが、Ginger Page です。
www.getginger.jp

 使い方は非常に簡単です。下記の記事から、一文間違った状態でチェックにかけけてみます。
ryuchan.hatenablog.com

 赤字の間違いを仕込んだ文章です。

The end of HEISEI ( 平成 ), REIWA ( 令和 ) has begin. The last year of HEISEI was turning point in my life. However, I still want to evolve, so I want to move forward without fear of change.
I will explain about TRANSLATE (Transact-SQL) - SQL Server | Microsoft Docs. Complex processing in REPLACE function can be describe very simply by TRANSLATE function. Let's compare the description contents of REPLACE function and TRANSLATE function.

 まず、チェックする文章を貼り付け、Ginger it! をクリックします。

f:id:koogucc11:20190503131005p:plain
チェックする文章を貼り付け

 begin → begun に変更されています。

f:id:koogucc11:20190503131159p:plain
begin → begun

 describe → described に変更されています。

f:id:koogucc11:20190503131319p:plain
describe → described

 この機能を使い、投稿前に英文のチェックをしています。また、Sentence Rephaser という機能もあり、イケてる言い回し候補を提案する機能もあります。コツコツ勉強していこう。

Some useful SQL functions, syntax, and more - Part 18 ( REIWA GANNEN Memorial Post )

The end of HEISEI ( 平成 ), REIWA ( 令和 ) has begun. The last year of HEISEI was turning point in my life. However, I still want to evolve, so I want to move forward without fear of change.
I will explain about TRANSLATE (Transact-SQL) - SQL Server | Microsoft Docs. Complex processing in REPLACE function can be described very simply by TRANSLATE function. Let's compare the description contents of REPLACE function and TRANSLATE function.

Let's execute the following query in SQL Server Management Studio.

DECLARE @str NVARCHAR(255) = 'aaa,aaa,aaa|bbb,bbb,bbb'

SELECT
    REPLACE(REPLACE(REPLACE(@str,'|','*'),',','|'),'*',',') [REPLACE],
    TRANSLATE(@str, ',|', '|,') [TRANSLATE]

f:id:koogucc11:20190429083951p:plain
Execution result of REPLACE and TRANSLATE

The features of TRANSLATE function are as follows.

The behavior of the TRANSLATE function is similar to using multiple REPLACE functions. TRANSLATE does not, however, replace a character more than once. This is dissimilar to multiple REPLACE functions, as each use would replace all relevant characters.

In stored procedures, I often find that the RELACE function is nested in multiple layers. Although not available in all cases, use TRANSLATE function to refactor complex stored procedures.

I bought this magazine. I will do my best to study English!!

便利な SQL の関数とか、構文とか、その他色々まとめてみる - その18 ( 令和元年記念投稿 )

 平成も終わり、令和が始まりました。平成最後の年は、自分自身として人生の転機でした。ただ、まだまだ進化はしていきたいので、変化を恐れず進んでいきたいと思っています。
 今回は、TRANSLATE (Transact-SQL) - SQL Server | Microsoft Docs について説明します。一見、REPLACE (Transact-SQL) - SQL Server | Microsoft Docs 似ていますが、REPLACE では複雑になってしまう処理も TRANSLATE では非常に簡潔に記述することが可能です。それでは、REPLACE と TRANSLATE で記述内容を比較してみましょう。下記のクエリを SQL Server Management Studio で実行してみましょう。

DECLARE @str NVARCHAR(255) = 'aaa,aaa,aaa|bbb,bbb,bbb'

SELECT
    REPLACE(REPLACE(REPLACE(@str,'|','*'),',','|'),'*',',') [REPLACE],
    TRANSLATE(@str, ',|', '|,') [TRANSLATE]

f:id:koogucc11:20190429083951p:plain
REPLACE と TRANSLATE の実行結果

 TRANSLATE は下記の部分が特徴となります。

TRANSLATE 関数の動作は、複数の REPLACE 関数を使用した場合と似ています。 ただし、TRANSLATE では文字が複数回置き換えられることはありません。 これは、使用するたびに関連するすべての文字が置き換えられる複数の REPLACE 関数とは異なります。

 ストアドプロシージャでよく RELACE 関数が何重にも入れ子になっている処理を見かけることがあります。全ての場合において使用できるわけではありませんが、TRANSLATE を使って、複雑になってしまったストアドプロシージャをリファクタリングしましょう。

The features you should know about SQL Server Management Studio - Part 14 - (New Features in SSMS 18)

Golden Week of 10 consecutive holidays has started. It's a historical Golden Week that changes from HEISEI( 平成 ) to REIWA( 令和 ) during consecutive holidays. Recently, I thought that SQL Server CTP update was quick. SSMS 18 has become generally available. Whenever a new version comes out related to SQL Server, I tend to look around dmv, execution plans. Since there were changes in the SSMS graphical execution plan, I tried it.

  • AdventureWorks2017, SalesOrderHeader is used.
  • A NonClustered index of OrderDate is created.

Let's execute the following query in SQL Server Management Studio!

SELECT
    SalesOrderID,
    RevisionNumber,
    OrderDate,
    DueDate,
    ShipDate,
    Status,
    OnlineOrderFlag,
    SalesOrderNumber,
    PurchaseOrderNumber,
    AccountNumber,
    CustomerID,
    SalesPersonID,
    TerritoryID,
    BillToAddressID,
    ShipToAddressID,
    ShipMethodID,
    CreditCardID,
    CreditCardApprovalCode,
    CurrencyRateID,
    SubTotal,
    TaxAmt,
    Freight,
    TotalDue,
    Comment,
    rowguid,
    ModifiedDate
FROM
    AdventureWorks2017.Sales.SalesOrderHeader WITH(INDEX(NonClusteredIndex))
WHERE
    OrderDate BETWEEN '2011-05-30 00:00:00.000' AND '2012-05-31 00:00:00.000'

f:id:koogucc11:20190427181744p:plain
Execution Result

The operator sees something that is unfamiliar when you look closely.

f:id:koogucc11:20190427182111p:plain
New one is displayed to the operator!

The elapsed time, actual number of lines and estimated number of lines are displayed. This sounds good. It is possible to check if there is a statistical problem or not. The red line of 3028 is the actual line, and the blue line of 3041 is the estimated number of lines.

f:id:koogucc11:20190427182532p:plain
actual number of lines and estimated number of lines

Every time a version is upgraded, good improvements are added. Thanx!