# Sloan / Demos / Mvs Documentation – Python ## Included Files 1. sloan/demos/mvs/mvs.mdx ## Materialized Views Demo Source: sloan/demos/mvs/mvs.mdx Learn how to create materialized views using Sloan with a step-by-step TypeScript example # Materialized Views ## Create materialized views to pre-aggregate your data, based on your egress API This demo will walk you through using Sloan tools to prompt your way to creating materialized views that pre-aggregate the aircraft telemetry data you've ingested. [Skip to prompt](#prompt-the-llm-to-create-a-materialized-view) if you started with the ads-b template. - **Sloan and Moose CLIs**: [Install them here](/sloan/getting-started/cursor) - **OS**: macOS or Linux (WSL supported for Windows) - **Docker Desktop/Engine**: [24.0.0+](https://docs.docker.com/get-started/get-docker/) - **Node**: [version 20+](https://nodejs.org/en/download) (LTS recommended) - **Anthropic API Key**: [Get one here](https://docs.anthropic.com/en/docs/initial-setup) - **Client**: [Cursor](https://www.cursor.com/) - **Completed**: [Ingest Demo](/sloan/demos/ingest) (or have data already in your system) ### Start with the ads-b template ```bash filename="Terminal" copy sloan init mvs-demo ads-b ``` ```bash filename="Terminal" copy cd mvs-demo npm install ``` ### Run the Moose Dev Server ```bash filename="Terminal" copy moose dev ``` ### Open the project in Cursor ### Initialize the Sloan MCP Navigate to `Cursor > Settings > Cursor Settings > Tools and Integrations` then toggle on the `Sloan MCP` tool. ### For best results, set the LLM to `claude-4-sonnet` Gemini 2.5 and o3 are also reasonably good, but claude-4-sonnet has the most consistent results. ### Create an egress API You can skip this step if you've already completed the [Egress Demo](/sloan/demos/egress). > Can you create an API that returns every aircraft within X miles of Y,Z coordinates. If you prefer to implement the egress API manually, you can create the following analytics API: ```typescript filename="app/index.ts" ... export * from './apis/getAircraftWithinRadius'; ``` ```typescript filename="app/apis/getAircraftWithinRadius.ts" /** * Parameters for the getAircraftWithinRadius API */ interface AircraftRadiusParams { /** Latitude of the center point */ center_lat: number; /** Longitude of the center point */ center_lon: number; /** Radius in miles from the center point */ radius_miles: number; /** Maximum number of results to return (optional) */ limit?: number & tags.Type<"int64"> & tags.Minimum<1> & tags.Maximum<1000>; } /** * API to retrieve aircraft within a specified radius from given coordinates * Uses ClickHouse's geoDistance function to calculate great circle distance */ ) => { // Execute the query with proper parameter handling const result = await client.query.execute(sql` SELECT hex, flight, aircraft_type, lat, lon, alt_baro, gs, track, timestamp, round(geoDistance(lon, lat, ${params.center_lon}, ${params.center_lat}) * 0.000621371, 2) as distance_miles FROM AircraftTrackingProcessed WHERE geoDistance(lon, lat, ${params.center_lon}, ${params.center_lat}) * 0.000621371 <= ${params.radius_miles} ORDER BY distance_miles ASC LIMIT ${params.limit || 100} `); return result; }, { metadata: { description: "Returns all aircraft within a specified distance (in miles) from given coordinates" } } ); ``` ### Prompt the LLM to create a materialized view > Given the egress API, can you create a materialized view that improves the performance of the query? ### Action The LLM should now use sloan tools to: 1. analyze the Moose project and the egress API 2. create a Materialized View that pre-aggregates the data You'll know it is succeeding if: 1. the LLM successfully creates the materialized view 2. the LLM sees that the materialized view was created in the ClickHouse database ### Optional further prompts > Can you create a new egress API that leverages the materialized view to improve the performance of the query? > Can you test both APIs to see what the performance difference is? > I want to see the difference in speed, number of rows read, amount of data read, compute, and other vectors you think are pertinent.