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
160
SQL Server 2025 LT
odashinsuke
0
1.1k
SQL Server ベクトル検索
odashinsuke
0
2.4k
Other Decks in Technology
See All in Technology
BedrockとLambdaで作る リアルタイム進行型推理ゲーム
kawametho
0
210
Codex概要
ymiya55
0
470
ai_cording_with_k8s_knowledge.pdf
mochizuki875
1
320
AIは爆速なのに、私が詰まっていた話 ― 音声入力と鳴くマスコットでボトルネックを削る
yama3133
0
550
FactoryBotアンチパターン / factory_bot anti-patterns
toshimaru
0
140
Essential architectural thinking (revised edition)
ufried
0
110
ハードウェアコンペでもAI駆動開発が進んでいる話
iotengineer22
1
570
ビジネスを止めない技術的負債の返済のための戦略とその手法 - 技術的負債と向き合う / Complexity and Simplicity
soudai
PRO
3
670
AI時代のAPI開発を加速する品質ガードレール / API Quality Guardrails in the AI Era
yokawasa
0
160
脆弱性診断って、何をしているの? 〜弱点を見つけて、会社の改善につなげるまで〜
keeg
0
720
20260930_Gemma4_Hands-on
tsho
0
240
TinyGoでオブザーバビリティ?OpenTelemetryはTinyGoで使えるのか / Is OpenTelemetry available in TinyGo environment?
ymotongpoo
2
110
Featured
See All Featured
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
320
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
350
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
KATA
mclloyd
PRO
35
16k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
260
Docker and Python
trallard
47
4.2k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
560
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
Bash Introduction
62gerente
615
220k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
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)