> ## Documentation Index
> Fetch the complete documentation index at: https://docs.textql.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ontology

> Query data using your semantic layer

When enabled, Ana uses your configured ontology (semantic layer) to understand your data relationships, business definitions, and pre-configured joins. Instead of writing raw SQL, Ana can query using business-friendly concepts like "metrics" and "dimensions" that you've pre-defined.

<Note>
  **Consistency Guaranteed:** With Ontology enabled, the same metric always produces the same calculation—ensuring everyone in your organization gets aligned results.
</Note>

<Frame caption="Ana using ontology to explore database structure and query metrics">
  <img src="https://mintcdn.com/textql/_MjEsXv_WMgj05pB/images/tool/ontology.png?fit=max&auto=format&n=_MjEsXv_WMgj05pB&q=85&s=8b67f6c0a0db395255614e663a36f6d2" alt="Ontology tool showing metrics and dimensions" width="2974" height="1916" data-path="images/tool/ontology.png" />
</Frame>

## <Icon icon="pencil" iconType="solid" /> You Define the Structure

Users create and customize their own ontology by defining these core components:

<CardGroup cols={2}>
  <Card title="Objects" icon="cube">
    Your business entities that map to database tables

    **Example:** Customers, Orders, Products
  </Card>

  <Card title="Metrics" icon="calculator">
    Pre-defined calculations with exact SQL logic

    **Example:** "Total Revenue", "Customer Lifetime Value"
  </Card>

  <Card title="Dimensions" icon="sliders">
    Attributes for grouping and filtering data

    **Example:** "Order Date", "Customer Region"
  </Card>

  <Card title="Links" icon="link">
    Relationships between different objects

    **Example:** "Orders belong to Customers"
  </Card>
</CardGroup>

<Tip>
  **Quality Matters:** The more accurately you define your ontology, the more fine-tuned and consistent your results will be. Each metric includes the exact SQL logic needed to calculate it.
</Tip>

## <Icon icon="scale-balanced" iconType="solid" /> How It Differs from Text to SQL

<Tabs>
  <Tab title="Quick Comparison">
    | Feature              | Text to SQL                          | Ontology                              |
    | -------------------- | ------------------------------------ | ------------------------------------- |
    | **Query Generation** | Generates SQL from scratch each time | Uses pre-defined metric SQL           |
    | **Consistency**      | May vary between queries             | Same metric = same calculation always |
    | **Business Logic**   | Ana figures it out on the fly        | Pre-configured by you                 |
    | **Complexity**       | Can handle simple queries well       | Excels with complex data models       |
    | **Setup Required**   | None                                 | Requires ontology configuration       |
  </Tab>

  <Tab title="Text to SQL">
    ### How It Works

    Generates SQL queries directly from natural language, requiring Ana to figure out table joins, column names, and business logic on the fly. Every query is generated from scratch.

    ### Best For

    * Ad-hoc exploration
    * Simple queries
    * Quick analysis
    * When you don't have an ontology

    ### Limitations

    * May interpret metrics differently each time
    * Requires Ana to infer business logic
    * Can be inconsistent across queries
  </Tab>

  <Tab title="Ontology">
    ### How It Works

    Uses your pre-configured semantic layer where you've already defined business objects, metrics (like "Total Revenue"), dimensions (like "Date"), and relationships. Ana queries using these high-level concepts through "Metrics Queries" that execute only the specific SQL logic you've associated with each metric.

    ### Best For

    * Consistent reporting
    * Complex data models
    * Organization-wide metrics
    * Enforcing business definitions

    ### Advantages

    * Same metric = same calculation always
    * Pre-configured business logic
    * Handles complex joins automatically
    * Scales with data model complexity
  </Tab>
</Tabs>

## <Icon icon="circle-check" iconType="solid" /> When to Use

Enable the Ontology tool when:

<Steps>
  <Step title="You Have an Ontology Configured">
    An ontology must be built and configured for your data warehouse before you can use this tool.
  </Step>

  <Step title="You Need Consistency">
    When metric calculations must be consistent across your organization and over time.
  </Step>

  <Step title="Complex Data Models">
    Your data model has many tables with intricate relationships that are hard to explain each time.
  </Step>

  <Step title="Enforce Business Logic">
    You want to ensure everyone uses the same definitions and calculation methods.
  </Step>

  <Step title="Pre-Defined Metrics">
    You're asking questions about metrics that have already been defined in your ontology.
  </Step>
</Steps>

<Warning>
  **Investment Pays Off:** The more effort you invest in defining your ontology, the more reliable and business-aligned your results will be.
</Warning>

## <Icon icon="rocket" iconType="solid" /> Benefits

<CardGroup cols={3}>
  <Card title="Consistency" icon="check-double">
    Everyone gets the same numbers for the same metrics—no more "version of the truth" debates
  </Card>

  <Card title="Efficiency" icon="gauge-high">
    No need to explain complex joins and calculations repeatedly—they're pre-configured
  </Card>

  <Card title="Governance" icon="shield-halved">
    Control how metrics are calculated and ensure compliance with business rules
  </Card>

  <Card title="Self-Service" icon="user">
    Non-technical users can query using business terms without knowing SQL
  </Card>

  <Card title="Documentation" icon="book">
    Your ontology serves as living documentation of your data model
  </Card>

  <Card title="Scalability" icon="chart-line">
    As your data model grows, the ontology handles increased complexity gracefully
  </Card>
</CardGroup>

## <Icon icon="book-open" iconType="solid" /> Building Your Ontology

Ready to create or enhance your ontology? Check out these resources:

<CardGroup cols={2}>
  <Card title="Ontology Overview" icon="map" href="/core/ontology/overview">
    Learn the fundamentals of ontologies and how they work in TextQL
  </Card>

  <Card title="Creating Objects" icon="cube" href="/core/ontology/build-your-ontology">
    Define your business entities and map them to database tables
  </Card>

  <Card title="Defining Metrics" icon="calculator" href="/core/ontology/build-your-ontology">
    Create calculations with business logic that everyone can use
  </Card>

  <Card title="Setting Up Links" icon="link" href="/core/ontology/build-your-ontology">
    Configure relationships between your objects
  </Card>

  <Card title="Writing Queries" icon="code" href="/core/ontology/build-your-ontology#ontology-query---tql-files">
    Learn how to query your ontology effectively
  </Card>

  <Card title="TQL Reference" icon="code" href="https://haym.me/tql_manual.html">
    Language reference for writing parameterized ontology queries in TQL
  </Card>

  <Card title="Why Use Ontology?" icon="question" href="/core/ontology/overview">
    Understand the value proposition and use cases
  </Card>
</CardGroup>

## <Icon icon="lightbulb" iconType="solid" /> Best Practices

<AccordionGroup>
  <Accordion title="Start with Core Metrics" icon="star">
    Begin by defining your organization's most important and frequently-used metrics. Don't try to model everything at once.

    **Examples:**

    * Revenue metrics (MRR, ARR, Total Revenue)
    * Customer metrics (CAC, LTV, Churn Rate)
    * Operational metrics (Conversion Rate, Average Order Value)
  </Accordion>

  <Accordion title="Use Clear Naming" icon="tag">
    Name your objects, metrics, and dimensions using familiar business terminology that everyone understands.

    **Good:** "Monthly Recurring Revenue", "Customer Lifetime Value"

    **Avoid:** "mrr\_calc\_v2", "ltv\_formula"
  </Accordion>

  <Accordion title="Document Your Logic" icon="file-text">
    Add descriptions to your metrics explaining what they calculate and when to use them. This helps users understand which metric to choose.
  </Accordion>

  <Accordion title="Version Control Changes" icon="code-branch">
    When updating metric definitions, consider the impact on historical reports. Document changes and communicate them to your team.
  </Accordion>

  <Accordion title="Regular Reviews" icon="calendar-check">
    Periodically review your ontology with business stakeholders to ensure it still reflects current business logic and requirements.
  </Accordion>
</AccordionGroup>
