Best Practices
Design Recommendations
Define semantic views based on business scenarios
A semantic view should correspond to a clear analysis domain, such as "employee salary analysis" or "order revenue analysis," rather than cramming all tables into a single view. Focused views are easier to maintain and offer better query performance. Start with 3–5 core tables, validate dimension and metric accuracy, then expand gradually.
Use business terminology for naming
Choose names that business users are familiar with for logical tables, dimensions, and metrics, rather than direct mappings of physical column names:
Leverage WITH SYNONYMS and COMMENT to enhance discoverability, especially in AI Agent scenarios where synonyms aid natural language understanding.
Set dimension metadata appropriately
is_unique = true: For unique-identifier dimensions (e.g., customer ID, employee name), helps the query engine optimizeis_time = true: For date/time dimensions, enables time-series analysis tools to correctly identify themenum_values: For dimensions with a limited set of values (e.g., status, category), constrains the value range and improves AI understanding accuracy
Ensure foreign key type matching
The data types of the foreign key column and the referenced column must match, otherwise creation will fail with an error. If two tables are joined via a string column, the primary key of the referenced table should also be declared as that string column, not an integer ID:
Mind the order of logical table definitions
In the TABLES clause, tables referenced by foreign keys must be defined first:
Maintenance Recommendations
Use DROP IF EXISTS to ensure idempotent scripts
Structural changes require rebuild
Currently, ALTER SEMANTIC VIEW only supports RENAME TO and does not support adding/removing dimensions or modifying metrics. The standard flow for modifying view structure:
Monitor view status via information_schema
FAQ
Q: FOREIGN KEY creation fails with type mismatch error
Check whether the data types of the foreign key column and the referenced column match. When the referenced column name differs from the primary key column name, you must specify it explicitly:
Q: DESC view returns empty
Semantic views have no regular column definitions; DESC without EXTENDED returns empty. Use:
Q: semantic_view() reports function not found
You must specify at least one DIMENSIONS or METRICS parameter; passing only the view name is not allowed:
Q: Filters defined in the FILTERS clause cannot be used in queries
Named filters in FILTERS are semantic annotations for the AI/metadata layer and cannot be passed as parameters to semantic_view(). Filtering must be done via the outer WHERE clause:
Q: ALTER SEMANTIC VIEW RENAME TO reports a syntax error
The new name must not include a schema prefix:
Q: Metrics defined with arithmetic expressions (e.g., MAX - MIN) produce incorrect query results
Arithmetic expression metrics (MAX(col) - MIN(col), SUM(col) / COUNT(col), etc.) do not error at creation time, but queries only return the value of the first operand -- the operation is not executed. This is a current version limitation; do not use such expressions.
Composite calculations should be performed in the outer SQL of the semantic_view() query:
Q: Metrics that reference other metrics (derived metrics) report "cannot resolve column"
Referencing other metric names within a metric definition is not supported. Patterns like emps.avg AS emps.total / emps.count will error at creation time. These must also be calculated in outer SQL.
Q: Window function metrics report "Compiler internal error" at query time
Window functions such as RANK() OVER (...), ROW_NUMBER() OVER (...) cannot be used in metric definitions. They do not error at creation but behave abnormally at query time. Do not use them.
