Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Slow Query with Indexing

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimize Slow Query with Indexing. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Explain how you would optimize a slow-running query in a big data environment using indexing or partitioning.

Use the provided schema to write a PostgreSQL query that reads only the requested event-date range, excludes internal users, aggregates valid Apple device app-open events, and reports the day-over-day change in active users for each product line.

Output

  1. One row per event date and product line
  2. Columns: event_date, product_line, active_users, event_count, avg_latency_ms, previous_active_users, and active_user_change
  3. Include only dates from 2025-01-01 through 2025-01-03, sort by event_date, then product_line

Schema

analytics_events
ColumnTypeDescription
event_idPKBIGINTUnique analytics event identifier
event_dateDATEEvent date and partition key
user_idBIGINTUser associated with the event
device_idBIGINTApple device associated with the event
event_nameVARCHAR(50)Name of the recorded event
latency_msINTEGERObserved event latency in milliseconds
app_users
ColumnTypeDescription
user_idPKBIGINTUnique user identifier
user_nameVARCHAR(100)User display name
is_internalBOOLEANWhether the user is an internal tester
apple_devices
ColumnTypeDescription
device_idPKBIGINTUnique device identifier
product_lineVARCHAR(50)Apple device product line
device_modelVARCHAR(100)Specific Apple device model
Tablesanalytics_eventsapp_usersapple_devices
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results