{"id":8879,"date":"2026-10-20T13:00:40","date_gmt":"2026-10-20T16:00:40","guid":{"rendered":"https:\/\/powertuning.com.br\/blog\/?p=8879"},"modified":"2026-10-20T12:04:03","modified_gmt":"2026-10-20T15:04:03","slug":"de-20-horas-para-20-segundos","status":"publish","type":"post","link":"https:\/\/powertuning.com.br\/blog\/de-20-horas-para-20-segundos\/","title":{"rendered":"\u201cDe 20 horas para 20 segundos.\u201d"},"content":{"rendered":"<div style=\"max-width: 860px; margin: 0 auto;\">\n<p><!-- DESTAQUE INICIAL --><\/p>\n<div style=\"background: linear-gradient(135deg, #081F42 0%, #0B2D5E 60%, #16437F 100%); border-radius: 14px; padding: 36px 40px; margin-bottom: 32px; color: #ffffff;\">\n<p style=\"font-size: 17px; line-height: 1.8; margin: 0 0 16px 0; color: #e4edf9;\">Nem todo problema grande precisa de uma solu\u00e7\u00e3o complexa. \u00c0s vezes, o tamanho do problema engana, e a gente sai procurando algo sofisticado quando a resposta cabe em poucas linhas.<\/p>\n<p style=\"font-size: 15px; line-height: 1.6; margin: 0; color: #c4d7f0;\">Um ajuste na estrat\u00e9gia de atualiza\u00e7\u00e3o de colunas no SQL Server transformou um processo de horas em segundos.<\/p>\n<\/div>\n<p><!-- INTRODU\u00c7\u00c3O --><\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Um cliente nos acionou porque estava travado nos testes de atualiza\u00e7\u00e3o do ERP: o processo inteiro estava levando cerca de <strong>20 horas<\/strong>.<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Esse cen\u00e1rio obrigaria manter a produ\u00e7\u00e3o parada por todo esse tempo, o que estava tornando o procedimento extremamente custoso para uma empresa que opera <strong>24&#215;7<\/strong>.<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Entramos em uma call, onde o cliente nos mostrou os logs da aplica\u00e7\u00e3o e o que estava sendo executado no banco.<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">O padr\u00e3o logo ficou claro:<\/p>\n<p><!-- ETAPAS DO PROBLEMA --><\/p>\n<div style=\"margin: 24px 0 32px 0;\">\n<div style=\"display: flex; align-items: flex-start; gap: 16px; margin-bottom: 16px; padding: 20px; background: #F4F7FB; border-radius: 10px; border: 1px solid #D9E3F0;\">\n<div style=\"background: #0B2D5E; color: #ffffff; min-width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;\">1<\/div>\n<div>\n<p style=\"margin: 0 0 6px 0; color: #0b2d5e; font-size: 16px; font-weight: bold;\">ALTER TABLE<\/p>\n<p style=\"margin: 0; color: #33475b; font-size: 15px; line-height: 1.7;\">Adicionar uma nova coluna \u00e0 tabela.<\/p>\n<\/div>\n<\/div>\n<div style=\"display: flex; align-items: flex-start; gap: 16px; padding: 20px; background: #F4F7FB; border-radius: 10px; border: 1px solid #D9E3F0;\">\n<div style=\"background: #0B2D5E; color: #ffffff; min-width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;\">2<\/div>\n<div>\n<p style=\"margin: 0 0 6px 0; color: #0b2d5e; font-size: 16px; font-weight: bold;\">UPDATE na tabela inteira<\/p>\n<p style=\"margin: 0; color: #33475b; font-size: 15px; line-height: 1.7;\">Preencher o valor padr\u00e3o em cerca de 20 milh\u00f5es de registros, levando aproximadamente uma hora por UPDATE.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!-- SOLU\u00c7\u00c3O --><\/p>\n<h2 style=\"color: #0b2d5e; font-size: 24px; line-height: 1.3; margin: 42px 0 20px 0; padding-left: 14px; border-left: 5px solid #F5C800;\">A solu\u00e7\u00e3o: deixar o SQL Server fazer o trabalho<\/h2>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Em vez de adicionar a coluna e executar um UPDATE para preencher todas as linhas, a solu\u00e7\u00e3o foi usar o pr\u00f3prio <strong>ALTER TABLE<\/strong> com uma constraint DEFAULT:<\/p>\n<p><!-- C\u00d3DIGO 1 --><\/p>\n<div style=\"background: #0D1B40; border-radius: 10px; padding: 24px; margin: 24px 0; overflow-x: auto;\">\n<p style=\"color: #f5c800; font-size: 13px; font-weight: bold; margin: 0 0 12px 0; text-transform: uppercase; letter-spacing: 1px;\">Coluna NOT NULL<\/p>\n<pre style=\"margin: 0; white-space: pre-wrap; overflow-wrap: anywhere;\"><span style=\"color: #000000;\"><code style=\"font-family: Consolas, Monaco, monospace; font-size: 14px; line-height: 1.8;\">ALTER TABLE dbo.Tabela\r\nADD NovaColuna INT NOT NULL\r\nCONSTRAINT DF_Tabela_NovaColuna DEFAULT (0);<\/code><\/span><\/pre>\n<\/div>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Se a coluna precisar aceitar valores NULL, existe uma alternativa:<\/p>\n<p><!-- C\u00d3DIGO 2 --><\/p>\n<div style=\"background: #0D1B40; border-radius: 10px; padding: 24px; margin: 24px 0; overflow-x: auto;\">\n<p style=\"color: #f5c800; font-size: 13px; font-weight: bold; margin: 0 0 12px 0; text-transform: uppercase; letter-spacing: 1px;\">Coluna NULL<\/p>\n<pre style=\"margin: 0; white-space: pre-wrap; overflow-wrap: anywhere;\"><span style=\"color: #000000;\"><code style=\"font-family: Consolas, Monaco, monospace; font-size: 14px; line-height: 1.8;\">ALTER TABLE dbo.Tabela\r\nADD NovaColuna INT NULL\r\nCONSTRAINT DF_Tabela_NovaColuna DEFAULT (0) WITH VALUES;<\/code><\/span><\/pre>\n<\/div>\n<p><!-- EXPLICA\u00c7\u00c3O T\u00c9CNICA --><\/p>\n<h2 style=\"color: #0b2d5e; font-size: 24px; line-height: 1.3; margin: 42px 0 20px 0; padding-left: 14px; border-left: 5px solid #F5C800;\">Por que fica instant\u00e2neo?<\/h2>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">No SQL Server Enterprise, a partir da vers\u00e3o 2012, quando o valor padr\u00e3o \u00e9 uma constante e as condi\u00e7\u00f5es para a opera\u00e7\u00e3o de metadados s\u00e3o atendidas, o valor pode ser armazenado apenas nos metadados da tabela.<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Nesse cen\u00e1rio, as linhas existentes n\u00e3o precisam ser reescritas fisicamente. O SQL Server retorna o valor padr\u00e3o quando a linha \u00e9 lida, sem precisar percorrer e atualizar os 20 milh\u00f5es de registros.<\/p>\n<div style=\"background: #FFF8DE; border-left: 5px solid #F5C800; border-radius: 0 10px 10px 0; padding: 24px; margin: 28px 0;\">\n<p style=\"color: #0b2d5e; font-size: 18px; font-weight: bold; margin: 0 0 10px 0;\">O impacto na pr\u00e1tica<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8; margin: 0;\">Sem a atualiza\u00e7\u00e3o f\u00edsica de todas as linhas, \u00e9 poss\u00edvel evitar uma longa opera\u00e7\u00e3o de escrita, reduzir a gera\u00e7\u00e3o de log e limitar o tempo de bloqueio associado \u00e0 altera\u00e7\u00e3o.<\/p>\n<\/div>\n<p><!-- PONTOS DE ATEN\u00c7\u00c3O --><\/p>\n<h2 style=\"color: #0b2d5e; font-size: 24px; line-height: 1.3; margin: 42px 0 20px 0; padding-left: 14px; border-left: 5px solid #F5C800;\">Pontos de aten\u00e7\u00e3o<\/h2>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Essa t\u00e9cnica tem restri\u00e7\u00f5es importantes. Antes de aplic\u00e1-la, \u00e9 necess\u00e1rio validar as caracter\u00edsticas da coluna e do ambiente:<\/p>\n<div style=\"margin: 24px 0;\">\n<div style=\"padding: 18px 20px; margin-bottom: 12px; border: 1px solid #D9E3F0; border-radius: 10px;\">\n<p style=\"margin: 0 0 6px 0; color: #0b2d5e; font-size: 16px; font-weight: bold;\">1. Edi\u00e7\u00e3o do SQL Server<\/p>\n<p style=\"margin: 0; color: #33475b; font-size: 15px; line-height: 1.7;\">O comportamento descrito depende da edi\u00e7\u00e3o e das condi\u00e7\u00f5es da opera\u00e7\u00e3o. Na Standard, a escrita f\u00edsica pode ser necess\u00e1ria, e o ganho tende a ser diferente.<\/p>\n<\/div>\n<div style=\"padding: 18px 20px; margin-bottom: 12px; border: 1px solid #D9E3F0; border-radius: 10px;\">\n<p style=\"margin: 0 0 6px 0; color: #0b2d5e; font-size: 16px; font-weight: bold;\">2. Valor padr\u00e3o constante<\/p>\n<p style=\"margin: 0; color: #33475b; font-size: 15px; line-height: 1.7;\">O DEFAULT precisa atender aos requisitos para a opera\u00e7\u00e3o otimizada. Fun\u00e7\u00f5es como NEWID() exigem valores distintos por linha e podem demandar a grava\u00e7\u00e3o f\u00edsica dos dados.<\/p>\n<\/div>\n<div style=\"padding: 18px 20px; margin-bottom: 12px; border: 1px solid #D9E3F0; border-radius: 10px;\">\n<p style=\"margin: 0 0 6px 0; color: #0b2d5e; font-size: 16px; font-weight: bold;\">3. Tipos de dados<\/p>\n<p style=\"margin: 0; color: #33475b; font-size: 15px; line-height: 1.7;\">Tipos como varchar(max), nvarchar(max), varbinary(max), xml, text, image e geography est\u00e3o entre as restri\u00e7\u00f5es a verificar na documenta\u00e7\u00e3o.<\/p>\n<\/div>\n<div style=\"padding: 18px 20px; border: 1px solid #D9E3F0; border-radius: 10px;\">\n<p style=\"margin: 0 0 6px 0; color: #0b2d5e; font-size: 16px; font-weight: bold;\">4. Tamanho m\u00e1ximo da linha<\/p>\n<p style=\"margin: 0; color: #33475b; font-size: 15px; line-height: 1.7;\">A opera\u00e7\u00e3o otimizada tamb\u00e9m tem restri\u00e7\u00f5es relacionadas ao tamanho m\u00e1ximo da linha, incluindo situa\u00e7\u00f5es em que ele pode ultrapassar 8.060 bytes.<\/p>\n<\/div>\n<\/div>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Consulte a<a style=\"color: #0b5cad; font-weight: bold; text-decoration: underline;\" href=\"https:\/\/lnkd.in\/dmH9uDuf\" target=\"_blank\" rel=\"noopener noreferrer\"> documenta\u00e7\u00e3o oficial da Microsoft<\/a> para validar os requisitos aplic\u00e1veis \u00e0 sua vers\u00e3o e edi\u00e7\u00e3o do SQL Server.<\/p>\n<p><!-- RESULTADO --><\/p>\n<div style=\"background: linear-gradient(135deg, #081F42 0%, #0B2D5E 60%, #16437F 100%); border-radius: 14px; padding: 32px; margin: 38px 0; text-align: center; color: #ffffff;\">\n<p style=\"font-size: 14px; font-weight: bold; letter-spacing: 1.5px; text-transform: uppercase; color: #f5c800; margin: 0 0 12px 0;\">Resultado alcan\u00e7ado<\/p>\n<p style=\"font-size: 27px; line-height: 1.4; font-weight: bold; margin: 0 0 14px 0;\">&#8220;De 20 horas para 20 segundos.&#8221;<\/p>\n<p style=\"font-size: 16px; line-height: 1.7; margin: 0; color: #dce8f8;\">Nas palavras do cliente, o processo que inviabilizava os testes de atualiza\u00e7\u00e3o passou a ser conclu\u00eddo em poucos segundos.<\/p>\n<\/div>\n<p><!-- DICA EXTRA --><\/p>\n<h2 style=\"color: #0b2d5e; font-size: 24px; line-height: 1.3; margin: 42px 0 20px 0; padding-left: 14px; border-left: 5px solid #F5C800;\">Dica extra: teste antes de ir para produ\u00e7\u00e3o<\/h2>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">O teste \u00e9 o rei. Sempre execute a opera\u00e7\u00e3o primeiro em um ambiente de homologa\u00e7\u00e3o com configura\u00e7\u00f5es t\u00e3o pr\u00f3ximas da produ\u00e7\u00e3o quanto poss\u00edvel.<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8;\">Essa valida\u00e7\u00e3o ajuda a identificar particularidades do ambiente, confirmar o comportamento esperado e garantir uma atualiza\u00e7\u00e3o mais segura e controlada.<\/p>\n<p><!-- FECHAMENTO --><\/p>\n<div style=\"background: #F4F7FB; border-radius: 12px; padding: 28px 30px; margin: 36px 0;\">\n<p style=\"color: #0b2d5e; font-size: 20px; line-height: 1.5; font-weight: bold; margin: 0 0 12px 0;\">Problema grande, solu\u00e7\u00e3o simples, cliente satisfeito.<\/p>\n<p style=\"color: #33475b; font-size: 16px; line-height: 1.8; margin: 0;\">O desafio n\u00e3o foi escrever o c\u00f3digo, foi enxergar onde estava o gargalo. Mais uma solu\u00e7\u00e3o padr\u00e3o <strong>#PowerTuning<\/strong>.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Nem todo problema grande precisa de uma solu\u00e7\u00e3o complexa. \u00c0s vezes, o tamanho do problema engana, e a gente sai procurando algo sofisticado quando a resposta cabe em poucas linhas. Um ajuste na estrat\u00e9gia de atualiza\u00e7\u00e3o de colunas no SQL Server transformou um processo de horas em segundos. Um cliente nos acionou porque estava travado&hellip; <br \/> <a class=\"read-more\" href=\"https:\/\/powertuning.com.br\/blog\/de-20-horas-para-20-segundos\/\">Leia mais<\/a><\/p>\n","protected":false},"author":37,"featured_media":8881,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","_exactmetrics_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false,"_members_access_role":[],"_members_access_error":""},"categories":[176,71,59,63],"tags":[1638,312,812,1634,1651,1644,1643,13,284,1655,1639,53,33,1640,381,1656,1648,1373,1645,1649,1641,1466,1647,810,1022,43,1642,54,22,1650,1646,17,35,1653,1636,1637,14,1652,1654,874,1635],"class_list":["post-8879","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-banco-de-dados","category-consultoria","category-tuning","category-sql-server","tag-adicao-de-colunas-sql-server","tag-administracao-de-banco-de-dados","tag-alta-disponibilidade","tag-alter-table","tag-ambiente-24x7","tag-atualizacao-de-banco-de-dados","tag-atualizacao-de-erp","tag-banco-de-dados","tag-bancodedados","tag-boas-praticas-sql-server","tag-coluna-com-valor-padrao","tag-consultoria-sql-server","tag-dba","tag-default-constraint","tag-erp","tag-especialista-em-sql-server","tag-ganho-de-performance","tag-infraestrutura-de-dados","tag-manutencao-de-banco-de-dados","tag-melhoria-de-performance","tag-metadados-sql-server","tag-otimizacao-de-banco-de-dados","tag-otimizacao-de-processos","tag-otimizacao-de-queries","tag-otimizacao-sql-server","tag-performance","tag-performance-de-queries","tag-performance-sql-server","tag-power-tuning","tag-reducao-de-downtime","tag-reducao-de-tempo-de-processamento","tag-soupowertuning","tag-sql-server","tag-sql-server-2012","tag-sql-server-enterprise","tag-sql-server-standard","tag-sqlserver","tag-tabelas-com-milhoes-de-registros","tag-tuning-de-banco-de-dados","tag-tuning-sql-server","tag-update-sql"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2026\/10\/Case-20h-\u00b7-Capa-1920x1080-1-scaled.png","_links":{"self":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts\/8879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/comments?post=8879"}],"version-history":[{"count":2,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts\/8879\/revisions"}],"predecessor-version":[{"id":8883,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts\/8879\/revisions\/8883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/media\/8881"}],"wp:attachment":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/media?parent=8879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/categories?post=8879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/tags?post=8879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}