{"title":"Sorted tables","description":"Configure a physical sort order on Altertable catalog tables to improve min/max statistics and query pruning.","order":610,"askSuggestions":["Can I sort a table by an expression or macro?","When do existing files get the new sort order?"],"navigation":true,"body":{"code":"var Component=(()=>{var g=Object.create;var a=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var f=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var E=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),y=(n,e)=>{for(var t in e)a(n,t,{get:e[t],enumerable:!0})},i=(n,e,t,o)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let s of m(e))!S.call(n,s)&&s!==t&&a(n,s,{get:()=>e[s],enumerable:!(o=p(e,s))||o.enumerable});return n};var b=(n,e,t)=>(t=n!=null?g(f(n)):{},i(e||!n||!n.__esModule?a(t,\"default\",{value:n,enumerable:!0}):t,n)),T=n=>i(a({},\"__esModule\",{value:!0}),n);var c=E((x,d)=>{d.exports=_jsx_runtime});var A={};y(A,{default:()=>u,frontmatter:()=>w});var r=b(c()),w={title:\"Sorted tables\",description:\"Configure a physical sort order on Altertable catalog tables to improve min/max statistics and query pruning.\",order:610,askSuggestions:[\"Can I sort a table by an expression or macro?\",\"When do existing files get the new sort order?\"]};function l(n){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",h3:\"h3\",li:\"li\",p:\"p\",pre:\"pre\",strong:\"strong\",ul:\"ul\",...n.components},{DocsPreviewDocument:t,DocsPreviewSection:o}=e;return t||h(\"DocsPreviewDocument\",!0),o||h(\"DocsPreviewSection\",!0),(0,r.jsxs)(t,{anchors:\"sorted-tables set-a-sort-order expression-based-sort-keys remove-a-sort-order when-to-sort learn-more\",children:[(0,r.jsxs)(o,{sectionId:\"sorted-tables\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,r.jsx)(e.h1,{id:\"sorted-tables\",children:\"Sorted Tables\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"Altertable catalog tables can be configured with a sort order. When a sort order is defined, data is physically sorted by the specified columns whenever it is written out as data files \\u2014 during \",(0,r.jsx)(e.code,{children:\"INSERT\"}),\", file compaction, and inlined data flushing.\"]}),`\n`,(0,r.jsx)(e.p,{children:\"Sorting data before writing improves the effectiveness of min/max statistics at query time, which lets the query engine skip data files whose value ranges do not overlap with a query's filter predicates.\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"Run the statements below through Altertable's \",(0,r.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"}),\" against an \",(0,r.jsx)(e.a,{href:\"/docs/lakehouse/catalogs\",children:\"Altertable catalog\"}),\" you own.\"]}),`\n`]}),(0,r.jsxs)(o,{sectionId:\"set-a-sort-order\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,r.jsx)(e.h2,{id:\"set-a-sort-order\",children:\"Set a sort order\"}),`\n`,(0,r.jsx)(e.p,{children:\"Set the sort order for a table:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`ALTER TABLE my_catalog.main.sensor_readings SET SORTED BY (recorded_at ASC);\n`})}),`\n`,(0,r.jsx)(e.p,{children:\"Multiple sort keys are supported:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`ALTER TABLE my_catalog.main.sensor_readings SET SORTED BY (recorded_at ASC, sensor_id DESC);\n`})}),`\n`,(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:\"ASC\"}),\" and \",(0,r.jsx)(e.code,{children:\"DESC\"}),\" control the sort direction. \",(0,r.jsx)(e.code,{children:\"NULLS FIRST\"}),\" and \",(0,r.jsx)(e.code,{children:\"NULLS LAST\"}),\" are also supported to control null ordering:\"]}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`ALTER TABLE my_catalog.main.sensor_readings SET SORTED BY (recorded_at ASC NULLS LAST);\n`})}),`\n`]}),(0,r.jsxs)(o,{sectionId:\"expression-based-sort-keys\",anchors:\"\",ancestors:\"set-a-sort-order\",index:\"2\",children:[(0,r.jsx)(e.h3,{id:\"expression-based-sort-keys\",children:\"Expression-based sort keys\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"Arbitrary expressions are supported in \",(0,r.jsx)(e.code,{children:\"SET SORTED BY\"}),\", not just column references. This includes function calls, casts, and catalog macros.\"]}),`\n`,(0,r.jsx)(e.p,{children:\"Sort by the hour extracted from a timestamp:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`ALTER TABLE my_catalog.main.sensor_readings SET SORTED BY (date_trunc('hour', recorded_at) ASC);\n`})}),`\n`,(0,r.jsx)(e.p,{children:\"Sort by a macro:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`CREATE MACRO reading_day(t) AS date_trunc('day', t);\nALTER TABLE my_catalog.main.sensor_readings SET SORTED BY (reading_day(recorded_at) ASC);\n`})}),`\n`,(0,r.jsxs)(e.p,{children:[\"Expressions are validated when \",(0,r.jsx)(e.code,{children:\"SET SORTED BY\"}),\" runs \\u2014 an error is returned if any referenced columns or functions cannot be resolved.\"]}),`\n`]}),(0,r.jsxs)(o,{sectionId:\"remove-a-sort-order\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,r.jsx)(e.h2,{id:\"remove-a-sort-order\",children:\"Remove a sort order\"}),`\n`,(0,r.jsx)(e.p,{children:\"Remove the sort order from a table:\"}),`\n`,(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:\"language-sql\",children:`ALTER TABLE my_catalog.main.sensor_readings RESET SORTED BY;\n`})}),`\n`,(0,r.jsx)(e.p,{children:\"After resetting, subsequent compactions and flushes write data without sorting.\"}),`\n`]}),(0,r.jsxs)(o,{sectionId:\"when-to-sort\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,r.jsx)(e.h2,{id:\"when-to-sort\",children:\"When to sort\"}),`\n`,(0,r.jsxs)(e.p,{children:[\"Sort when queries repeatedly filter or range-scan on the same columns \\u2014 especially timestamps, status codes, or IDs used in \",(0,r.jsx)(e.code,{children:\"WHERE\"}),\" and \",(0,r.jsx)(e.code,{children:\"ORDER BY\"}),\" clauses. Sorting trades write-time cost for tighter file statistics and fewer files scanned at read time.\"]}),`\n`,(0,r.jsx)(e.p,{children:\"Skip sorting when write throughput is the bottleneck, filters are unpredictable, or tables stay small enough that full scans are cheap. You can add, change, or remove a sort order later; compaction and flush apply the current order going forward.\"}),`\n`]}),(0,r.jsxs)(o,{sectionId:\"learn-more\",anchors:\"\",ancestors:\"\",index:\"5\",children:[(0,r.jsx)(e.h2,{id:\"learn-more\",children:\"Learn more\"}),`\n`,(0,r.jsxs)(e.ul,{children:[`\n`,(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:(0,r.jsx)(e.a,{href:\"/docs/lakehouse\",children:\"Altertable Catalogs\"})}),\": Catalogs for storing and querying your data\"]}),`\n`,(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:(0,r.jsx)(e.a,{href:\"/docs/lakehouse/features/partitioning\",children:\"Partitioning\"})}),\": Split writes by partition key for file-level pruning\"]}),`\n`,(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:(0,r.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL Engine\"})}),\": DuckDB dialect and federated queries\"]}),`\n`]})]})]})}function u(n={}){let{wrapper:e}=n.components||{};return e?(0,r.jsx)(e,{...n,children:(0,r.jsx)(l,{...n})}):l(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 T(A);})();\n;return Component;"},"_id":"docs/lakehouse/features/sorted-tables.mdx","_raw":{"sourceFilePath":"docs/lakehouse/features/sorted-tables.mdx","sourceFileName":"sorted-tables.mdx","sourceFileDir":"docs/lakehouse/features","contentType":"mdx","flattenedPath":"docs/lakehouse/features/sorted-tables"},"type":"Docs","path":"/lakehouse/features/sorted-tables","level":3,"parent":"lakehouse/features"}