{"id":1962,"date":"2015-02-22T18:00:34","date_gmt":"2015-02-22T16:00:34","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=1962"},"modified":"2015-02-22T18:11:34","modified_gmt":"2015-02-22T16:11:34","slug":"sql-fejtoro-7-megoldas","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2015\/02\/22\/sql-fejtoro-7-megoldas\/","title":{"rendered":"SQL fejt\u00f6r\u0151 7. &#8211; megold\u00e1s"},"content":{"rendered":"<p>K\u00e9rd\u00e9s: meg tudom-e n\u00e9zni egy hosszan fut\u00f3 SQL DML lek\u00e9rdez\u00e9sn\u00e9l, hogy a m\u00f6g\u00f6tte fut\u00f3 oper\u00e1torok (join, index seek, stb.) h\u00e1ny sz\u00e1zal\u00e9kn\u00e1l j\u00e1rnak?<\/p>\n<p>SQL Server 2014-t\u0151l igen!<\/p>\n<p>N\u00e9zz\u00fck a k\u00f6vetkez\u0151 (buta) lek\u00e9rdez\u00e9st: <\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSET STATISTICS PROFILE ON;\r\nGO\r\n--Next, run your query in this session\r\nselect * from &#x5B;dbo].&#x5B;Nums] n1 cross join dbo.Nums n2;\r\n<\/pre>\n<p>Ez egy lass\u00fa, sok soros cross join, csak dem\u00f3 c\u00e9lra. A vizsg\u00e1land\u00f3 lek\u00e9rdez\u00e9st olyan sessionben kell futtatni, amiben a STATISTICS PROFILE be van kapcsolva.<\/p>\n<p>\u00cdgy n\u00e9z ki a v\u00e9grehajt\u00e1si terve:<\/p>\n<p><a href=\"http:\/\/soci.hu\/blog\/wp-content\/uploads\/2015\/02\/CrossJoinPlan.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/soci.hu\/blog\/wp-content\/uploads\/2015\/02\/CrossJoinPlan-300x66.png\" alt=\"CrossJoinPlan\" width=\"300\" height=\"66\" class=\"alignnone size-medium wp-image-1963\" srcset=\"https:\/\/soci.hu\/blog\/wp-content\/uploads\/2015\/02\/CrossJoinPlan-300x66.png 300w, https:\/\/soci.hu\/blog\/wp-content\/uploads\/2015\/02\/CrossJoinPlan-600x132.png 600w, https:\/\/soci.hu\/blog\/wp-content\/uploads\/2015\/02\/CrossJoinPlan.png 884w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>A terv fontos a k\u00f6vetkez\u0151kh\u00f6z, az\u00e9rt raktam ide. \u00c9s most j\u00f6n a l\u00e9nyeg: sys.dm_exec_query_profiles. \u0150 mutatja meg, bel\u00fcl mi zajlik:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT  \r\n   node_id,physical_operator_name, SUM(row_count) row_count, SUM(estimate_row_count) AS estimate_row_count, \r\n   CAST(SUM(row_count)*100 AS float) \/ SUM(estimate_row_count) PercentComplete\r\nFROM sys.dm_exec_query_profiles \r\n--WHERE session_id=54\r\nGROUP BY node_id,physical_operator_name\r\nORDER BY node_id;\r\n<\/pre>\n<p>A v\u00e9grehajt\u00e1s kezdet\u00e9n \u00edgy n\u00e9z ki a kimenete (ha valaki tud tippet adni, hogy lehet ezt SyntaxHighlighterrel j\u00f3l form\u00e1zni megk\u00f6sz\u00f6n\u00f6m):<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnode_id\tphysical_operator_name\trow_count\testimate_row_count\tPercentComplete\r\n0\tParallelism\t\t1320422\t\t4294967295\t\t0.030743470422631\r\n1\tNested Loops\t\t1323284\t\t4294967296\t\t0.0308101065456867\r\n2\tClustered Index Scan\t15\t\t100000\t\t\t0.015\r\n3\tTable Spool\t\t1323284\t\t1410065408\t\t0.0938455757082157\r\n4\tClustered Index Scan\t400000\t\t400000\t\t\t100\r\n<\/pre>\n<p>Egy perccel k\u00e9s\u0151bb:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnode_id\tphysical_operator_name\trow_count\testimate_row_count\tPercentComplete\r\n0\tParallelism\t15917761\t4294967295\t0.370614254002137\r\n1\tNested Loops\t15920504\t4294967296\t0.370678119361401\r\n2\tClustered Index Scan\t161\t100000\t0.161\r\n3\tTable Spool\t15920504\t1410065408\t1.12906138322911\r\n4\tClustered Index Scan\t400000\t400000\t100\r\n<\/pre>\n<p>Mit jelen ez? A terv alapj\u00e1n van k\u00e9t clustered index scan\u00fcnk. Az egyik felolvasta a t\u00e1bl\u00e1ban tal\u00e1lhat\u00f3 400000 sort, azzal \u0151 v\u00e9gzett is. Azt\u00e1n a cilusos join elkezdi hozz\u00e1venni a t\u00e1bla sorait, minden sorhoz 400000-et. Az eg\u00e9sz eredm\u00e9nye beleker\u00fcl a Table Spoolba, majd csurog ki a Parallelism oper\u00e1toron kereszt\u00fcl (ami Gather Stream m\u0171veletet hajt v\u00e9gre, azaz \u00f6sszegy\u0171jti a t\u00f6bb sz\u00e1l \u00e1ltal \u00f6sszerakott sorokat egy streambe, mivel p\u00e1rhuzamos a terv).<\/p>\n<p>L\u00e1ssuk be, marha \u00e9rdekes l\u00e1tni, hogy \u00e1ll bel\u00fcl egy nagyobb DML m\u0171velet, nem?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>K\u00e9rd\u00e9s: meg tudom-e n\u00e9zni egy hosszan fut\u00f3 SQL DML lek\u00e9rdez\u00e9sn\u00e9l, hogy a m\u00f6g\u00f6tte fut\u00f3 oper\u00e1torok (join, index seek, stb.) h\u00e1ny sz\u00e1zal\u00e9kn\u00e1l j\u00e1rnak?&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4,30,87,93],"tags":[],"class_list":["post-1962","post","type-post","status-publish","format-standard","hentry","category-adatbazisok","category-szakmai-elet","category-sql-server","category-sql-server-2014","category-teaser"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1962"}],"version-history":[{"count":3,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1962\/revisions"}],"predecessor-version":[{"id":1966,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1962\/revisions\/1966"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}