DB2 Interview Questions and Answers for 7 years experience
-
What are the different types of joins in DB2? Explain with examples.
- Answer: DB2 supports various JOIN types: INNER JOIN (returns rows only when there's a match in both tables), LEFT (OUTER) JOIN (returns all rows from the left table, and matching rows from the right table; NULLs for non-matches), RIGHT (OUTER) JOIN (returns all rows from the right table, and matching rows from the left table; NULLs for non-matches), and FULL (OUTER) JOIN (returns all rows from both tables; NULLs where there's no match in the other table). For example: An `INNER JOIN` between `EMPLOYEES` and `DEPARTMENTS` tables would only return employees who have a matching department ID. A `LEFT JOIN` would return all employees, even if they don't have a department assigned (department information would be NULL in those cases).
-
Explain the difference between clustered and non-clustered indexes in DB2.
- Answer: A clustered index in DB2 physically orders the data rows in the table according to the index key. There can only be one clustered index per table. A non-clustered index maintains a separate structure containing the index key and a pointer to the data row's location. Non-clustered indexes are faster for searches but don't affect the physical order of the data. Clustered indexes improve performance for range scans and queries based on the index key because the data is already sorted.
-
How do you handle deadlocks in DB2?
- Answer: Deadlocks occur when two or more transactions are blocked indefinitely, waiting for each other to release locks. Strategies to handle deadlocks include: proper lock granularity (using finer-grained locks where possible), minimizing lock holding times, using appropriate isolation levels, and monitoring for deadlocks using DB2's monitoring tools. DB2's deadlock detection mechanism usually automatically detects and resolves deadlocks by rolling back one of the involved transactions.
-
What are the different isolation levels in DB2?
- Answer: DB2 offers several isolation levels to control concurrency: `RR` (Repeatable Read) - prevents dirty reads, non-repeatable reads, and phantom reads; `CS` (Cursor Stability) - prevents dirty reads and non-repeatable reads; `UR` (Uncommitted Read) - allows dirty reads; `RS` (Read Stability) - prevents dirty reads. The choice depends on the application's needs; higher isolation levels provide more data integrity but can reduce concurrency.
-
Explain the concept of stored procedures in DB2. What are their advantages?
- Answer: Stored procedures are pre-compiled SQL code stored in the database. Advantages include: improved performance (due to pre-compilation), enhanced security (restricting direct access to data), code reusability, and easier maintenance. They encapsulate business logic within the database, improving modularity.
-
How do you optimize queries in DB2? Give specific examples.
- Answer: Query optimization involves techniques like using appropriate indexes, avoiding functions in `WHERE` clauses, using `EXISTS` instead of `COUNT(*)` in subqueries, optimizing joins (e.g., using appropriate join types), writing efficient SQL statements (e.g., avoiding `SELECT *`), and using query hints where necessary. Analyzing execution plans using DB2's explain tools is crucial for identifying bottlenecks.
-
What are different types of DB2 indexes?
- Answer: DB2 supports various index types: B-tree indexes (the most common type), bitmap indexes (efficient for columns with low cardinality), unique indexes (ensure unique values for a column), and function-based indexes (indexes on expressions or functions of columns).
-
Describe your experience with DB2 performance tuning.
- Answer: (This requires a personalized answer based on your experience. Include specific examples of performance issues you encountered, the tools you used (e.g., DB2 Performance Center, explain plans), and the techniques you applied to resolve the issues. Mention metrics like query response time, CPU utilization, and I/O.)
-
How do you handle large data volumes in DB2?
- Answer: Techniques for handling large data volumes include partitioning the tables (horizontally or vertically), using materialized query tables (MQTs) for pre-calculated results, optimizing indexing strategies, employing data warehousing techniques, and using DB2's built-in features for data compression and efficient storage management.
Thank you for reading our blog post on 'DB2 Interview Questions and Answers for 7 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!