Warning: Constant WP_TEMP_DIR already defined in /var/www/html/blogs/joe/wp-config.php on line 93

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blogs/joe/wp-config.php:93) in /var/www/html/blogs/joe/wp-includes/rest-api/class-wp-rest-server.php on line 1902
{"id":831,"date":"2013-01-09T16:07:44","date_gmt":"2013-01-10T00:07:44","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/joe\/?p=831"},"modified":"2013-02-07T05:13:23","modified_gmt":"2013-02-07T13:13:23","slug":"row-and-page-count-influence-on-memory-grants","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/","title":{"rendered":"Row and Page Count Influence on Sort Memory Grants"},"content":{"rendered":"

My prior post demonstrated the influence of row and page counts on estimated CPU and I\/O cost<\/a>.\u00a0 Now in this post I’m going to step through the influence of row and page counts on memory grants for a Sort operator.\u00a0 I’ll be using the MemoryGrantInfo element from the query execution plan to measure the influence.\u00a0\u00a0I wrote\u00a0about this element\u00a0in\u00a0my\u00a0“Memory Grant Execution Plan Statistics<\/a>” blog post.<\/p>\n

Starting State<\/strong><\/p>\n

I’ll be querying the member table which has 142 data pages and 10,000 rows.\u00a0 The query itself will\u00a0have an ORDER BY curr_balance.\u00a0 The curr_balance column is currently not supported by an existing index and will require a Sort operator in the plan, which then means that a memory grant will be needed:<\/p>\n

\r\nSELECT\u00a0 [member].[member_no],\r\n[member].[lastname],\r\n[curr_balance]\r\nFROM\u00a0\u00a0\u00a0 [dbo].[member]\r\nORDER BY [curr_balance] ASC\r\nOPTION\u00a0 (RECOMPILE, MAXDOP 1);\r\n<\/pre>\n

Using an actual execution plan, I see the following MemoryGrantInfo data for the baseline query:<\/p>\n

<MemoryGrantInfo
\nSerialRequiredMemory=”512″
\nSerialDesiredMemory=”1904″
\nRequiredMemory=”512″
\nDesiredMemory=”1904″
\nRequestedMemory=”1904″
\nGrantWaitTime=”0″
\nGrantedMemory=”1904″
\nMaxUsedMemory=”680″ \/><\/p>\n

I’m forcing a serial plan, so you’ll see that the SerialRequiredMemory matches the RequiredMemory attribute and the SerialDesiredMemory matches the DesiredMemory attribute (and others as well).\u00a0 I used this hint for this blog post to maintain an apples-to-apples comparison (serial plan vs. serial plan).<\/p>\n

The Sort operator has the following CPU and I\/O costs (not including subtree costs):<\/p>\n

    \n
  • Estimated CPU Cost = 0.60342<\/li>\n
  • Estimated I\/O Cost = 0.0112613<\/li>\n<\/ul>\n

    Inflating the page count while maintaining the row count<\/strong><\/p>\n

    What happens if we inflate the page count while maintaining the row count?<\/p>\n

    \r\n-- Please don't use this in production - for demonstration purposes only!\r\nUPDATE STATISTICS dbo.[member]\r\nWITH ROWCOUNT = 10000, PAGECOUNT = 149000;\r\n<\/pre>\n

    Re-executing the query, I see the following memory grant statistics:<\/p>\n

    <MemoryGrantInfo
    \nSerialRequiredMemory=”512″
    \nSerialDesiredMemory=”1904″
    \nRequiredMemory=”512″
    \nDesiredMemory=”1904″
    \nRequestedMemory=”1904″
    \nGrantWaitTime=”0″
    \nGrantedMemory=”1904″
    \nMaxUsedMemory=”680″ \/><\/p>\n

    The memory grant statistics for the 149000 page plan are identical to the 142 page plan.<\/p>\n

    The Sort operator has the following CPU and I\/O costs (excluding subtree costs):<\/p>\n

      \n
    • Estimated CPU Cost = 0.60342<\/li>\n
    • Estimated I\/O Cost = 0.0112613<\/li>\n<\/ul>\n

      So we see identical CPU and I\/O costs for the Sort operator even with inflated page counts – even though the leaf-level Clustered Index Scan itself has an inflated estimated I\/O cost (110.373).<\/p>\n

      Inflating the row count while maintaining the page count<\/strong><\/p>\n

      What happens if I inflate the row count while maintaining the page count (at the original 142 pages)?<\/p>\n

        \r\nUPDATE STATISTICS dbo.[member]\r\nWITH ROWCOUNT = 100000000, PAGECOUNT = 142;\r\n<\/pre>\n

      This time we\u00a0see a difference:<\/p>\n

      <MemoryGrantInfo
      \nSerialRequiredMemory=”512″
      \nSerialDesiredMemory=”8564592″<\/span>
      \nRequiredMemory=”512″
      \nDesiredMemory=”8564592″<\/span>
      \nRequestedMemory=”4579608″<\/span>
      \nGrantWaitTime=”0″
      \nGrantedMemory=”4579608″<\/span>
      \nMaxUsedMemory=”680″ \/><\/p>\n

      SerialRequiredMemory and RequiredMemory are the same as prior scenarios (512) – but we see a big spike in SerialDesiredMemory, DesiredMemory, RequestedMemory and GrantedMemory.<\/p>\n

      The Sort operator has the following CPU and I\/O costs (excluding subtree costs):<\/p>\n

        \n
      • Estimated CPU Cost = 5003.06<\/li>\n
      • Estimated I\/O Cost = 7875.01<\/li>\n<\/ul>\n

        So we see that the Sort operator CPU and I\/O cost is sensitive to row counts, but not data page counts (and the leaf-level Clustered Index Scan shows a 0.107569 estimated I\/O cost and a 110 estimated CPU cost).<\/p>\n

        Summary<\/strong><\/p>\n

        So recapping what we see when manipulating row and page counts for a query that uses a Sort operator:<\/p>\n

          \n
        • The memory grant introduced by the Sort operator was sensitive to row counts, but not<\/em> page counts.<\/li>\n
        • The Sort operator’s estimated CPU and I\/O costs were both sensitive to row counts, but not page counts.<\/li>\n<\/ul>\n

          \n","protected":false},"excerpt":{"rendered":"

          My prior post demonstrated the influence of row and page counts on estimated CPU and I\/O cost.\u00a0 Now in this post I’m going to step through the influence of row and page counts on memory grants for a Sort operator.\u00a0 I’ll be using the MemoryGrantInfo element from the query execution plan to measure the influence.\u00a0\u00a0I […]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-performance"],"yoast_head":"\nRow and Page Count Influence on Sort Memory Grants - Joe Sack<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Row and Page Count Influence on Sort Memory Grants - Joe Sack\" \/>\n<meta property=\"og:description\" content=\"My prior post demonstrated the influence of row and page counts on estimated CPU and I\/O cost.\u00a0 Now in this post I’m going to step through the influence of row and page counts on memory grants for a Sort operator.\u00a0 I’ll be using the MemoryGrantInfo element from the query execution plan to measure the influence.\u00a0\u00a0I […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/\" \/>\n<meta property=\"og:site_name\" content=\"Joe Sack\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-10T00:07:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-02-07T13:13:23+00:00\" \/>\n<meta name=\"author\" content=\"Joseph Sack\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Joseph Sack\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/\",\"name\":\"Row and Page Count Influence on Sort Memory Grants - Joe Sack\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/#website\"},\"datePublished\":\"2013-01-10T00:07:44+00:00\",\"dateModified\":\"2013-02-07T13:13:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/#\/schema\/person\/533eb0113a15fb5a6e8067a49e4ae648\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Performance\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/category\/performance\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Row and Page Count Influence on Sort Memory Grants\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/\",\"name\":\"Joe Sack\",\"description\":\"SQL Server Performance Tuning, High Availability and Disaster Recovery Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/#\/schema\/person\/533eb0113a15fb5a6e8067a49e4ae648\",\"name\":\"Joseph Sack\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a4b39a7719a6bfff1add3ec00527810734579ee114d6d983e8e68f937b77be96?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a4b39a7719a6bfff1add3ec00527810734579ee114d6d983e8e68f937b77be96?s=96&d=mm&r=g\",\"caption\":\"Joseph Sack\"},\"description\":\"Joe Sack is a Principal Consultant with SQLskills. He has worked as a SQL Server professional since 1997 and has supported and developed for SQL Server environments in financial services, IT consulting, manufacturing, retail and the real estate industry. Prior to joining SQLskills he worked at Microsoft as a Premier Field Engineer supporting very large enterprise customer environments. He was responsible for providing deep SQL Server advisory services, training, troubleshooting and ongoing solutions guidance. His areas of expertise include performance tuning, scalability, T-SQL development and high-availability. In 2006 Joe earned the \u201cMicrosoft Certified Master: SQL Server 2005\u201d certification and in 2008 he earned the \u201cMicrosoft Certified Master: SQL Server 2008\u201d certification. In 2009 he took over responsibility for the entire SQL Server Microsoft Certified Master program and held that post until 2011. He was given the SQL Server MVP award in 2013.\",\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/joe\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/josephsack\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/joe\/author\/joe\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Row and Page Count Influence on Sort Memory Grants - Joe Sack","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/","og_locale":"en_US","og_type":"article","og_title":"Row and Page Count Influence on Sort Memory Grants - Joe Sack","og_description":"My prior post demonstrated the influence of row and page counts on estimated CPU and I\/O cost.\u00a0 Now in this post I’m going to step through the influence of row and page counts on memory grants for a Sort operator.\u00a0 I’ll be using the MemoryGrantInfo element from the query execution plan to measure the influence.\u00a0\u00a0I […]","og_url":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/","og_site_name":"Joe Sack","article_published_time":"2013-01-10T00:07:44+00:00","article_modified_time":"2013-02-07T13:13:23+00:00","author":"Joseph Sack","twitter_misc":{"Written by":"Joseph Sack","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/","url":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/","name":"Row and Page Count Influence on Sort Memory Grants - Joe Sack","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/#website"},"datePublished":"2013-01-10T00:07:44+00:00","dateModified":"2013-02-07T13:13:23+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/#\/schema\/person\/533eb0113a15fb5a6e8067a49e4ae648"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/row-and-page-count-influence-on-memory-grants\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/joe\/"},{"@type":"ListItem","position":2,"name":"Performance","item":"https:\/\/www.sqlskills.com\/blogs\/joe\/category\/performance\/"},{"@type":"ListItem","position":3,"name":"Row and Page Count Influence on Sort Memory Grants"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/joe\/","name":"Joe Sack","description":"SQL Server Performance Tuning, High Availability and Disaster Recovery Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/joe\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/#\/schema\/person\/533eb0113a15fb5a6e8067a49e4ae648","name":"Joseph Sack","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/joe\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a4b39a7719a6bfff1add3ec00527810734579ee114d6d983e8e68f937b77be96?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a4b39a7719a6bfff1add3ec00527810734579ee114d6d983e8e68f937b77be96?s=96&d=mm&r=g","caption":"Joseph Sack"},"description":"Joe Sack is a Principal Consultant with SQLskills. He has worked as a SQL Server professional since 1997 and has supported and developed for SQL Server environments in financial services, IT consulting, manufacturing, retail and the real estate industry. Prior to joining SQLskills he worked at Microsoft as a Premier Field Engineer supporting very large enterprise customer environments. He was responsible for providing deep SQL Server advisory services, training, troubleshooting and ongoing solutions guidance. His areas of expertise include performance tuning, scalability, T-SQL development and high-availability. In 2006 Joe earned the \u201cMicrosoft Certified Master: SQL Server 2005\u201d certification and in 2008 he earned the \u201cMicrosoft Certified Master: SQL Server 2008\u201d certification. In 2009 he took over responsibility for the entire SQL Server Microsoft Certified Master program and held that post until 2011. He was given the SQL Server MVP award in 2013.","sameAs":["http:\/\/3.209.169.194\/blogs\/joe","https:\/\/twitter.com\/https:\/\/twitter.com\/josephsack"],"url":"https:\/\/www.sqlskills.com\/blogs\/joe\/author\/joe\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/joe\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}