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
270
SQL Server 2025 LT
odashinsuke
0
1.2k
SQL Server ベクトル検索
odashinsuke
0
2.5k
Other Decks in Technology
See All in Technology
視野はAIに、視座は人に / ai-for-breadth-humans-for-perspective
nkzn
6
3.1k
eBPF 簡介 2026 版
shunghsiyu
0
190
Technical Project Manager の役割とやりがい、少しだけ AI
yoshiiryo1
0
600
Kiro Crew 入門 (手を離しても進む仕事) | 目指せ Kiro マスター 11
inariku
PRO
3
470
増え続けるプロダクトの品質をどう担保するか。ProductSpecとTypeScriptによる取り組み
joere
3
570
Don't write the architecture only in the prompt.
neohsu
0
310
試験勉強はGrokBotでやれ
maigo999
0
110
AIに任せられるFlutterコードベースを作る 〜仕様をコードで表し、ハーネスで守る〜
myohei
1
640
複雑な業務システムを高速でつくるため、チーム全員をつくれる人へ
ayakurabuchi
0
140
`go get`したらELFが降ってきた件
taiyow
0
180
Sinatra on PicoRuby ローカルでセルフホスト
bash0c7
0
110
Copilot Coworkで「みのるん式」やってみた
minorun365
PRO
2
1.6k
Featured
See All Featured
KATA
mclloyd
PRO
35
16k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
970
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
590
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
800
Building a Scalable Design System with Sketch
lauravandoore
464
34k
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.7k
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
4 Signs Your Business is Dying
shpigford
187
23k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
460
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)