GuidesOperate

Change a sorting key

Set a table sorting key with ALTER TABLE MODIFY ORDER BY.

Change a sorting key

Use a table sorting key when you know which paths your queries filter or sort.

Before you begin

  • Use the analytics database. Create it with the database API if it does not exist.
  • Use an events table with tenant and event_time paths.

1. Set the sorting key

Choose paths that match the table's query patterns.

ALTER TABLE events MODIFY ORDER BY (tenant, event_time)

2. Inspect the table

Read the sorting-key expression from system.tables.

SELECT sorting_key FROM system.tables WHERE database = currentDatabase() AND name = 'events'

Check the result

The sorting_key value contains tenant and event_time.

Next steps