When connecting to your pg database, connect from this project directory.
To run sql statements from an external file, use the \i [filepath] command.
- Create a new postgres user named
indexed_cars_user - Create a new database named
indexed_carsowned byindexed_cars_user - Run the provided
scripts/car_models.sqlscript on theindexed_carsdatabase - Run the provided
scripts/car_model_data.sqlscript on theindexed_carsdatabase 10 times
there should be 223380 rows incar_models
Enable timing queries in Postgres by toggling the \timing command in the psql shell.
- Run a query to get a list of all
make_titlevalues from thecar_modelstable where themake_codeis'LAM', without any duplicate rows, and note the time somewhere. (should have 1 result) - Run a query to list all
model_titlevalues where themake_codeis'NISSAN', and themodel_codeis'GT-R'without any duplicate rows, and note the time somewhere. (should have 1 result) - Run a query to list all
make_code,model_code,model_title, and year fromcar_modelswhere themake_codeis'LAM', and note the time somewhere. (should have 1360 rows) - Run a query to list all fields from all
car_modelsin years between2010and2015, and note the time somewhere (should have 78840 rows) - Run a query to list all fields from all
car_modelsin the year of2010, and note the time somewhere (should have 13140 rows)
Given the current query requirements, "should get all make_titles", "should get a list of all model_titles by the make_code", etc.
Create indexes on the columns that would improve query performance.
To add an index:
CREATE INDEX [index name]
ON [table name] ([column name(s) index]);
Record your index statements in indexing.sql
Write the following statements in indexing.sql
- Create a query to get a list of all
make_titlevalues from thecar_modelstable where themake_codeis'LAM', without any duplicate rows, and note the time somewhere. (should have 1 result) - Create a query to list all
model_titlevalues where themake_codeis'NISSAN', and themodel_codeis'GT-R'without any duplicate rows, and note the time somewhere. (should have 1 result) - Create a query to list all
make_code,model_code,model_title, and year fromcar_modelswhere themake_codeis'LAM', and note the time somewhere. (should have 1360 rows) - Create a query to list all fields from all
car_modelsin years between2010and2015, and note the time somewhere (should have 78840 rows) - Create a query to list all fields from all
car_modelsin the year of2010, and note the time somewhere (should have 13140 rows)
Compare the times of the queries before and after the table has been indexes.
Why are queries #4 and #5 not running faster?
- Add your recorded indexing statements to the
scripts/car_models.sql - Delete the
car_modelstable - Run the provided
scripts/car_models.sqlscript on theindexed_carsdatabase - Run the provided
scripts/car_model_data.sqlscript on theindexed_carsdatabase 10 times
there should be 223380 rows incar_models