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

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

Summary of SQL Server Tuning - Part 1 - (Examine indexes)

Happy New Year to all! I decided to translate old Japanese version posts to English for studying English in this year. First of all, I try to translate following post.
ryuchan.hatenablog.com

I would like to summarize the tuning of SQL Server. First, let's look at how to extract queries that don't use indexes, SQL Server tuning makes heavy use of dynamic management views that start with "dm_*". At this time, mainly use the following three tables to find queries that don't use indexes.

  • sys.dm_db_missing_index_group_stats
    Get information about the missing index group.
  • sys.dm_db_missing_index_groups
    Table required to JOIN sys.dm_db_missing_index_group_stats and sys.dm_db_missing_index_details.
  • sys.dm_db_missing_index_details
    Get detailed information about the missing indexes.

You can use the following query to find the missing index. (Using the usual AdventureWorksDB.)

USE AdventureWorks2012
SELECT gs.last_user_seek AS [Time of last seek],
       id.statement AS [Table name] ,
       id.equality_columns AS [Columns that can be used for equality predicates],
       id.inequality_columns AS [Columns that can be used for inequality predicates] ,
       id.included_columns AS [Columns required as included columns],
       gs.unique_compiles AS [Number of compilations and recompilations],
       gs.user_seeks AS [Number of seeks generated by the query.]
 FROM  sys.dm_db_missing_index_group_stats AS gs
       INNER JOIN sys.dm_db_missing_index_groups AS ig
			ON gs.group_handle = ig.index_group_handle
       INNER JOIN sys.dm_db_missing_index_details AS id
			ON ig.index_handle = id.index_handle
 WHERE id.[database_id] =DB_ID() Order By gs.last_user_seek ASC


After executing some queries without indexes, the above query will look like the below. I explain how to improve the area in the red frame.
http://cdn-ak.f.st-hatena.com/images/fotolife/k/koogucc11/20130923/20130923124748.jpg

  • Last seek time
    2013-09-23 03:42:41.773
  • Table name
    [AdventureWorks2012]. [Purchasing]. [PurchaseOrderHeader].
  • Columns that can be used for equality predicates
    [RevisionNumber].
  • Columns that can be used for inequality predicates
    NULL
  • Columns required as inclusive columns
    [PurchaseOrderID], [Status], [EmployeeID], [VendorID], [ShipMethodID], [OrderDate], [ShipDate], [SubTotal], [TaxAmt], [Freight], [TotalDue], [ModifiedDate].
  • Number of compilations and recompilations
    1
  • Number of seeks generated by the query
    2

From the above results, it can be determined that the RevisionNumber column, which has no index is specified in the WHERE clause. In addition, you can see that the following included columns are required.

[PurchaseOrderID], [Status], [EmployeeID], [VendorID], [ShipMethodID], [OrderDate], [ShipDate], [SubTotal], [TaxAmt], [Freight], [ TotalDue], [ModifiedDate]

Therefore, you can solve this problem by executing the following query.

CREATE NONCLUSTERED INDEX IX_PurchaseOrderHeader_RevisionNumber
ON [Purchasing].[PurchaseOrderHeader] ([RevisionNumber])
INCLUDE ([PurchaseOrderID],[Status],[EmployeeID],[VendorID],[ShipMethodID],[OrderDate],[ShipDate],[SubTotal],[TaxAmt],[Freight],[TotalDue],[ModifiedDate])

After executing the above query, if you run the script again, you will get the following result. The problematic rows have disappeared. A proper index has been created.

http://cdn-ak.f.st-hatena.com/images/fotolife/k/koogucc11/20130923/20130923130127.jpg

However, even without this kind of examination, it is possible to handle by checking the execution plan of SQL Server Management Studio during daily development. When the execution plan is displayed, if there are not enough indexes, they will be displayed as shown in the red frame below.

http://cdn-ak.f.st-hatena.com/images/fotolife/k/koogucc11/20130923/20130923132105.jpg

Right-click on the red frame to display the menu as shown below. Click "Missing Index Details..." from the menu.

http://cdn-ak.f.st-hatena.com/images/fotolife/k/koogucc11/20130923/20130923132224.jpg

As shown in the figure below, CREATE INDEX statement will be generated.

http://cdn-ak.f.st-hatena.com/images/fotolife/k/koogucc11/20130923/20130923132428.jpg

The generated query is shown below. It also shows what percentage improvement, etc. (In this example, it seems to improve by 83.0941%.)

/*
Missing Index Details from SQLQuery1.sql - SQLSERVERTEST.AdventureWorks2012 (SQLSERVERTEST\sqlservertest (53))
The Query Processor estimates that implementing the following index could improve the query cost by 83.0941%.
*/

/*
USE [AdventureWorks2012]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [Purchasing].[PurchaseOrderHeader] ([RevisionNumber])
INCLUDE ([PurchaseOrderID],[Status],[EmployeeID],[VendorID],[ShipMethodID],[OrderDate],[ShipDate],[SubTotal],[TaxAmt],[Freight],[TotalDue],[ModifiedDate])
GO
*/

Be sure to check your queries in SQL Server Management Studio on a regular basis.

Ginger is a really useful website to check my grammar!!
www.getginger.jp

年末に記事書いてみる / I'll write an article at the end of the year.

The year is almost over. There weren't many articles this year, either. Next year will be a year of new challenges, so I will try my best to write articles next year. Have a happy new year, everyone!

Following article is still being read. Tuning is an eternal issue.
ryuchan.hatenablog.com

 今年ももうすぐ終わりです。今年も記事数少なかったなぁ。来年はまた新たな挑戦の年になるので、来年こそ記事頑張って書いていきたいと思います。皆様、良いお年を!

 この記事が未だに読まれているなぁ。チューニングは永遠の課題ですね。
ryuchan.hatenablog.com

Useful SQL functions, syntax, and more - Part 19 (FAST?)

Recently I started working on SQL Server just a little bit on side job. Tuning is an area that I personally like very much because it's like putting the puzzle back together. While doing some research, I found a query hint that I'd never used called "FAST".

FAST number_rows
Specifies that the query is optimized for fast retrieval of the first number_rows. This result is a nonnegative integer. After the first number_rows are returned, the query continues execution and produces its full result set.

Is there anything this convenient for the keyword "fast search"? But I was interested in it, so I'll check out how it works as soon as possible. I'll use the following query.

SELECT 
    SOH.SalesOrderID,
    SOH.CustomerID,
    SOH.SalesPersonID
FROM 
    Sales.SalesOrderHeader SOH
WHERE 
    SalesPersonID = 279;

Above execution plan as follows.
f:id:koogucc11:20200831201858p:plain

Next, let's add OPTION (FAST n). n = 100.

SELECT
    SOH.SalesOrderID,
    SOH.CustomerID,
    SOH.SalesPersonID
FROM 
    Sales.SalesOrderHeader SOH
WHERE 
    SalesPersonID = 279
OPTION (FAST 100);

 Above Execution plan as follows.
f:id:koogucc11:20200831201946p:plain

Let's compare the two plans. The number of rows is different and the cost is lower with OPTION (FAST 100) added.
f:id:koogucc11:20200831202923p:plain

The estimated number of rows is different. The one with OPTION (FAST 100) has 100. There is an attribute "EstimateRowsWithoutRowGoal = 429" that I've never seen before. (My knowledge of SQL Server stops at version 2012, but I don't remember this being around for a long time.)
f:id:koogucc11:20200831203652p:plain

The estimated number of rows has been reduced here as well, and "EstimateRowsWithoutRowGoal = 31465" has been added.
f:id:koogucc11:20200831204006p:plain

Hmmm, I haven't written an article in a while, but I'm halfway through. But I guess this is convenient because it's a good story to write an article about above, lol.

便利な SQL の関数とか、構文とか、その他色々まとめてみる - その19 (FASTって?)

 最近、副業でほんの少しだけ SQL Server のお仕事を開始しました。チューニングは、パズルを組みなおすような作業なので個人的に非常に好きな分野です。色々調べものをしていたら、FAST という使ったことのないクエリヒントを見つけました。

FAST number_rows
最初の number_rows を高速検索するためにクエリの最適化を行うことを指定します。 この結果は負以外の整数です。 最初の number_rows を返した後、クエリは実行を続け、完全な結果セットを作成します。

 高速検索というキーワードにこんな都合がいいものあるんかいな?と思いながらも興味を持ったので、早速動作をチェックしてみます。下記のクエリを使用します。

SELECT 
    SOH.SalesOrderID,
    SOH.CustomerID,
    SOH.SalesPersonID
FROM 
    Sales.SalesOrderHeader SOH
WHERE 
    SalesPersonID = 279;

 実行プランは下図の通りです。
f:id:koogucc11:20200831201858p:plain

 次に、OPTION (FAST n) を付加してみましょう。n = 100 にしておきます。

SELECT
    SOH.SalesOrderID,
    SOH.CustomerID,
    SOH.SalesPersonID
FROM 
    Sales.SalesOrderHeader SOH
WHERE 
    SalesPersonID = 279
OPTION (FAST 100);

 実行プランは下図の通りです。
f:id:koogucc11:20200831201946p:plain

 二つのプランを比較してみましょう。件数が違っていたり、Cost が OPTION (FAST 100) を付加したほうが低くなってますね。
f:id:koogucc11:20200831202923p:plain

 推定行数が異なっていますね。OPTION (FAST 100) を付加したほうは 100件になっています。しかも、付加した方には EstimateRowsWithoutRowGoal = 429 とか見たことない属性があります。(知識が 2012 で止まっているのですが、こんなの昔からあったっけ?)
f:id:koogucc11:20200831203652p:plain

 こちらも推定行数が減っており、EstimateRowsWithoutRowGoal = 31465 が付加されています。
f:id:koogucc11:20200831204006p:plain

 うーん、久しぶりに記事を書いてみたけど、中途半端に終わってしまった。しかし、これは記事を書くネタになるから都合がいいかな笑

 

Finding various software to make iPad sub-display

I forgot to write the English version of the article. I wrote early in the morning of May 5.

                      • -

This is my first article in a while. I hadn't written a article at all since I wrote my New Year's resolutions.
ryuchan.hatenablog.com

I haven't achieved what I had resoluted for at all. Ugh.

Here are my resolutions for this year. Write blogs regularly and publish technical information. Learn English every day and use it for my work.

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

Due to "Corona Virus", Golden Week was turned into Gaman Week ("Gaman" means endurance.) as some governor had sent out by e-mail. Today (May 4), a state of emergency was declared extended until May 31. It looks like I'll still be WFH. The only way to achieve our goals is to improve efficiency, since I mostly work on a PC, so the only way to improve efficiency is to make iPad multi-display. Multi-display is a must. After the state of emergency was declared, I purchased one unsold display at an appliance store.
https://www.biccamera.com/bc/item/5286471/

However, there are many things that i want to keep open at all times while working, such as email, Teams, Twitter? Youtube? Facebook? It's annoying to switch between multiple applications. I want more sub-displays! So, I tried out four sub-display tools this time.

  • Sidecar
  • Duet Display
  • Yam Display
  • spacedesk

Sidecar connects to iPad from macOS and is very responsive and functional. However, the MAC has been taken over by my kid's cram school for online classes, so I can no longer use it at work.
f:id:koogucc11:20200504200840j:plain

Duet Display was probably very much in use on macOS until Sidecar was released. There are many items that can be configured, and it is as good as Sidecar. However, it's wired, and if i buy Duet Air, can do it wirelessly...
f:id:koogucc11:20200504201514j:plain

Yam Display is only for macOS, so I can't use it this time, unfortunately. So, I tried the last spacedesk and it fit my home requirements perfectly. What's more, it's free! The OS is limited to Windows. In addition, since the connection is via LAN, the sub-display device (iPad etc) must be connected to the same segment of LAN. In some companies, it may be difficult to connect personal devices to the company network, but this time it was not a problem at all because i'am WFH now. So, let's set it up quickly.

First, install the server function on the PC from which i want to connect. The application will be installed as below.
f:id:koogucc11:20200504205007p:plain

Next, install the Multi Monitor App on your iPad, which will be sub-display. After the installation, set the IP address of the PC on which the server function is installed.
f:id:koogucc11:20200504205922j:plain

After setting it up, can make it a sub-display by clicking on the red frame part of the tool.
f:id:koogucc11:20200504210143j:plain

As a result, my home environment looks like below, with a 5 sub-display lol.
f:id:koogucc11:20200504210603p:plain

Even with all these features, spacedesk is free software. Great!

I recently bought a new oven toaster. It's stylish, multi-functional and very easy to use. I highly recommend it.

クイジナートノンフライ オーブントースター TOA-28J

クイジナートノンフライ オーブントースター TOA-28J

  • 発売日: 2019/10/25
  • メディア: ホーム&キッチン
www.cuisinart.jp

iPad をサブディスプレイ化するソフトをいろいろ探してみた

 久ぶりの投稿です。新年の抱負を書いて以来、まったく記事を書いていませんでした。
ryuchan.hatenablog.com

 抱負としてあげていたこともまったく達成できていません。ううっ。

今年の抱負をあげます。 定期的にブログを書き、技術情報を公開すること。 毎日英語を勉強し、仕事に役立てること。

今年の抱負を書いてみた - 都内で働くSEの技術的なひとりごと / Technical soliloquy of System Engineer working in Tokyo

 コロナウィルスの影響でゴールデンウイーク (GW) は、どこかの知事がメール配信していたようにガマンウィーク (GW) になってしまいました。本日 (5/4) に緊急事態宣言が 5/31 まで延長されました。在宅でのお仕事もまだまだ続きそうです。在宅で子供の面倒や家事などをしながらだと、仕事に集中する時間も限られますので、目標を達成するには効率をあげるしかありません。PC での作業が中心なので、効率を向上させるには....."マルチディスプレイ化" が必須です。緊急事態宣言後、家電量販店で売れ残っていたディスプレイを一台買いました。
https://www.biccamera.com/bc/item/5286471/

 しかし、メール, Teams, Twitter?, Youtube?, Facebook?, 各種アプリ画面等々仕事中に常時開いておきたいものは多数あります。複数のアプリケーションを切り替えるのは億劫です。もっとサブディスプレイがほしい!というわけで、今回 4 つほどサブディスプレイツールを試してみました。

  • Sidecar
  • Duet Display
  • Yam Display
  • spacedesk

 Sidecar は、macOS から iPad に接続するもので、レスポンス・機能ともに非常に優れています。ただ、mac は子供の塾でオンライン授業を行うことになったため、占有されてしまい、仕事で使うことができなくなりました。
f:id:koogucc11:20200504200840j:plain

 Duet Display は、Sidecar がリリースされるまでは、macOS で非常に多くの人が使っていたかと思います。設定できる項目も多く、Sidecar に引けを取らない感じです。ただ、有線なんですよね。Duet Air を購入すれば無線でも可能になるんですが....
f:id:koogucc11:20200504201514j:plain

 Yam Display は、macOS 専用なので、今回は残念ながら使えません。そこで、最後の spacedesk を試してみたんですが、私の在宅要件にぴったりはまりました。しかも、無料!接続元の OS は Windows に限られます。また、LAN 経由での接続になるため、サブディスプレイとなる機器 (iPad) などが、同一セグメントの LAN に接続している必要があります。会社では個人機器を社内ネットワークにつなぐのは難しいケースもあるかと思いますが、今回は在宅なので全く問題ありません。それでは、早速セットアップしてみましょう。

 まず、接続元となる PC にサーバー機能をインストールします。下図のようなアプリケーションがインストールされます。
f:id:koogucc11:20200504205007p:plain

 次に、サブディスプレイとなる iPad などに Multi Monitor App をインストールします。インストール後に、サーバー機能をインストールされた PC の IP アドレスを設定します。
f:id:koogucc11:20200504205922j:plain

 設定後、ツールの赤枠部分をクリックすることで、サブモニタ化することができます。
f:id:koogucc11:20200504210143j:plain

 結果、私の在宅環境は下図のようになりました。5面ディスプレイ笑
f:id:koogucc11:20200504210603p:plain

 これだけの機能があり、フリーソフトとは。恐るべし、spacedesk!

 最近、オーブントースターを買い換えました。これ、見た目おしゃれで、多機能で、すごく使いやすいです。お勧めです。

クイジナートノンフライ オーブントースター TOA-28J

クイジナートノンフライ オーブントースター TOA-28J

  • 発売日: 2019/10/25
  • メディア: ホーム&キッチン
www.cuisinart.jp