Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
型を合わせとくとデータが多くなっても安心
Search
Oda Shinsuke
October 22, 2023
Technology
410
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
型を合わせとくとデータが多くなっても安心
第13回 関西DB勉強会
https://kansaidbstudy.connpass.com/event/294889/
Oda Shinsuke
October 22, 2023
More Decks by Oda Shinsuke
See All by Oda Shinsuke
SQL Server 2025 最適化されたロック
odashinsuke
0
100
SQL Server 2025 LT
odashinsuke
0
1k
SQL Server ベクトル検索
odashinsuke
0
2.3k
Other Decks in Technology
See All in Technology
プラットフォームを「作る」、 チームに「入り込む」
sansantech
PRO
0
400
OpenClawでAzure DevOpsのWiki更新を自動化する - クラウドAIだけでは届かない場所へ
yutakaosada
0
130
【データ横丁主催】AI Agentがコンテキストを使って仕事をした後、何が残るのか― 組織の経験を次の判断に引き継ぐ「Agent Memory」
shisyu_gaku
2
280
Execution in the Kingdom of Agents: Reflections on Abstraction and Complexity
bcantrill
0
320
[Kiro Meetup #7] Kiro Crew Dive Deep
konippi
0
430
あなたの知らないAmazon VPC Route Server/Amazon VPC Route Server you don't know about
masakiokuda
0
170
Making AI Agents Safe and Fast- Jev, Obsidian, and the Meta-Harness
x5gtrn
PRO
0
170
ScotSecure West 2026 - Glasgow
raybugg
0
210
Account Factory for Terraformによる 標準化されたアカウント発行の自動化
pensuke628
0
120
2026-09-26 Platform Engineering Kaigi 2026 インフラとアプリの境界線と委譲の設計 / Drawing the Infra and App Line
masasuzu
0
510
いちAWSエンジニアのAI活用を振り返る #devio2026 / devio osaka 2026 kawahara
masahirokawahara
1
290
CI/CDではもう遅い - 人とAIが迂回しないDevSecOps Verify基盤の再設計 -
kintotechdev
1
490
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
Mind Mapping
helmedeiros
1
380
エンジニアに許された特別な時間の終わり
watany
109
250k
Site-Speed That Sticks
csswizardry
13
1.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Testing 201, or: Great Expectations
jmmastey
46
8.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
290
Transcript
型を合わせとくとデータが多くなっ ても安心 第13回 関西DB勉強会 2023/10/21 @shinsukeoda
今日のお話し SQL Server の話しです 2012 以前 (~ 2012) 2014 以降
(2014 ~) 今日詳しく話すのはこっち! 型の不一致で起きる問題の1パターンを紹 介します 今日取り上げるのは文字列型について
2種類の文字列型 char / varchar 日本語環境で特に意識してなかったら、 MS932 で文字データが格納される char および varchar
(Transact-SQL) - SQL Server | Microsoft Learn nchar / nvarchar Unicode で文字データが格納される nchar および nvarchar (Transact-SQL) - SQL Server | Microsoft Learn
今日のお題 create table [受注テーブル] ( [Id] bigint not null primary
key , [区分] char(1) not null , [伝票番号] varchar(10) not null , constraint [UX_受注テーブル_区分_伝票番号] unique ([区分], [伝票番号]) )
区分、伝票番号でデータを取得 ① where [区分] = 1 and [伝票番号] = 'JU00000001’
② where [区分] = '1' and [伝票番号] = 'JU00000001’ ③ where [区分] = N'1' and [伝票番 号] = N'JU00000001' テーブル定義から抜粋 [区分] char(1) not null [伝票番号] varchar(10) not null
① 区分に数値 where [区分] = 1 and [伝票番号] = 'JU00000001’
テーブル定義から抜粋 [区分] char(1) not null [伝票番号] varchar(10) not null 3つの中では、1番ダメなやつ 区分列が暗黙の型変換で数値に変換されて実行される。 区分列の 1文字が全て数値に変換出来るならエラーにならない。
型変換の優先順位 データ型の優先順位 (Transact-SQL) - SQL Server | Microsoft Learn より
② 全部型が一致 where [区分] = '1' and [伝票番号] = 'JU00000001’
テーブル定義から抜粋 [区分] char(1) not null [伝票番号] varchar(10) not null 正解! 全部型が一致してるので問題なし。unique 制約のインデックス も使われる。
③ 区分に nchar / nvarchar where [区分] = N'1' and
[伝票番号] = N'JU00000001' テーブル定義から抜粋 [区分] char(1) not null [伝票番号] varchar(10) not null おしい! 暗黙の型変換で、区分列 が nvarchar に変換されて実行さ れる。 文字列型同士の変換なのでエラーにはならない。
暗黙の型変換って… -- 数値に変換 CONVERT_IMPLICIT(int, [dbkan].[dbo].[受注テーブ ル].[区分],0) = CONVERT_IMPLICIT(int, @1, 0)
-- nchar に変換 CONVERT_IMPLICIT(nchar(1), [dbkan].[dbo].[受注 テーブル].[区分],0) = @1
数値型への変換
nchar 型への変換
暗黙の型変換でもインデックス有効? SQL Server 2014 以降だと、char / varchar / nchar /
nvarchar の convert_implicit ではインデックスが効 くようになっています! 公式ドキュメントは未確認! 2012 までは 数値と同じようにテーブルス キャン
型変換無しの実行プラン
型変換無しのリード/実行時間 (1 row affected) Table '受注テーブル_10000000'. Scan count 0, logical
reads 3, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.
型変換有りの実行プラン
None
型変換有りのリード/実行時間 (1 row affected) Table '受注テーブル_10000000'. Scan count 1, logical
reads 18057, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0. SQL Server Execution Times: CPU time = 1234 ms, elapsed time = 1243 ms.
手抜き実装 + ライブラリの問題? プログラムから パラメータ 渡す時に型指 定してる?プログラムの型からライブラ リが内部で推察して型指定されてない? .NET の場合
String => nvarchar になる パラメーターおよびパラメーター データ型の構成 - ADO.NET | Microsoft Learn
まとめ 型揃えとかないとデータ増えた時やらか すかも join の条件の列は、双方同じ型になってい る? プログラムから SQL 呼び出す時にパラメー タの型を手抜きせずにちゃんと渡してる?
SQL のリテラル書くときに、’aaa’, N’aaa’ の違い意識してる?
サンプルクエリとか 型を合わせとくとデータが多くなっても 安心 - お だ のスペース (hatenablog.com)