SQL Server & Middleware DBA Flashcards

1
Q

How do you ensure data integrity in SQL Server?

A

Ensuring data integrity in SQL Server involves implementing constraints such as primary keys, foreign keys, unique constraints, and check constraints. These constraints enforce rules at the database level to maintain consistent and accurate data.

Candidates should also discuss using triggers and stored procedures to enforce business rules and data validation. Look for responses that show a comprehensive understanding of data integrity mechanisms and their practical application in SQL Server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How would you identify and troubleshoot a SQL Server performance issue caused by high CPU usage?

A

Check the Server Tools: Perf Mon
DMVs: sys.dm_exec_query_stats, sys.dm_exec_requests

Once identified, you can analyze and optimize these queries by adding appropriate indexes, adjusting query logic, or updating statistics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you approach optimizing SQL Server memory usage?

A
  • Set appropriate Min and Max Server memory to ensure SQL Server does not consume all the available sys memory.
  • Monitor Memory Usage through DMVs such as sys.dm_os_memory_clerks and sys.dm_exec_query_memory_grants
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What methods do you use to monitor SQL Server performance on an ongoing basis?

A
  • Use Built-in tools such as Profiler, Extended Events, and Perf Mon
  • DMVs such as sys.dm_os_wait_stats and sys.dm_exec_query_stats
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you handle and optimize SQL Server tempdb usage?

A
  • Ensure it’s configured correctly which means one tempdb data file per CPU and all files the same size and distributed across separate storage devices to balance I/O
  • Monitor through DMVs such as sys.dm_db_file_space_usage
  • Ensure Best Practices such as avoiding large temp tables and minimizing tempdb contention
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some common causes of SQL Server slow performance, and how would you resolve them?

A

Common Causes: Poorly Optimized Queries, lack of proper indexing, high CPU or memory usage, and I/O bottlenecks

How to Resolve: Analyze and optimize slow-running queries, indexes are appropriately created and maintained, properly monitor server resource usage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you approach the process of indexing a large SQL Server table to improve query performance?

A

To index a large SQL Server table, you would start by analyzing the queries that frequently access this table to determine which columns are most often used in WHERE clauses, JOIN conditions, and ORDER BY clauses. These columns are good candidates for indexing.

Next, you would create appropriate indexes, such as clustered or non-clustered indexes, based on the query patterns. It’s also essential to regularly update statistics to ensure the SQL Server query optimizer has the most accurate information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you handle database corruption?

A
  • Identify the corrupted data by running DBCC CHECKDB.
  • Attempt to repair the corruption using DBCC REPAIR_ALLOW_DATA_LOSS, REPAIR_FAST, or REPAIR_REBUILD.
  • If repairs fail, restore the database from the latest clean backup.
  • Investigate and resolve the root cause to prevent future occurrences.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a composite index? What is a good use case?

A
  • Indexes multiple columns in combination.
  • Queries filtering on multiple columns, like first_name and last_name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you optimize a slow-running query?

A

To optimize a slow-running query, I would first analyze the query execution plan to identify any bottlenecks or areas causing delays. I look for things like full table scans, missing indexes, or inefficient joins.

If the query is performing a full table scan, adding appropriate indexes to the columns used in the WHERE clause or JOIN operations can significantly improve performance. For instance, if the query frequently filters on a column, an index on that column can reduce the data retrieval time.

I also consider rewriting the query to simplify it or break it down into smaller parts if possible. For example, using subqueries or temporary tables helps streamline complex queries.

Additionally, I check for other factors, such as the proper use of joins, avoiding unnecessary columns in the SELECT statement, and ensuring that the statistics on the tables are up-to-date. These steps help ensure the query runs as efficiently as possible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you handle database deadlocks?

A

I would first try to identify the root cause of the deadlock by reviewing the database logs and deadlock graphs, which provide detailed information about the involved transactions and the resources they are contending for.

Depending on the query, I would potentially change the isolation level to read uncommitted or adding the nolock hint. I would also look at the query to see if it’s possible to break it up into shorter transactions or implementing temp tables.

Might suggest a deadlock retry mechanism if deadlocks are frequently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is database partitioning and when would you use it?

A

Database partitioning involves dividing a large table into smaller, more manageable pieces called partitions. Each partition is stored separately and can be queried individually, which can significantly improve performance and manageability, especially for very large datasets.

I would use partitioning when a table grows so large that query performance starts to degrade.

For instance, in a table storing historical transaction data, I might partition the data by month or year.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an OLTP database and how would you optimize one?

A

OLTP systems are designed for managing transactional data, focusing on fast query processing, high concurrency, and maintaining data integrity. They typically involve a large number of short, write-heavy transactions, such as insert, update, and delete operations.

To optimize an OLTP database, I would use techniques like normalization to reduce data redundancy, implement appropriate indexing to speed up query execution while keeping the number of indexes small to reduce overhead on write operations, and use efficient transaction management to handle concurrent access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you optimize a SQL query?

A
  • Query execution plan analysis to identify bottlenecks and missing indexes
  • Make sure indexes on columns used in WHERE, JOIN, and ORDER BY clauses (where appropriate)
  • Avoid Select *, retrieve only the amount of data needed
  • Avoid Correlated subqueries
  • Limiting rows with WHERE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the difference between WHERE and HAVING clauses.

A

WHERE - Filter rows before any grouping occurs, and it applies to individual rows in a the table. Used with SELECT, UPDATE, and DELETE

HAVING - Filter GROUPS OF ROWS created by GROUP BY clause. Used with aggregate functions such as COUNT, SUM, AVG

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the differences between INNER JOIN, LEFT JOIN, and RIGHT JOIN in SQL?

A
  • An INNER JOIN returns only the rows with a match between the two tables based on the join condition.
  • A LEFT JOIN returns all the rows from the left table and the matched rows from the right table; if there is no match, NULL values are returned for the columns from the right table.
  • A RIGHT JOIN is similar to a LEFT JOIN, but it returns all the rows from the right table and the matched rows from the left table, filling in NULLs where there is no match.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which DMV would you use to identify blocking sessions?

A

sys.dm_trans_locks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which DMV would you use to look for resource contention?

A

sys.dm_os_waiting_tasks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which DMV would you use to see what is currently running?

A

sys.dm_exec_requests

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How would you see who is on the DB currently and if anything is being blocked in their session?

A

sp_who2 and the isblocking column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the 4 types of locks?

A

S -> Select
X -> Exclusive
IX -> Intended Exclusive
U -> Update

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are the types of Isolation Levels?

A
  • Read Uncommitted
  • Read Committed
  • Repeatable Read
  • Snapshot
  • Serializable
23
Q

What is a correlated subquery?

A

a subquery that references columns from the outer query. It is re-executed for each row processed by the outer query. This makes it more dynamic, but potentially less efficient.

24
Q

Which DMV will show you whether you might need to rebuild or reorganize an index?

A

sys.dm_db_index_physical_stats

25
Q

What is contained in the Query Store?

A

Plan Store - Used for storing estimated execution plan information

Runtime stats score - Used for storing execution statistics information

Wait stats store - For persisting wait stats information

26
Q

How do you enable the query store?

A

ALTER DATABASE <DATABASE_NAME> SET QUERY_STORE = ON</DATABASE_NAME>

27
Q

What are some views in the query store?

A

Regressed Queries - shows queries that have regressed over time
Top Resource Consuming Queries
Query Waits Statistics - Analyzes most active wait categories

28
Q

What are some columns you might use in PERFMON?

A

Processing(_Total)% Processor Time
Paging File(_Total)% Usage
PhysicalDisk(_Total)\Avg. Disk sec/Read and Avg. Disk sec/Write
SQLServer:Buffer Manager\Page Life Expectancy

29
Q

What is Page Life Expectancy?

A

Page life expectancy indicates how long SQL Server expects a page to live in memory. You should monitor this value over time, and evaluate sudden drops. Such drops in the counter’s value could indicate poor query patterns, external memory pressure (for example, the server running a large SSIS package) or could just be normal system processing like running a consistency check on a large database

30
Q

Which waits could indicate CPU Contention?

A

SOS_SCHEDULER YIELD
CXPACKET

31
Q

Which waits could indicate Storage System Performance issues?

A

PAGEIOLATCH_SH

32
Q

What does it mean if a query is SARGable?

A

Refers to a predicate (WHERE clause) in a specific format that can use an index to speed up execution of a query

33
Q

What types of expressions are NON-SARGABLE?

A
  • LIKE with a wildcard at the Front
  • Functions in the where clause
34
Q

What is Parameter Sniffing and why can this be bad?

A

When the database engine executes a query for the first time, it will optimize the query based on the initial value of the parameter which will reduce the overall workload of compiling queries on the server.

This can be bad if the initial value of the parameter produces a non-optimal execution plan for parameters used down the line.

35
Q

What two categories do blocking problems usually fall into?

A
  • Poor Transactional Design
  • Long Running Transactions
36
Q

Which DMV will allow you to see what sessions are currently waiting?

A

sys.dm_exec_session_wait_stats

37
Q

Which wait type could be indicative of a blocking problem?

38
Q

Which wait type could be indicative of TempDB Contention?

A

PAGEIOLATCH_UP or PAGELATCH_EX on tempdb system pages

39
Q

What are some of the requirements for setting up a SQL Server Failover Cluster?

A

Almost all the same requirements as a RAC db in Oracle. Virtual network name for SQL Server, VIP for SQL Server, IP for Public and Private for each node in failover cluster, Shared Drives for SQL Server Data and Log Files, Quorum Disk, and MSDTC Disk.

40
Q

What are the prereqs for AlwaysOn?

A

Windows Failover Cluster Services on each node, full backup of primary db, full recovery mode, shared folder for backups

41
Q

How would you configure an AG to move all read operations to the secondary database?

A

Configure an AG listener with ApplicationIntent=ReadOnly

42
Q

Can you explain what a deadlock is and how SQL Server handles deadlocks?

A

A deadlock occurs when two or more sessions are waiting for each other to release locks, in a circular chain, so that they can proceed with their transaction. SQL Server detects deadlocks automatically and resolves them by choosing one process as a deadlock victim and rolling back its transaction, allowing other transactions to proceed.

43
Q

What are the considerations for securing SQL Server?

A

Authentication should be Windows Authentication where possible. Use TDE for Encryption at Rest and secure data in flight with SSL/TLS. Regularly apply SQL and OS Patches.

44
Q

What is DBCC and what are some examples?

A

Database Console Commands, DBCC CHECKDB, DBCC SHRINKDATABASE, DBCC SHRINKFILE

45
Q

Tell me about your process for troubleshooting Database Problems.

A
  • Identifying the issue by gathering information on symptoms and error messages
  • Isolate the problem by determining if it is related to the database, application, or infrastructure
  • Conduct tests to pinpoint the root cause and apply the necessary fixes
  • Post resolution, monitor the DB to ensure stability and document the issue, analysis, and resolution steps for further analysis
46
Q

How does SQL Server handle transactions, and what are the ACID properties?

A

SQL Server uses transactions to ensure a sequence of operations is completed successfully and maintains data integrity. The ACID properties are:

- Atomicity (all operations complete or none do)
- Consistency (data remains consistent before and after the transaction)
- Isolation (transactions are isolated from each other)
- Durability (once committed, changes are permanent)
47
Q

What is SQL Profiler/Extended Events, and how are they used?

A

SQL Server Profiler is a tool for monitoring and analyzing SQL Server events. It captures and records data about each event, such as query execution, performance metrics, and errors. It allows database administrators to diagnose performance issues, identify slow-running queries, and audit SQL Server activity. The captured trace data can be saved and replayed to troubleshoot issues or optimize SQL Server performance.

48
Q

What cmdlet in Powershell allows you to see more info on a given cmdlet?

A

Get-Help <Cmdlet_name></Cmdlet_name>

49
Q

How would you find all cmdlets in Powershell that have dba in their title?

A

Get-Command dba

50
Q

How would you find the server-level configuration settings using T-SQL?

A

SELECT * FROM SYS.CONFIGURATIONS ORDER BY NAME

51
Q

What table has information such as object, type, and created date?

A

sys.objects

52
Q

Which table has information on all user and system database files?

A

sys.master_files

53
Q

What table will allow you to see info on the database’s recovery model?

A

sys.databases