1Z0-909 OCP MySQL 8.0 Database Developer Practice Questions
Prepare for 1Z0-909 with more than an answer.
- Level
- Professional
- Valid for
- Lifetime (does not expire)
Domains covered on the exam 7
- Connectors and APIs
- Data-driven Applications
- MySQL Schema Objects and Data
- Transactions
- Query Optimization
- MySQL Stored Programs
- JSON and Document Store
- 1
You are analyzing a query's performance using
EXPLAIN. TheExtracolumn for a joined table showsUsing where; Using index. What does this indicate about the query's execution for that table?Show answer details
Correct answer: D
Using indexmeans the query is performing an index scan.Using wheremeans that after fetching the rows via the index, the storage engine still needs to apply aWHEREclause filter to them because the index couldn't resolve all conditions. This is different from a covering index (Using indexalone), where the index contains all necessary data and no table access is needed. It's a reasonably efficient access method, but it's not a covering index. - 2
A developer needs to store international product descriptions. The descriptions can contain a wide variety of characters from different languages, including emojis. Which character set and column type combination is most appropriate for this
descriptioncolumn?Show answer details
Correct answer: C
utf8mb4is the recommended character set for storing Unicode data in MySQL 8.0. It supports the full Unicode standard, including 4-byte characters like emojis. The olderutf8(now an alias forutf8mb3) only supports up to 3-byte characters and cannot store emojis. TheTEXTtype is appropriate for storing long description strings that may exceed the length limits ofVARCHAR. - 3
What is the primary purpose of using a SAVEPOINT within a database transaction?
Show answer details
Correct answer: B
A
SAVEPOINTcreates a named marker within the current transaction. This allows you to useROLLBACK TO SAVEPOINT savepoint_nameto undo all changes made since that savepoint was set, without aborting the entire transaction. This is useful for complex operations where you might want to retry a sub-task without losing all the work done earlier in the transaction. - 4
A stored function is created to calculate a customer's lifetime value based on their orders. This calculation is complex and resource-intensive. To allow the query optimizer to better cache results from this function, how should it be declared?
Show answer details
Correct answer: A
A function should be declared as
DETERMINISTICif it always produces the same result for the same input parameters. This is a hint to the optimizer. If a function is deterministic, a call with a given set of arguments can be replaced with a constant value if it's called multiple times within a single query. A customer lifetime value function that only queries historical data is deterministic. Declaring it as such can lead to performance improvements. - 5
True or False: A composite index on columns
(A, B)can be effectively used by the query optimizer to satisfy aWHEREclause condition ofB = 5.Show answer details
Correct answer: B
This statement is false. For a composite (multi-column) index, the query optimizer can only use the index if the
WHEREclause includes a condition on the left-most prefix of the index columns. For an index on(A, B), the optimizer can use it for conditions onAor onA AND B. It cannot use the index to search for conditions onBalone. - 6
A financial services application is experiencing deadlocks during high-concurrency periods. The problematic transaction involves updating a user's balance and inserting a record into a transaction log table. The database uses the default REPEATABLE READ isolation level. Analysis reveals that two concurrent transactions often attempt to lock the same range of rows in the transaction log table, which is indexed by
transaction_date. Which strategy is most effective at resolving these deadlocks while maintaining data consistency?Show answer details
Correct answer: A
Changing the isolation level to READ COMMITTED for these specific transactions is the best solution. REPEATABLE READ uses gap locks, which can lock the space between index records, leading to a higher chance of deadlocks when inserting into a sequentially indexed column. READ COMMITTED does not use gap locks for ordinary statements, which significantly reduces the likelihood of this type of deadlock. Switching to SERIALIZABLE would worsen the problem by increasing locking. Using LOCK TABLES is too coarse and would serialize access, killing concurrency. Retrying transactions is a valid strategy but doesn't solve the root cause of the frequent deadlocks.
- 7
A developer is building a feature to store user preferences as JSON objects in a
user_profilestable. The application needs to update a specific nested attribute (theme.color) and add a new attribute (notifications.enabled) in a single atomic operation. The existing JSON is:{"theme": {"color": "dark"}, "font_size": 14}. Which MySQL function should be used to achieve this?Show answer details
Correct answer: B
JSON_MERGE_PATCHis the correct function. It follows RFC 7396 semantics, where it recursively merges JSON documents. It updates existing key values and adds new key-value pairs. In this case, it would updatetheme.colorand addnotifications.enabled.JSON_SETcan update existing values and add new ones, but requires specifying each path and value pair individually, making it less concise for merging objects.JSON_REPLACEonly updates existing values and does not add new ones.JSON_MERGE_PRESERVEwould create an array if keys conflict, which is not the desired behavior here. - 8
A data analyst needs to generate a report showing the total sales for each product category, but only for categories with total sales exceeding $10,000. Which of the following query structures is correct?
erDiagram PRODUCTS ||--o{ ORDER_ITEMS : contains CATEGORIES ||--o{ PRODUCTS : belongs to CATEGORIES { int category_id PK string category_name } PRODUCTS { int product_id PK int category_id FK string product_name decimal price } ORDER_ITEMS { int item_id PK int product_id FK int quantity }Show answer details
Correct answer: B
The
HAVINGclause is used to filter groups after aggregation has been performed, whereas theWHEREclause filters rows before aggregation. To filter based on the result of an aggregate function likeSUM(), you must useHAVING. The aliastotal_salescannot be used in theWHEREclause of the same query level where it is defined. - 9
You are tasked with optimizing a slow query that retrieves user information. The
EXPLAINoutput shows that a full table scan is performed on theuserstable despite an index existing on theemailcolumn. The query is:SELECT user_id, name FROM users WHERE YEAR(created_at) = 2023 AND SUBSTRING(email, INSTR(email, '@') + 1) = 'example.com';. What is the primary reason the index onemailis not being used?Show answer details
Correct answer: C
Applying a function like
SUBSTRING()orINSTR()to an indexed column in theWHEREclause prevents the MySQL optimizer from using the index on that column. This makes the predicate non-SARGable (Search-Argument-able). To make the query use the index, theWHEREclause should be rewritten to compare the column directly, for example,WHERE email LIKE '%@example.com'. Note that even withLIKE, a leading wildcard (%) can also prevent index usage, but the function application is the definite cause here. - 10
A developer needs to create a stored procedure that accepts an employee ID and returns their department name and manager's name. Which parameter modes should be used for the department name and manager's name? (Select TWO)
Show answer details
Correct answer: B, E
The
OUTparameter mode is used for parameters that the procedure will set and return to the caller. Since the procedure needs to return the department name and manager's name, these should beOUTparameters.While using
OUTparameters is one way to return values, a common and often preferred method for returning tabular data (even a single row with multiple columns) is to have the stored procedure execute aSELECTstatement. This produces a result set that the calling application can then process.
