{"title":"Optimize table","description":"Optimize Altertable tables for better storage layout, faster queries, and improved lakehouse performance at scale.","order":680,"askSuggestions":["Why do queries slow down after many small appends?","Does optimizing a table apply its sort order?"],"navigation":true,"body":{"code":"var Component=(()=>{var m=Object.create;var a=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var g=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty;var w=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),y=(n,e)=>{for(var r in e)a(n,r,{get:e[r],enumerable:!0})},l=(n,e,r,i)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let o of f(e))!b.call(n,o)&&o!==r&&a(n,o,{get:()=>e[o],enumerable:!(i=u(e,o))||i.enumerable});return n};var x=(n,e,r)=>(r=n!=null?m(g(n)):{},l(e||!n||!n.__esModule?a(r,\"default\",{value:n,enumerable:!0}):r,n)),z=n=>l(a({},\"__esModule\",{value:!0}),n);var c=w((I,s)=>{s.exports=_jsx_runtime});var v={};y(v,{default:()=>p,frontmatter:()=>q});var t=x(c()),q={title:\"Optimize table\",description:\"Optimize Altertable tables for better storage layout, faster queries, and improved lakehouse performance at scale.\",order:680,askSuggestions:[\"Why do queries slow down after many small appends?\",\"Does optimizing a table apply its sort order?\"]};function d(n){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",li:\"li\",p:\"p\",pre:\"pre\",strong:\"strong\",ul:\"ul\",...n.components},{DocsPreviewDocument:r,DocsPreviewSection:i}=e;return r||h(\"DocsPreviewDocument\",!0),i||h(\"DocsPreviewSection\",!0),(0,t.jsxs)(r,{anchors:\"optimize-table sql-example what-optimization-does when-to-run-it learn-more\",children:[(0,t.jsxs)(i,{sectionId:\"optimize-table\",sectionTitle:\"Optimize table\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,t.jsx)(e.h1,{id:\"optimize-table\",children:\"Optimize table\"}),`\n`,(0,t.jsx)(e.p,{children:\"Optimize table maintenance rewrites one table's files so future queries scan fewer, better-organized Parquet files. Use it after large ingests, many small appends, or update-heavy workloads that leave data spread across many files.\"}),`\n`,(0,t.jsxs)(e.p,{children:[\"Optimization is housekeeping for an \",(0,t.jsx)(e.a,{href:\"/docs/lakehouse/catalogs\",children:\"Altertable catalog\"}),\" table. It does not change the logical rows in the table; it changes the physical layout used by the query engine.\"]}),`\n`]}),(0,t.jsxs)(i,{sectionId:\"sql-example\",sectionTitle:\"SQL example\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,t.jsx)(e.h2,{id:\"sql-example\",children:\"SQL example\"}),`\n`,(0,t.jsxs)(e.p,{children:[\"Run \",(0,t.jsx)(e.code,{children:\"OPTIMIZE TABLE\"}),\" from SQL after a large ingest or a period of frequent small writes:\"]}),`\n`,(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:\"language-sql\",children:`OPTIMIZE TABLE my_catalog.main.events;\n`})}),`\n`,(0,t.jsxs)(e.p,{children:[\"The command returns optimization work by tier, such as \",(0,t.jsx)(e.code,{children:\"cold\"}),\", \",(0,t.jsx)(e.code,{children:\"warm\"}),\", and \",(0,t.jsx)(e.code,{children:\"hot\"}),\", including the number of files considered and rewritten.\"]}),`\n`]}),(0,t.jsxs)(i,{sectionId:\"what-optimization-does\",sectionTitle:\"What optimization does\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,t.jsx)(e.h2,{id:\"what-optimization-does\",children:\"What optimization does\"}),`\n`,(0,t.jsx)(e.p,{children:\"For the target table, optimization can:\"}),`\n`,(0,t.jsxs)(e.ul,{children:[`\n`,(0,t.jsx)(e.li,{children:\"Compact many small files into larger files.\"}),`\n`,(0,t.jsxs)(e.li,{children:[\"Apply the table's current \",(0,t.jsx)(e.a,{href:\"/docs/lakehouse/features/sorted-tables\",children:\"sort order\"}),\" when rewriting data.\"]}),`\n`,(0,t.jsx)(e.li,{children:\"Improve file-level statistics used for pruning.\"}),`\n`,(0,t.jsx)(e.li,{children:\"Reduce metadata and planning overhead for frequently queried tables.\"}),`\n`]}),`\n`]}),(0,t.jsxs)(i,{sectionId:\"when-to-run-it\",sectionTitle:\"When to run it\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,t.jsx)(e.h2,{id:\"when-to-run-it\",children:\"When to run it\"}),`\n`,(0,t.jsx)(e.p,{children:\"Optimize tables that receive frequent small writes or show slower query planning over time. It is most useful for large tables that are queried repeatedly by dashboards, AI agents, scheduled tasks, or external BI tools.\"}),`\n`,(0,t.jsx)(e.p,{children:\"Skip optimization for tiny tables or tables that are about to be fully replaced. The rewrite costs compute now to improve repeated reads later.\"}),`\n`]}),(0,t.jsxs)(i,{sectionId:\"learn-more\",sectionTitle:\"Learn more\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,t.jsx)(e.h2,{id:\"learn-more\",children:\"Learn more\"}),`\n`,(0,t.jsxs)(e.ul,{children:[`\n`,(0,t.jsxs)(e.li,{children:[(0,t.jsx)(e.strong,{children:(0,t.jsx)(e.a,{href:\"/docs/lakehouse/query/sql-explorer\",children:\"SQL Explorer\"})}),\": run maintenance SQL interactively.\"]}),`\n`,(0,t.jsxs)(e.li,{children:[(0,t.jsx)(e.strong,{children:(0,t.jsx)(e.a,{href:\"/docs/lakehouse/features/partitioning\",children:\"Partitioning\"})}),\": split data by partition keys.\"]}),`\n`,(0,t.jsxs)(e.li,{children:[(0,t.jsx)(e.strong,{children:(0,t.jsx)(e.a,{href:\"/docs/lakehouse/features/sorted-tables\",children:\"Sorted tables\"})}),\": configure physical sort order.\"]}),`\n`,(0,t.jsxs)(e.li,{children:[(0,t.jsx)(e.strong,{children:(0,t.jsx)(e.a,{href:\"/docs/lakehouse/compute/performance\",children:\"Performance\"})}),\": understand cache behavior and benchmarking.\"]}),`\n`]})]})]})}function p(n={}){let{wrapper:e}=n.components||{};return e?(0,t.jsx)(e,{...n,children:(0,t.jsx)(d,{...n})}):d(n)}function h(n,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+n+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return z(v);})();\n;return Component;"},"_id":"docs/lakehouse/features/optimize-table.mdx","_raw":{"sourceFilePath":"docs/lakehouse/features/optimize-table.mdx","sourceFileName":"optimize-table.mdx","sourceFileDir":"docs/lakehouse/features","contentType":"mdx","flattenedPath":"docs/lakehouse/features/optimize-table"},"type":"Docs","path":"/lakehouse/features/optimize-table","level":3,"parent":"lakehouse/features"}