ä»æ¥ãç§ã¯ä»ã®ããšã«ã€ããŠè©±ããã-決ããŠãã¹ããã©ã¯ãã£ã¹ã«å±ããŠããªããã®ã«ã€ããŠã足ãèžã¿ã«ãã£ãŠä»¥åã«å®è¡ãããã¯ãšãªãé ããããããšã©ãŒã®ããã«ãŸã£ããå®è¡ãããªããªã£ããããå©ããåããŠ...ãã³ããšèšç»ã¬ã€ãã«ã€ããŠã§ãã
ãã³ãã¯ã¯ãšãªãªããã£ãã€ã¶ãŒãžã®ãã³ãã§ããå®å šãªãªã¹ãã¯MSDNã«ãããŸãããããã®äžéšã¯å®éã®ãã³ãã§ãïŒããšãã°ãOPTIONïŒMAXDOP 4ïŒãæå®ã§ããŸãïŒãããã«ãããã¯ãšãªã¯æ倧䞊å床= 4ã§å®è¡ã§ããŸãããSQL Serverããã®ãã³ãã䜿çšããŠäžŠåãã©ã³ãçæããä¿èšŒã¯ãããŸããã
ä»ã®éšåã¯ãè¡åãžã®çŽæ¥ã®ã¬ã€ãã§ããããšãã°ãOPTIONïŒHASH JOINïŒãšèšè¿°ããå ŽåãSQL Serverã¯NESTED LOOPSãšMERGE JOINã®ãªããã©ã³ãæ§ç¯ããŸãããããŠãããã·ã¥çµåã ãã§èšç»ãæ§ç¯ããããšãäžå¯èœã§ããããšãå€æããå Žåãã©ããªããç¥ã£ãŠããŸããïŒãªããã£ãã€ã¶ãŒã¯ããèšãã§ããã-ç§ã¯èšç»ãæ§ç¯ã§ãããã¯ãšãªã¯å®è¡ãããŸããã
åé¡ã¯ããªããã£ãã€ã¶ããã³ããŒã§æ»æã§ãããã³ããç¹å®ã®ïŒå°ãªããšãç§ã«ã¯ïŒããããªãããšã§ãããããŠãã©ã®ãã³ããæåã®ãã³ãã§ãããäœãåé¡ãçºçããå Žåã«ãªã¯ãšã¹ããã¯ã©ãã·ã¥ãããå¯èœæ§ããããŸãã確ãã«ãããã説æãããŠããæ¢è£œã®ã³ã¬ã¯ã·ã§ã³ã¯ãã§ã«ããã€ããããŸãããããã¯ãããã«ããŠãå ¬åŒæ å ±ã§ã¯ãªãããã€ã§ãå€æŽãããå¯èœæ§ããããŸãã
ãã©ã³ã¬ã€ãã¯ãç¹å®ã®ãã³ãã®ã»ãããç¹å®ã®èŠæ±ïŒããã¹ããããã£ãŠããïŒã«ãã€ã³ãã§ããããã«ããïŒæ£ãã翻蚳ããæ¹æ³ãããããªãïŒãã®ã§ããããã¯ãããšãã°ãORMã«ãã£ãŠçæãããèŠæ±ããã¹ãã«çŽæ¥åœ±é¿ãäžããããªãå Žåã«é¢é£ããå¯èœæ§ããããŸãã
ãã³ããšèšç»ã¬ã€ãã¯ã©ã¡ãããã¹ããã©ã¯ãã£ã¹ã§ã¯ãªãããã³ããšãããã®ã¬ã€ããçç¥ããããšããå§ãããŸããããã¯ãããŒã¿ã®ååžãå€åããããŒã¿ã¿ã€ããå€åãã100äžä»¥äžã®äºæ ãçºçããå¯èœæ§ãããããã§ãããã³ãã䜿çšããã¯ãšãªã¯ããã³ãã䜿çšããªãå Žåãããæ©èœãäœäžããå Žåã«ãã£ãŠã¯å®å šã«æ©èœããªããªããŸããããªãã¯èªåãäœãããŠããã®ãããããŠãã®çç±ã100ïŒ èªèããŠããå¿ èŠããããŸãã
ãªãç§ãããã«åãæãã£ãã®ãã«ã€ããŠã®ç°¡åãªèª¬æã§ãã
ç§ã¯ã10ããæ倧ãŸã§ã®ããŸããŸãªãµã€ãºã®nvarcharãã£ãŒã«ããããããããåºãããŒãã«ãæã£ãŠããŸãããããŠããã®ããŒãã«ãžã®äžé£ã®ã¯ãšãªããããCHARINDEXã¯ãããã®åã®1ã€ä»¥äžã§ãµãã¹ããªã³ã°ã®åºçŸãæ€çŽ¢ããŸããããšãã°ã次ã®ãããªãªã¯ãšã¹ãããããŸãã
SELECT *
FROM table
WHERE CHARINDEX(N' ', column)>1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET x ROWS FETCH NEXT y ROWS ONLY
ããŒãã«ã®Idã«ã¯ã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ããããåã«ã¯äžæã§ãªãéã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ããããŸãããåç¥ã®ãšãããããã§ã¯CHARINDEXã䜿çšããŠãããããããã¯ãŸã£ããSARGableã§ã¯ãããŸãããSBã®æœåšçãªåé¡ãåé¿ããããã«ãããã§èŠã€ããããšãã§ããéããŠããããŒã¿ããŒã¹StackOverflow2013ã§ãã®ç¶æ³ãã·ãã¥ã¬ãŒãããŸãã
Idã«ããã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãšæ¬¡ã®ãããªã¯ãšãªã®ã¿ãæã€dbo.PostsããŒãã«ãèããŠã¿ãŸãã
SELECT *
FROM dbo.Posts
WHERE CHARINDEX (N'Data', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY
å®éã®ããŒã¿ããŒã¹ãšäžèŽãããããã«ãTitleåã«ã€ã³ããã¯ã¹ãäœæããŸãã
CREATE INDEX ix_Title ON dbo.Posts (Title);
ãã¡ãããçµæãšããŠãã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãå察æ¹åã«ã¹ãã£ã³ããããšãããªããå®å šã«è«ççãªå®è¡èšç»ãåŸ
ãããŸãã
ããŒãã«ãæçš¿ããã¹ãã£ã³ã«ãŠã³ã1ãè«çèªã¿åã516ãç©çèªã¿åã0ãå èªã¿èªã¿åã0ãlobè«çèªã¿åã0ãlobç©çèªã¿åã0ãlobå èªã¿èªã¿åã0ãSQLServer
å®è¡æéïŒ
CPUæé= 16 ms
ãããããããŒã¿ããšããäžè¬çãªåèªã®ä»£ããã«ããã£ãšçãããã®ãæ¢ããšã©ããªããŸããïŒããšãã°ãN'Aptana 'ïŒãããäœã§ãããããããªãïŒããã¡ãããèšç»ã¯åããŸãŸã§ãããå®è¡çµ±èšã¯å€å°ç°ãªããŸãã
ããŒãã«ãæçš¿ããã¹ãã£ã³ã«ãŠã³ã1ãè«çèªã¿åã253191ãç©çèªã¿åã113ãå èªã¿èªã¿åã224602ãLOBè«çèªã¿åã0ãLOBç©çèªã¿åã0ãLOBå èªã¿èªã¿åã0ãSQLServer
å®è¡æéïŒ
CPUæé= 2563ããªç§
ãããŠãããè«ççã§ã-èšèã¯ããŸãäžè¬çã§ã¯ãªããSQL Serverã¯ãããå«ã25è¡ãèŠã€ããããã«ããå€ãã®ããŒã¿ãã¹ãã£ã³ããå¿ èŠããããŸããããããã©ãããããããããã¯ã¯ãŒã«ã§ã¯ãããŸããããïŒ
ãããŠãç§ã¯éã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãäœæããŠããŸããããã¶ããSQL Serverãããã䜿çšããæ¹ãè¯ãã§ããããïŒåœŒèªèº«ã¯ããã䜿çšããªãã®ã§ããã³ããè¿œå ããŸãã
SELECT *
FROM dbo.Posts
WHERE CHARINDEX (N'Aptana', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY
OPTION (TABLE HINT (dbo.Posts, INDEX(ix_Title)));
ãããŠãäœããã©ããããããå®å šã«æ²ããã§ããå®è¡çµ±èšïŒ
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Posts'. Scan count 5, logical reads 109312, physical reads 5, read-ahead reads 104946, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 35031 ms
ãããŠèšç»ïŒ
å®è¡èšç»ã¯äžŠåã§ã2ã€ã®çš®é¡ããããã©ã¡ããtempdbã«æµåºããããŸããã¡ãªã¿ã«ãéã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã¹ãã£ã³ã®åŸãããŒã«ãã¯ã¢ããã®åã«å®è¡ãããæåã®äžŠã¹æ¿ãã«æ³šæããŠãã ãããããã¯ãã©ã³ãã I / Oã®æ°ãåæžããããšããç¹å¥ãªSQL Serverã®æé©åã§ããããŒã«ãã¯ã¢ããã¯ãã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ããŒã®æé ã§å®è¡ãããŸãã詳现ã«ã€ããŠã¯ããã¡ããã芧ãã ããã
2çªç®ã®ãœãŒãã¯ãéé ã®IDã§25è¡ãéžæããããã«å¿ èŠã§ããã¡ãªã¿ã«ãSQL Serverã¯ãéé ã§ã®ã¿Idã§äžŠã¹æ¿ãã "é"æ¹åã§ããŒã«ãã¯ã¢ãããå®è¡ããæåã«ã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ããŒãæé ã§ã¯ãªãéé ã§äžŠã¹æ¿ããå¿ èŠããããšæšæž¬ããå¯èœæ§ããããŸãã
ãšã³ã㪠'Data'ã«ããæ€çŽ¢ã䜿çšããŠãéã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã«é¢ãããã³ããå«ãã¯ãšãªã®å®è¡ã«é¢ããçµ±èšãæäŸããŠããŸãããã©ãããããã®ããŒããããããŒããã©ã€ãã§ã¯ã16å以äžããããã¹ã¯ãªãŒã³ã·ã§ãããæ®ãããšã¯æããŸããã§ãããç³ãèš³ãããŸãããããã以äžé·ãåŸ ã¡ãããããŸããã
ãããããªã¯ãšã¹ãã¯ã©ãã§ããïŒã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã¹ãã£ã³ã¯ç©¶æ¥µã®å€¢ã§ãããäœãéãããããšã¯ã§ããŸãããïŒ
ãã¹ãŠã®çš®é¡ãåé¿ããããšãããšãéã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãèããŠäœæããŸããããããã¯éåžžãéã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã®ãã¹ããã©ã¯ãã£ã¹ãšéåžžèŠãªããããã®ãšççŸããŸãã
CREATE INDEX ix_Id_Title ON dbo.Posts (Id DESC, Title);
次ã«ããã³ãã䜿çšããŠãSQL Serverã«äœ¿çšããããã«æ瀺ããŸãã
SELECT *
FROM dbo.Posts
WHERE CHARINDEX (N'Aptana', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY
OPTION (TABLE HINT (dbo.Posts, INDEX(ix_Id_Title)));
ãããããã¯ããŸããããŸããïŒ
ããŒãã«ãæçš¿ããã¹ãã£ã³ã«ãŠã³ã1ãè«çèªã¿åã6259ãç©çèªã¿åã0ãå èªã¿èªã¿åã7816ãLOBè«çèªã¿åã0ãLOBç©çèªã¿åã0ãLOBå èªã¿èªã¿åã0ãSQLServer
å®è¡æéïŒ
CPUæé= 1734ããªç§
ããã»ããµæéã®å¢å ã¯ããã»ã©å€§ãããããŸããããèªãå¿ èŠãããã®ã¯ããã»ã©å€ãã¯ãããŸãããé »ç¹ãªãããŒã¿ãã¯ã©ãã§ããïŒ
ããŒãã«ãæçš¿ããã¹ãã£ã³ã«ãŠã³ã1ãè«çèªã¿åã208ãç©çèªã¿åã0ãå èªã¿èªã¿åã0ãLOBè«çèªã¿åã0ãLOBç©çèªã¿åã0ãLOBå èªã¿èªã¿åã0ãSQLServer
å®è¡æéïŒ
CPUæé= 0 ms
ãããŒããããããã§ããããŠããªã¯ãšã¹ãã¯ORMããã®ãã®ã§ããããã®ããã¹ããå€æŽããããšã¯ã§ããªãã®ã§ããã®ã€ã³ããã¯ã¹ããªã¯ãšã¹ãã«ããã€ã«ãããæ¹æ³ãç解ããå¿ èŠããããŸãããããŠãèšç»ã¬ã€ããå©ãã«ãªããŸãã
sp_create_plan_guideïŒMSDNïŒã¹ãã¢ãããã·ãŒãžã£ã¯ããã©ã³ã¬ã€ãã®äœæã«äœ¿çšãããŸãã
詳现ã«æ€èšããŠã¿ãŸãããã
sp_create_plan_guide [ @name = ] N'plan_guide_name'
, [ @stmt = ] N'statement_text'
, [ @type = ] N'{ OBJECT | SQL | TEMPLATE }'
, [ @module_or_batch = ]
{
N'[ schema_name. ] object_name'
| N'batch_text'
| NULL
}
, [ @params = ] { N'@parameter_name data_type [ ,...n ]' | NULL }
, [ @hints = ] {
N'OPTION ( query_hint [ ,...n ] )'
| N'XML_showplan'
| NULL
}
åå -æ確ã§äžæã®ãã©ã³ã¬ã€ãå
stmt-ããã¯ããã³ããè¿œå ããå¿ èŠããããªã¯ãšã¹ãã§ãããã®ãªã¯ãšã¹ãã¯ãã¢ããªã±ãŒã·ã§ã³ããã®ãªã¯ãšã¹ããšãŸã£ããåãããã«èšè¿°ããå¿ èŠãããããšãç¥ã£ãŠããããšãéèŠã§ããå¥åŠãªã¹ããŒã¹ïŒãã©ã³ã¬ã€ãã¯äœ¿çšãããŸãããééã£ãæ¹è¡ïŒãã©ã³ã¬ã€ãã¯äœ¿çšãããŸãããç©äºãããç°¡åã«ããããã«ãå°ãåŸã§æ»ããã©ã€ãããã¯ãããããŸãïŒããã§èŠã€ããŸããïŒã
ã¿ã€ã -æå®ããããªã¯ãšã¹ãã®å Žæã瀺ããŸã stmt... ã¹ãã¢ãããã·ãŒãžã£ã®äžéšã§ããå Žåã¯ãOBJECTã«ããå¿ èŠããããŸãããããè€æ°ã®ãªã¯ãšã¹ãã®ãããã®äžéšã§ããå ŽåããŸãã¯ã¢ãããã¯ãªã¯ãšã¹ãã®å ŽåããŸãã¯1ã€ã®ãªã¯ãšã¹ãã®ãããã®å Žåã¯ãSQLãå¿ èŠã§ããããã§TEMPLATEã瀺ãããŠããå Žåãããã¯ãMSDNã§èªãããšãã§ããã¯ãšãªã®ãã©ã¡ãŒã¿ãŒåã«é¢ããå¥ã®è©±ã§ãã
@module_or_batchã¯äŸåããŸãã¿ã€ããããã¿ã€ã= 'OBJECT'ãããã¯ã¹ãã¢ãããã·ãŒãžã£ã®ååã§ããå¿ èŠããããŸããããã¿ã€ã= 'BATCH'-ãããå šäœã®ããã¹ãããããã¢ããªã±ãŒã·ã§ã³ããååŸãããã®ãåèªããšã«æå®ããŸããå¥åŠãªã¹ããŒã¹ïŒãŸããããªãã¯ãã§ã«ç¥ã£ãŠããŸããNULLã®å Žåãããã¯1ã€ã®èŠæ±ããã®ãããã§ãããstmt ãã¹ãŠã®å¶éä»ãã
ãã©ã¡ãŒã¿-ãªã¯ãšã¹ãã«æž¡ããããã¹ãŠã®ãã©ã¡ãŒã¿ãšããŒã¿ã¿ã€ããããã«ãªã¹ããããŸãã
æåŸã«@hintsãããéšåã§ããããã§ããªã¯ãšã¹ãã«è¿œå ãããã³ããæå®ããå¿ èŠããããŸããããã§ã¯ãå¿ èŠã«å¿ããŠãå¿ èŠãªå®è¡ãã©ã³ãXML圢åŒã§æ瀺çã«æ¿å ¥ã§ããŸãããã®ãã©ã¡ãŒã¿ãŒã¯NULLã«ããããšãã§ããŸããããã«ãããSQL Serverã¯ãã¯ãšãªã§æ瀺çã«æå®ãããŠãããã³ãã䜿çšããŸãããstmt...
ããã§ãã¯ãšãªã®ãã©ã³ã¬ã€ããäœæããŸãã
DECLARE @sql nvarchar(max) = N'SELECT *
FROM dbo.Posts
WHERE CHARINDEX (N''Data'', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY';
exec sp_create_plan_guide @name = N'PG_dboPosts_Index_Id_Title'
, @stmt = @sql
, @type = N'SQL'
, @module_or_batch = NULL
, @params = NULL
, @hints = N'OPTION (TABLE HINT (dbo.Posts, INDEX (ix_Id_Title)))'
ãããŠããªã¯ãšã¹ããå®è¡ããããšããŸãïŒ
SELECT *
FROM dbo.Posts
WHERE CHARINDEX (N'Data', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY
ãããŒãããŸã
ãããŸãããæåŸã®SELECTã¹ããŒãã¡ã³ãã®ããããã£ã«ã¯ã次ã®ããã«è¡šç€º
ãããŸãããã°ããããèšç»ã¬ã€ããé©çšãããŸãããä»ãAptanaããæ€çŽ¢ãããšã©ããªããŸããïŒãããŠããã¹ãŠãæªããªããŸã-ãã¹ãŠã®çµæãšãšãã«ã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã®ã¹ãã£ã³ã«æ»ããŸããã©ãããŠïŒãŸãããã©ã³ã¬ã€ãã¯SPECIFICã¯ãšãªã«é©çšãããããããã®ããã¹ãã¯å®è¡äžã®ããã¹ããš1察1ã§äžèŽããŸãã
幞ããªããšã«ãç§ã®ã·ã¹ãã äžã®ã»ãšãã©ã®ãªã¯ãšã¹ãã¯ãã©ã¡ãŒã¿ãŒåãããŠããŸãããã©ã¡ãŒã¿åãããŠããªãã¯ãšãªãæäœããªãã£ãã®ã§ãæäœããå¿ èŠããªãããšãé¡ã£ãŠããŸãããããã«ã€ããŠã¯ããã³ãã¬ãŒãã䜿çšããããšãã§ãïŒTEMPLATEã«ã€ããŠå°ãäžãåç §ïŒãããŒã¿ããŒã¹ã§åŒ·å¶ãã©ã¡ãŒã¿åãæå¹ã«ã§ããŸãïŒäœãããŠããã®ããç解ããã«ãããè¡ããªãã§ãã ãã!!!ïŒãããããããã®åŸãèšç»ã¬ã€ãããªã³ã¯ã§ããããã«ãªããŸããããããç§ã¯æ¬åœã«ãããè©ŠããŠããŸããã
ç§ã®å Žåããªã¯ãšã¹ãã¯æ¬¡ã®ããã«å®è¡ãããŸãïŒ
exec sp_executesql
N'SELECT *
FROM dbo.Posts
WHERE CHARINDEX (@p0, Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY;'
, N'@p0 nvarchar(250), @p1 int, @p2 int'
, @p0 = N'Aptana', @p1 = 0, @p2 = 25;
ãããã£ãŠã察å¿ãããã©ã³ã¬ã€ããäœæããŸãã
DECLARE @sql nvarchar(max) = N'SELECT *
FROM dbo.Posts
WHERE CHARINDEX (@p0, Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY;';
exec sp_create_plan_guide @name = N'PG_paramters_dboPosts_Index_Id_Title'
, @stmt = @sql
, @type = N'SQL'
, @module_or_batch = NULL
, @params = N'@p0 nvarchar(250), @p1 int, @p2 int'
, @hints = N'OPTION (TABLE HINT (dbo.Posts, INDEX (ix_Id_Title)))'
ãããŠãäžäºããã¹ãŠãå¿ èŠã«å¿ããŠæ©èœããŸãïŒ
枩宀æ¡ä»¶ã®å€ã«ãããšããã©ã¡ãŒã¿ãŒãæ£ããæå®ã§ãããšã¯éããŸããstmtãã©ã³ã¬ã€ãããªã¯ãšã¹ãã«æ·»ä»ããããã«ãç§ã¯äžèšã®ãã©ã€ãããã¯ãããããŸãããã©ã³ãã£ãã·ã¥ãã¯ãªã¢ããã¬ã€ããåé€ãããã©ã¡ãŒã¿ãŒåãããã¯ãšãªãå床å®è¡ããŠããã£ãã·ã¥ãããã®å®è¡ãã©ã³ãšplan_handleãååŸããŸãã
ããã«å¯ŸããèŠæ±ã¯ãããšãã°æ¬¡ã®ããã«äœ¿çšã§ããŸãã
SELECT qs.plan_handle, st.text, qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text (qs.sql_handle) st
CROSS APPLY sys.dm_exec_query_plan (qs.plan_handle) qp
ããã§ãsp_create_plan_guide_from_handleã¹ãã¢ãããã·ãŒãžã£ã䜿çšããŠãæ¢åã®ãã©ã³ãããã©ã³ã¬ã€ããäœæã§ããŸãã
ãã©ã¡ãŒã¿ãšããŠåãåããŸãåå-äœæãããã¬ã€ãã®ååã@ plan_handle-æ¢åã®å®è¡ãã©ã³ã®ãã³ãã«ãããã³@statement_start_offset-ã¬ã€ããäœæããå¿ èŠããããããå ã®ã¹ããŒãã¡ã³ãã®éå§ãå®çŸ©ããŸãã
è©ŠããŠã¿ãïŒ
exec sp_create_plan_guide_from_handle N'PG_dboPosts_from_handle'
, 0x0600050018263314F048E3652102000001000000000000000000000000000000000000000000000000000000
, NULL;
ãããŠä»ãSSMSã§Programmability-> Plan Guidesã«ãããã®ã確èªã
ãŸããçŸåšã®å®è¡ãã©ã³ã¯ãPlan Guide 'PG_dboPosts_from_handle'ã䜿çšããŠãªã¯ãšã¹ãã«ãéä»ãããããŠããŸãããäœããããã»ãšãã©ãã¹ãŠã®ãªããžã§ã¯ããšåæ§ã§ãSSMSã§ã¯ãå¿ èŠãªæ¹æ³ã§ã¹ã¯ãªãããäœæããŠåäœæã§ããŸãã
RMBãã¹ã¯ãªãã->ããããããŠäœæãããšã@ hintsãã©ã¡ãŒã¿ã®å€ãå¿ èŠãªãã®ã«çœ®ãæããå¿ èŠãããæ¢è£œã®ã¹ã¯ãªãããåŸããããããçµæãšããŠæ¬¡ã®ããã«ãªããŸãã
USE [StackOverflow2013]
GO
/****** Object: PlanGuide PG_dboPosts_from_handle Script Date: 05.07.2020 16:25:04 ******/
EXEC sp_control_plan_guide @operation = N'DROP', @name = N'[PG_dboPosts_from_handle]'
GO
/****** Object: PlanGuide PG_dboPosts_from_handle Script Date: 05.07.2020 16:25:04 ******/
EXEC sp_create_plan_guide @name = N'[PG_dboPosts_from_handle]', @stmt = N'SELECT *
FROM dbo.Posts
WHERE CHARINDEX (@p0, Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY', @type = N'SQL', @module_or_batch = N'SELECT *
FROM dbo.Posts
WHERE CHARINDEX (@p0, Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY;',
@params = N'@p0 nvarchar(250), @p1 int, @p2 int',
@hints = N'OPTION (TABLE HINT (dbo.Posts, INDEX (ix_Id_Title)))'
GO
ãªã¯ãšã¹ããå®è¡ããŠåå®è¡ããŸãããã£ãšããã¹ãŠãæ©èœ
ããŸãããã©ã¡ãŒã¿å€ã眮ãæãããšããã¹ãŠãåãããã«æ©èœããŸãã
1ã€ã®ã¹ããŒãã¡ã³ãã«å¯Ÿå¿ã§ããã¬ã€ãã¯1ã€ã ãã§ããããšã«æ³šæããŠãã ãããåãã¹ããŒãã¡ã³ãã«å¥ã®ã¬ã€ããè¿œå ããããšãããšããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã
ã¡ãã»ãŒãž10502ãã¬ãã«16ãç¶æ 1ãè¡1ã§
æå®ãããã¹ããŒãã¡ã³ãã«ããããã©ã³ã¬ã€ã 'PG_dboPosts_from_handle2'ãäœæã§ããŸããstmtããã³@module_or_batchããŸãã¯@plan_handleãš@statement_start_offsetã«ãã£ãŠãããŒã¿ããŒã¹å ã®æ¢åã®ãã©ã³ã¬ã€ã 'PG_dboPosts_from_handle'ãšäžèŽããŸããæ°ãããã©ã³ã¬ã€ããäœæããåã«ãæ¢åã®ãã©ã³ã¬ã€ããåé€ããŠãã ããã
æåŸã«è§ŠããŠããããã®ã¯ãsp_control_plan_guideã¹ãã¢ãããã·ãŒãžã£ã§ãã
ãã®å©ããåããŠããã©ã³ã¬ã€ããåé€ãç¡å¹åãæå¹åã§ããŸã-äžåºŠã«1ã€ãã€ãååã瀺ãããã¹ãŠã®ã¬ã€ãïŒããããããŸãã-ãŸãã¯ãã¹ãŠããŸãã¯ããã·ãŒãžã£ãå®è¡ãããããŒã¿ããŒã¹ã®ã³ã³ããã¹ãå ã®ãã¹ãŠïŒ-ãã®ãããå€@operationãã©ã¡ãŒã¿ãŒ-DROP ALLãDISABLE ALLãENABLE ALLãç¹å®ã®ãã©ã³ã«HPã䜿çšããäŸãããäžã«ç€ºããŸããæå®ããååã®ç¹å®ã®ãã©ã³ã¬ã€ãã¯åé€ãããŸãã
ãã³ããšèšç»ã¬ã€ããªãã§å®è¡ã§ããŸãããïŒ
äžè¬ã«ãã¯ãšãªãªããã£ãã€ã¶ãŒãæãã§ããçš®ã®ã²ãŒã ãå®è¡ããŠããããã«æããã99ïŒ ã®ç¢ºçã§ïŒç§ã®å Žåã®ããã«ïŒããçš®ã®ã²ãŒã ãå®è¡ããŠããããšãã©ãã»ã©æè¯ããç¥ã£ãŠããå Žåããã ãããªã¯ãšã¹ãããã¹ãã«çŽæ¥åœ±é¿ãäžããããšãã§ããªãå Žåã¯ããªã¯ãšã¹ãã«ãã³ããè¿œå ã§ãããã©ã³ã¬ã€ããåœã®æ©äººã«ãªãããšãã§ããŸããå¿ èŠã«å¿ããŠãªã¯ãšã¹ãããã¹ããæžãæããèœåããããšããŸããã-ããã§äœãå€æŽã§ããŸããïŒæ¿ç¥ããŸããïŒå šææ€çŽ¢ã®åœ¢åŒã§ããšããŸããã¯ãã䜿çšããªããŠããå®éã«ã¯ããã§äœ¿çšããå¿ èŠããããŸããããšãã°ããã®ãããªã¯ãšãªã«ã¯ãå®å šã«éåžžã®ïŒã¯ãšãªã®ïŒèšç»ãšå®è¡çµ±èšããããŸãã
;WITH c AS (
SELECT p2.id
FROM dbo.Posts p2
WHERE CHARINDEX (N'Aptana', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY
)
SELECT p.*
FROM dbo.Posts p
JOIN c ON p.id = c.id;
ããŒãã«ãæçš¿ããã¹ãã£ã³ã«ãŠã³ã1ãè«çèªã¿åã6250ãç©çèªã¿åã0ãå èªã¿èªã¿åã0ãLOBè«çèªã¿åã0ãLOBç©çèªã¿åã0ãLOBå èªã¿èªã¿åã0ãSQLServer
å®è¡æéïŒ
CPUæé= 1500ããªç§
SQL Serverã¯æåã«ix_Id_Titleã® "ã¯ããŒã«ããã"ã€ã³ããã¯ã¹ã«ãã£ãŠå¿ èŠãª25ã®èå¥åãèŠã€ããããããã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ã§éžæãããèå¥åãæ€çŽ¢ããŸã-ã¬ã€ããããåªããŠããŸãïŒãããããããŒã¿ãã«å¯ŸããŠã¯ãšãªãå®è¡ãã20,000è¡ç®ãã25è¡ã衚瀺ãããšã©ããªããŸããã
;WITH c AS (
SELECT p2.id
FROM dbo.Posts p2
WHERE CHARINDEX (N'Data', Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET 20000 ROWS FETCH NEXT 25 ROWS ONLY
)
SELECT p.*
FROM dbo.Posts p
JOIN c ON p.id = c.id;
ããŒãã«ãæçš¿ããã¹ãã£ã³ã«ãŠã³ã1ãè«çèªã¿åã5914ãç©çèªã¿åã0ãå èªã¿èªã¿åã0ãLOBè«çèªã¿åã11ãLOBç©çèªã¿åã0ãLOBå èªã¿èªã¿åã0ãSQLServer
å®è¡æéïŒ
CPUæé= 1453ããªç§
exec sp_executesql
N'SELECT *
FROM dbo.Posts
WHERE CHARINDEX (@p0, Title) > 1
ORDER BY ROW_NUMBER() OVER (ORDER BY Id DESC)
OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY;'
, N'@p0 nvarchar(250), @p1 int, @p2 int'
, @p0 = N'Data', @p1 = 20000, @p2 = 25;
Table 'Posts'. Scan count 1, logical reads 87174, physical reads 0, read-ahead reads 0, lob logical reads 11, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 1437 ms
ã¯ããcharindexã«è²»ãããããããããã»ããµæéã¯åãã§ãããã¬ã€ãã䜿çšãããªã¯ãšã¹ãã§ã¯ãæ¡éãã«å€ãã®èªã¿åããè¡ããããããåé¡ã«ãªãå¯èœæ§ããããŸãã
æçµçµæããŸãšããŸãããããã³ããšã¬ã€ãã¯ãä»ããã§ããªããå©ããããšãã§ããŸãããç©äºãããã«æªåãããå¯èœæ§ããããŸãããªã¯ãšã¹ãããã¹ãã§ã€ã³ããã¯ã¹ä»ãã®ãã³ããæ瀺çã«æå®ããŠããã€ã³ããã¯ã¹ãåé€ãããšãã¯ãšãªãå®è¡ã§ããªããªããŸãã SQL Server 2017ã§ã¯ãã€ã³ããã¯ã¹ãåé€ããåŸãã¬ã€ãä»ãã®ã¯ãšãªã¯æ£åžžã«å®è¡ãããŸã-ã¬ã€ãã¯ç¡èŠãããŸãããSQL Serverã®ãã¹ãŠã®ããŒãžã§ã³ã§åžžã«ãã®ããã«ãªããšã¯éããŸããã
ãã·ã¢èªã®ãã©ã³ã¬ã€ãã«ã€ããŠã¯ããŸãæ å ±ããªãã®ã§ãèªåã§æžãããšã«ããŸãããããã§èªãããšãã§ããŸããã©ã³ã¬ã€ãã®äœ¿çšã«é¢ããå¶éã«ã€ããŠãç¹ã«ãPGã䜿çšããŠãã³ãã䜿çšããŠã€ã³ããã¯ã¹ãæ瀺çã«ç€ºããšããªã¯ãšã¹ãã倱æããå Žåããããšããäºå®ã«ã€ããŠãç§ã¯ããªãããããã決ããŠäœ¿çšããªãããšãæã¿ãŸãããããŠãããããªãã-ããã幞éãªã-ãããã©ãã«ã€ãªããã®ãç¥ã£ãŠãããªãã