{"id":243082,"date":"2025-06-25T08:38:00","date_gmt":"2025-06-25T15:38:00","guid":{"rendered":"https:\/\/virtual-dba.com\/?p=243082"},"modified":"2025-11-13T10:40:29","modified_gmt":"2025-11-13T17:40:29","slug":"billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it","status":"publish","type":"post","link":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/","title":{"rendered":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-summary\">SUMMARY:<\/h2>\n\n\n\n<p>Database professionals facing surprisingly slow <code>SELECT MAX()<\/code> queries on massive SQL Server tables can eliminate costly billion-row Clustered Index Scans by intentionally introducing a non-partitioned Non-Clustered Index to provide the query optimizer a globally sorted shortcut.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Table partitioning<\/strong> is a double-edged sword that, despite aiding large table management, can break the contiguous sorting structure of a clustered index, forcing the SQL Server optimizer to scan every partition to find the true global maximum.<\/li>\n\n\n\n<li>Creating a non-partitioned <strong>Non-Clustered Index<\/strong> on a column already covered by the clustered index is necessary to create a unified, globally sorted B-tree, enabling the optimizer to find the maximum value instantly.<\/li>\n\n\n\n<li>Do not trust the graphical <strong>execution plan icon<\/strong> alone; detailed analysis of the plan&#8217;s properties\u2014specifically checking for <code>ScanDirection = BACKWARD<\/code> and <code>ActualRowsRead = 1<\/code>\u2014confirms the high-efficiency seek operation.<\/li>\n\n\n\n<li>Redundant indexing and declaring the index as <strong>UNIQUE<\/strong> (when the column, like an <code>IDENTITY<\/code> column, guarantees uniqueness) are sometimes necessary strategic trade-offs that exchange minimal storage cost for massive performance gains.<\/li>\n<\/ul>\n\n\n\n<p>Understanding deeper architectural features, such as how table partitioning affects a clustered index&#8217;s global sort order, is crucial for correctly diagnosing and resolving these deceptively simple performance bottlenecks.<\/p>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-summary\" data-level=\"2\">SUMMARY:<\/a><\/li><li><a href=\"#h-a-simple-query-with-a-devious-problem\" data-level=\"2\">A Simple Query with a Devious Problem<\/a><\/li><li><a href=\"#h-when-partitioning-works-against-you\" data-level=\"2\">When Partitioning Works Against You<\/a><\/li><li><a href=\"#h-the-counterintuitive-fix-adding-a-duplicate-key\" data-level=\"2\">The Counterintuitive Fix: Adding a &#8220;Duplicate&#8221; Key<\/a><\/li><li><a href=\"#h-don-t-trust-the-icon-read-the-real-execution-plan\" data-level=\"2\">Don&#8217;t Trust the Icon: Read the Real Execution Plan<\/a><\/li><li><a href=\"#h-key-takeaways-for-data-professionals\" data-level=\"2\">Key Takeaways for Data Professionals<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-a-simple-query-with-a-devious-problem\">A Simple Query with a Devious Problem<\/h2>\n\n\n\n<p>It&#8217;s a scenario every database professional has encountered: a seemingly simple query brings a powerful server to its knees. In this case, <strong>64 Core \/ 1TB RAM \/ all SSD.<\/strong><\/p>\n\n\n\n<p>\u2026 AND the query was as basic as it gets:<\/p>\n\n\n\n<p><code>SELECT @LastMaxID = MAX(AttributeID) FROM dbo.HugeTable;<\/code><\/p>\n\n\n\n<p>The <code>AttributeID<\/code> column is a <code>BIGINT<\/code> and the table&#8217;s <code>IDENTITY<\/code> column. It&#8217;s also the leading column of the clustered index. By all accounts, this query should be instantaneous. The SQL Server optimizer should simply navigate to the end of the clustered index, pick off the last value, and be done.<\/p>\n\n\n\n<p>Instead, the execution plan revealed a costly <code>Clustered Index Scan<\/code>. The server was reading the entire table\u2014all 1.78 billion rows of it\u2014to find a single value. The query took ages, consumed massive I\/O, and left me scratching my head. How could the optimizer make such a poor choice?<\/p>\n\n\n\n<p>The answer wasn&#8217;t in the query or the index itself, but in a deeper architectural feature: table partitioning.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-when-partitioning-works-against-you\">When Partitioning Works Against You<\/h2>\n\n\n\n<p>Our HugeTable was partitioned by a <code>YYYYMM <\/code>date column. Partitioning is a fantastic feature for managing enormous datasets; it allows for efficient data loading, archiving, and partition-level maintenance. The clustered index key included this partition column to ensure it was &#8220;partition-aligned,&#8221; which is a standard best practice.<\/p>\n\n\n\n<p><code>CREATE UNIQUE CLUSTERED INDEX ... ON HugeTable(AttributeID, PartitionYYYYMM)<\/code><\/p>\n\n\n\n<p>This alignment is typically great for performance, but it was the source of our <code>MAX()<\/code> query problem. A partitioned table isn&#8217;t one single, physically sorted structure. It&#8217;s a collection of many smaller, individually sorted B-trees.<\/p>\n\n\n\n<p>For the optimizer, this means there is no single &#8220;end of the table.&#8221; The highest <code>AttributeID<\/code> could be at the end of <em>any<\/em> of those partitions. To guarantee a correct result, the optimizer concluded it had no choice but to scan every row in every partition to find the true global maximum.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-counterintuitive-fix-adding-a-duplicate-key\">The Counterintuitive Fix: Adding a &#8220;Duplicate&#8221; Key<\/h2>\n\n\n\n<p>The solution initially felt wrong: I needed to add another index on the exact same column.<\/p>\n\n\n\n<p><code>CREATE UNIQUE NONCLUSTERED INDEX IX_HugeTable_AttributeID ON dbo.HugeTable(AttributeID);<\/code><\/p>\n\n\n\n<p>Wait, create a non-clustered index on a column that&#8217;s already the lead key of the clustered index? Isn&#8217;t that just duplicating data and wasting space?<\/p>\n\n\n\n<p>Yes, it is. And in this case, it was absolutely necessary.<\/p>\n\n\n\n<p>This is a classic &#8220;space vs. speed&#8221; trade-off. The key difference is that this new non-clustered index was not partitioned. By default, it was created on the primary filegroup as a single, globally sorted B-tree. It contained a sorted list of every <code>AttributeID <\/code>from the entire table, from all partitions, in one unified structure.<\/p>\n\n\n\n<p>This gave the query optimizer the &#8220;shortcut&#8221; it was missing. With this index in place, finding the <code>MAX(AttributeID)<\/code> became trivial again:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Identify the new, non-partitioned index.<\/li>\n\n\n\n<li>Navigate directly to the last page of that single index structure.<\/li>\n\n\n\n<li>Read the value.<\/li>\n<\/ol>\n\n\n\n<p>The cost of storing this &#8220;duplicate&#8221; key\u2014a <code>BIGINT<\/code> value for each of the billions of rows\u2014was a small price to pay for turning a multi-hour query into a millisecond operation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-don-t-trust-the-icon-read-the-real-execution-plan\">Don&#8217;t Trust the Icon: Read the Real Execution Plan<\/h2>\n\n\n\n<p>After creating the index, I reran the query. The new execution plan was dramatically cheaper, but one thing was visually confusing: the primary operator was still an<code> Index Scan<\/code>.<\/p>\n\n\n\n<p>If you only look at the GUI, you might think the problem isn&#8217;t solved. This is where the true value of reading the plan&#8217;s XML or properties comes in. When I examined the details of the<code> Index Scan<\/code> Operator, I saw the real story:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ScanDirection = BACKWARD:<\/strong> It wasn&#8217;t starting from the beginning; it started from the end of the index.<\/li>\n\n\n\n<li><strong>A Top Operator:<\/strong> The scan was feeding rows into a Top (1) operator, meaning it was instructed to stop after finding the very first row.<\/li>\n\n\n\n<li><strong>ActualRowsRead = 1:<\/strong> The runtime statistics confirmed it! To find the maximum value, the engine only had to read a single row from the index.<\/li>\n\n\n\n<li><strong>ActualLogicalReads = 4:<\/strong> The entire operation was completed by reading only four data pages from memory.<\/li>\n<\/ul>\n\n\n\n<p>What the icon called a &#8220;scan&#8221; was, in reality, a hyper-efficient seek to the last entry in the B-tree. The billion-row read operation was gone, replaced by an operation so fast it was essentially free.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-key-takeaways-for-data-professionals\"><strong>Key Takeaways for Data Professionals<\/strong><\/h2>\n\n\n\n<p>This journey from a crippling scan to a near-instant query offers several important lessons:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Partitioning is a Double-Edged Sword:<\/strong> While essential for managing very large tables, partitioning can introduce performance complexities. Understand how it changes the physical data structure and affects optimizer choices.<\/li>\n\n\n\n<li><strong>Redundant Indexes Aren&#8217;t Always Redundant:<\/strong> An index that seems to duplicate another can serve a completely different purpose. Our non-partitioned index provided a global view that the partitioned clustered index could not.<\/li>\n\n\n\n<li><strong>Dig Deeper Than the GUI:<\/strong> The graphical execution plan is a great starting point, but the real insights are in the properties and the <code>XML. ActualRowsRead<\/code> and other runtime stats tell you what really happened.<\/li>\n\n\n\n<li><strong>Declare Your Intentions with UNIQUE:<\/strong> Because our <code>AttributeID<\/code> was an <code>IDENTITY<\/code> column, I defined the new index as <code>UNIQUE<\/code>. This not only enforces data integrity but also provides the optimizer with more accurate information to create even better plans for other queries.<\/li>\n<\/ol>\n\n\n\n<p>Next time you face a deceptively simple query with a shockingly bad execution plan, remember this story. The solution might be counterintuitive, but by understanding the underlying architecture, you can give the optimizer the tools it needs to get the job done right.<\/p>\n\n\n\n<p><a href=\"https:\/\/virtual-dba.com\/platforms\/sql-server\/\">Check out our SQL Admin and Support Services.<\/a><\/p>\n\n\n\n<p><strong>Please contact us for more information.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SUMMARY: Database professionals facing surprisingly slow SELECT MAX() queries on massive SQL Server tables can eliminate costly billion-row Clustered Index Scans by intentionally introducing a non-partitioned Non-Clustered Index to provide the query optimizer a globally sorted shortcut. Understanding deeper architectural features, such as how table partitioning affects a clustered index&#8217;s global sort order, is crucial [&hellip;]<\/p>\n","protected":false},"author":59,"featured_media":243087,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","content-type":"","footnotes":""},"categories":[4166,4158,55],"tags":[4165,63,60],"class_list":["post-243082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-microsoft","category-sql-server","tag-data-management","tag-microsoft","tag-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.1 (Yoast SEO v27.1.1) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>The Billion-Row Scan: Why Your SQL Query for MAX Is Slow<\/title>\n<meta name=\"description\" content=\"Is your SQL query for MAX taking forever? See why partitioning causes it\u2014and how a non-clustered index can solve it. Read the fix!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It\" \/>\n<meta property=\"og:description\" content=\"Is your SQL query for MAX taking forever? See why partitioning causes it\u2014and how a non-clustered index can solve it. Read the fix!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-25T15:38:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-13T17:40:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"557\" \/>\n\t<meta property=\"og:image:height\" content=\"291\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ken Haff\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virtual_dba\" \/>\n<meta name=\"twitter:site\" content=\"@virtual_dba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ken Haff\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\"},\"author\":{\"name\":\"Ken Haff\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365\"},\"headline\":\"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It\",\"datePublished\":\"2025-06-25T15:38:00+00:00\",\"dateModified\":\"2025-11-13T17:40:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\"},\"wordCount\":1127,\"publisher\":{\"@id\":\"https:\/\/virtual-dba.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg\",\"keywords\":[\"data management\",\"Microsoft\",\"sql server\"],\"articleSection\":[\"Blog\",\"Microsoft\",\"SQL Server\"],\"inLanguage\":\"en-US\",\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\",\"url\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\",\"name\":\"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow\",\"isPartOf\":{\"@id\":\"https:\/\/virtual-dba.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg\",\"datePublished\":\"2025-06-25T15:38:00+00:00\",\"dateModified\":\"2025-11-13T17:40:29+00:00\",\"description\":\"Is your SQL query for MAX taking forever? See why partitioning causes it\u2014and how a non-clustered index can solve it. Read the fix!\",\"breadcrumb\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage\",\"url\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg\",\"contentUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg\",\"width\":557,\"height\":291,\"caption\":\"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtual-dba.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtual-dba.com\/#website\",\"url\":\"https:\/\/virtual-dba.com\/\",\"name\":\"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts\",\"description\":\"Remote Database Administration\",\"publisher\":{\"@id\":\"https:\/\/virtual-dba.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtual-dba.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/virtual-dba.com\/#organization\",\"name\":\"Virtual-DBA: Remote DBA | Remote Database Administration\",\"alternateName\":\"Virtual-DBA powered by XTIVIA\",\"url\":\"https:\/\/virtual-dba.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg\",\"contentUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg\",\"width\":557,\"height\":291,\"caption\":\"Virtual-DBA: Remote DBA | Remote Database Administration\"},\"image\":{\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/virtual_dba\",\"https:\/\/www.linkedin.com\/showcase\/36220649\/\",\"https:\/\/www.youtube.com\/channel\/UCx3AIeUQ2ziTLKZSJDZ-SEg\"],\"description\":\"Eliminate database downtime and spiraling costs with XTIVIA\u2019s Virtual-DBA. In today\u2019s always-on business world, gaps in 24x7 on-call DBA support, neglected maintenance and security, or a stretched team struggling with overwhelming workloads can lead to costly disruptions and threaten business continuity. XTIVIA\u2019s Virtual-DBA provides the immediate, expert database administration you need, exactly when you need it, ensuring optimal performance, ironclad security, and significant cost savings without the burden of expanding your in-house team. The goal of Virtual-DBA is to provide a cost-effective solution for organizations seeking to optimize the security, management, maintenance, availability, and performance of their critical business systems, whether self-managed or cloud-managed (e.g., AWS RDS, Azure SQL Database). We accomplish this through a comprehensive remote DBA service offering designed specifically to meet the Oracle\u00ae, DB2\u00ae, Informix\u00ae, MySQL\u2122, PostgreSQL\u00ae, MongoDB\u00ae, MariaDB, and Microsoft SQL Server\u00ae, CockroachDB, Databricks, AWS, and Azure needs of our clients.\",\"email\":\"info@xtivia.com\",\"telephone\":\"8886853101\",\"legalName\":\"XTIVIA, Inc\",\"foundingDate\":\"1992-05-01\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"201\",\"maxValue\":\"500\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365\",\"name\":\"Ken Haff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g\",\"caption\":\"Ken Haff\"},\"url\":\"https:\/\/virtual-dba.com\/author\/ken-haff\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow","description":"Is your SQL query for MAX taking forever? See why partitioning causes it\u2014and how a non-clustered index can solve it. Read the fix!","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:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/","og_locale":"en_US","og_type":"article","og_title":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It","og_description":"Is your SQL query for MAX taking forever? See why partitioning causes it\u2014and how a non-clustered index can solve it. Read the fix!","og_url":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/","og_site_name":"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts","article_published_time":"2025-06-25T15:38:00+00:00","article_modified_time":"2025-11-13T17:40:29+00:00","og_image":[{"width":557,"height":291,"url":"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg","type":"image\/jpeg"}],"author":"Ken Haff","twitter_card":"summary_large_image","twitter_creator":"@virtual_dba","twitter_site":"@virtual_dba","twitter_misc":{"Written by":"Ken Haff","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#article","isPartOf":{"@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/"},"author":{"name":"Ken Haff","@id":"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365"},"headline":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It","datePublished":"2025-06-25T15:38:00+00:00","dateModified":"2025-11-13T17:40:29+00:00","mainEntityOfPage":{"@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/"},"wordCount":1127,"publisher":{"@id":"https:\/\/virtual-dba.com\/#organization"},"image":{"@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage"},"thumbnailUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg","keywords":["data management","Microsoft","sql server"],"articleSection":["Blog","Microsoft","SQL Server"],"inLanguage":"en-US","accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/","url":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/","name":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow","isPartOf":{"@id":"https:\/\/virtual-dba.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage"},"image":{"@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage"},"thumbnailUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg","datePublished":"2025-06-25T15:38:00+00:00","dateModified":"2025-11-13T17:40:29+00:00","description":"Is your SQL query for MAX taking forever? See why partitioning causes it\u2014and how a non-clustered index can solve it. Read the fix!","breadcrumb":{"@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#primaryimage","url":"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg","contentUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/The-Billion-Row-Scan-Why-Your-SQL-Query-for-MAX-Is-Slow-and-How-to-Fix-It.jpg","width":557,"height":291,"caption":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It"},{"@type":"BreadcrumbList","@id":"https:\/\/virtual-dba.com\/blog\/billion-row-scan-why-sql-query-for-max-is-slow-and-how-to-fix-it\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtual-dba.com\/"},{"@type":"ListItem","position":2,"name":"The Billion-Row Scan: Why Your SQL Query for MAX Is Slow and How to Fix It"}]},{"@type":"WebSite","@id":"https:\/\/virtual-dba.com\/#website","url":"https:\/\/virtual-dba.com\/","name":"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts","description":"Remote Database Administration","publisher":{"@id":"https:\/\/virtual-dba.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtual-dba.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/virtual-dba.com\/#organization","name":"Virtual-DBA: Remote DBA | Remote Database Administration","alternateName":"Virtual-DBA powered by XTIVIA","url":"https:\/\/virtual-dba.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/","url":"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg","contentUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg","width":557,"height":291,"caption":"Virtual-DBA: Remote DBA | Remote Database Administration"},"image":{"@id":"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/virtual_dba","https:\/\/www.linkedin.com\/showcase\/36220649\/","https:\/\/www.youtube.com\/channel\/UCx3AIeUQ2ziTLKZSJDZ-SEg"],"description":"Eliminate database downtime and spiraling costs with XTIVIA\u2019s Virtual-DBA. In today\u2019s always-on business world, gaps in 24x7 on-call DBA support, neglected maintenance and security, or a stretched team struggling with overwhelming workloads can lead to costly disruptions and threaten business continuity. XTIVIA\u2019s Virtual-DBA provides the immediate, expert database administration you need, exactly when you need it, ensuring optimal performance, ironclad security, and significant cost savings without the burden of expanding your in-house team. The goal of Virtual-DBA is to provide a cost-effective solution for organizations seeking to optimize the security, management, maintenance, availability, and performance of their critical business systems, whether self-managed or cloud-managed (e.g., AWS RDS, Azure SQL Database). We accomplish this through a comprehensive remote DBA service offering designed specifically to meet the Oracle\u00ae, DB2\u00ae, Informix\u00ae, MySQL\u2122, PostgreSQL\u00ae, MongoDB\u00ae, MariaDB, and Microsoft SQL Server\u00ae, CockroachDB, Databricks, AWS, and Azure needs of our clients.","email":"info@xtivia.com","telephone":"8886853101","legalName":"XTIVIA, Inc","foundingDate":"1992-05-01","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"201","maxValue":"500"}},{"@type":"Person","@id":"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365","name":"Ken Haff","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtual-dba.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g","caption":"Ken Haff"},"url":"https:\/\/virtual-dba.com\/author\/ken-haff\/"}]}},"_links":{"self":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/posts\/243082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/users\/59"}],"replies":[{"embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/comments?post=243082"}],"version-history":[{"count":0,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/posts\/243082\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/media\/243087"}],"wp:attachment":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/media?parent=243082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/categories?post=243082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/tags?post=243082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}