Enhance your understanding of SQL database performance with this focused quiz covering indexes, join types, and query plan basics. Learn to identify techniques that optimize SQL queries and boost database efficiency.
Which type of SQL index is typically used to improve the speed of data retrieval on one or more columns in a table?
Explanation: A B-tree index is a common indexing structure supported by most databases to speed up lookups or range queries. 'Bucket list' and 'data mesh' are not types of indexes; 'data mesh' relates to architectural concepts. 'Tree loop' is not a recognized index type and is likely a misinterpretation.
Which scenario best justifies adding an index to the 'email' column of a 'users' table?
Explanation: Indexes are most beneficial when you frequently query or search data based on the indexed column, like searching users by email. Not frequently updating addresses is unrelated to index usefulness. The length of the emails or table size alone does not justify an index unless searches are involved.
What is a unique index designed to enforce in a SQL table?
Explanation: A unique index ensures that no two rows have the same value in the indexed column, enforcing uniqueness. It does not prevent NULLs unless specified. A table can have multiple indexes, and unique indexes are not directly about update speed.
What is a composite index in SQL?
Explanation: A composite index involves multiple columns and is useful for queries filtering on those columns. It is not shared between tables and does not mean a backup. Composite indexes are not restricted to primary keys.
When you want to retrieve matching rows from two tables based on a common column, which SQL JOIN should you use?
Explanation: INNER JOIN returns rows when there is a match in both tables. 'CARTESIAN JOIN' refers to a cross join that combines all rows, not just matches. 'CROSS PATCH' and 'OUTER PRUNE' are not valid join types.
Why would you use an 'EXPLAIN' or 'EXPLAIN PLAN' statement before running a SQL query?
Explanation: The EXPLAIN statement shows you the planned execution steps for your query, helping identify inefficiencies. It does not check spelling, optimize tables, or remove duplicates, which are separate tasks.
Which operation is often slower if a table has many indexes applied to it?
Explanation: Each index must be updated every time a row is inserted, making INSERT slower with many indexes. SELECTs and queries using ORDER BY or WHERE can benefit from indexes, whereas data modifications may slow down.
When does a database likely perform a full table scan instead of using an index?
Explanation: Without a proper index, the database must check every row, causing a full table scan. An empty table returns results quickly regardless of scanning. Sorting by primary key doesn't cause a scan, and using an index in every query would prevent it.
What is the primary benefit of having an appropriate index when running a SELECT query with a WHERE clause?
Explanation: Indexes dramatically speed up searches and filtering in SELECT queries. Indexes do not affect syntax, do not limit column values, and do not always provide automatic sorting unless explicitly designed (like clustered indexes).
How can the order of joining tables in a multi-table SQL query affect performance?
Explanation: The order of joins can impact how efficiently the database filters and combines data. It does not alter column names, datatypes, or restrict the amount of joins, which are defined by the query itself.
If a SQL query often searches for records by a 'last_name' column, what should you do to speed up these searches?
Explanation: Indexing the searched column makes lookups more efficient. Triggers are for automated actions, renaming doesn't help performance, and SELECT * can actually hurt efficiency if unnecessary data is retrieved.
What does a primary key constraint automatically create in most SQL databases?
Explanation: Primary keys enforce uniqueness and generally create a unique index on their columns. It does not force a full table scan, relate to clustered updates, or generate foreign tables.
What is a covering index in SQL query optimization?
Explanation: A covering index contains all the columns needed by the query, eliminating the need to look up the table rows. It does not encrypt or duplicate data and does not relate to indexing multiple schemas.
What does a LEFT OUTER JOIN return when used between two tables A and B?
Explanation: A LEFT OUTER JOIN returns all rows from the left table (A) and the matched rows from the right table (B), showing NULLs when there's no match in B. INNER JOIN would return only matching rows. The reverse is a RIGHT JOIN. Only showing non-matches is also incorrect.
How can an index help speed up a SQL query that uses an ORDER BY clause on the 'created_at' column?
Explanation: If an index already stores 'created_at' in order, the result set doesn't need to be sorted again, saving time. Indexes do not remove duplicates, do not hide NULLs, and do not increase table size as a primary function.
In a SQL query plan, what does the term 'Seq Scan' or 'Table Scan' indicate?
Explanation: A 'Seq Scan' or 'Table Scan' shows that every row in the table is examined in sequence. Random skips do not occur, indexes are not always used during such scans, and indexes themselves are not scanned in this operation.