Session 7 - Transactions, Indexes, Optimize Queries, dynamic views, cursors Flashcards
What is the term for when a database searches an indexed value?
SEEK
What is the term for when a database searches for a non indexed value?
SCAN
What are the characteristics of a transaction?
- Atomicity - Single unit of work
- Consistency - Everything has to be done
- Isolation - Each transaction must be isolated (database locking)
- Durability - Once a transaction is done, it is committed - Permanent
What is it called when you write a statement that starts and ends without the user telling the computer that it’s a transaction?
Implicit Transaction.
What is it called when you tell the computer when you start and end a transaction?
Explicit Transaction.
What are the two ways to end a transaction?
COMMIT TRANSACTION
ROLLBACK TRANSACTION
How do you assign in the log the name of a transaction?
BEGIN TRANSACTION tranName WITH MARK
ROLLBACK TRANSACTION tranName
What is the net effect if you rollback a transaction that has nested transactions?
Any transactions that were committed within the parent transaction will be rolled back as well.
What happens if you rollback a child nested transaction and try to commit the parent transaction?
It will fail because a rollback transaction rolls back EVERYTHING.
How do you find out the nested level of a transaction?
SELECT @@TRANCOUNT
What are the scope and types of locks?
- Intent Shared (IS)
- Shared (S)
- Update (U)
- Intent Exclusive (IE)
- Shared with intent exclusive (SIX)
- Exclusive (X)
What are the elements of shared locks? (SELECT query)
- Allows concurrent transactions to read (SELECT)
- No other transactions can modify the data
- Released as soon as the read operation is completed
What are the elements of Update Locks?
- Used to prevent deadlock
- Transactions reading data - Shared Lock
- To update data, must convert to exclusive
- Cannot convert to Exclusive if there is a Shared Lock.
- Solution - Use Update Lock; then convert to Exclusive Lock.
What are the elements of Exclusive Locks?
- Prevents access by concurrent transactions
2. Used by INSERT, UPDATE, DELETE
What are the elements of Intent Locks?
- Signal intent to place locks at a lower level.
- Prevent other transactions from modifying higher level resources
- Detecting lock conflicts
What are the scopes where locks are used?
Rows, Pages and Tables
What is the order that locks go?
Shared -> Update -> Exclusive
How can you delay a transaction from running for a specified amount of time (EX. 10 Seconds)?
WAITFOR DELAY ‘00:00:10’
How can you specify when to run a transaction at a specific time (EX. 10am)?
WAITFOR TIME ‘10:00:00’
What are the problems with no concurrency control (ie: no locks, etc)
- Lost Updates
- Uncommitted dependency (Dirty Read)
- Inconsistent analysis (nonrepeatable read)
- Phantom Reads
- Missing and double reads caused by row updates
- Missing one or more rows that were not the target of update
What are the transaction isolation levels?
- Read Committed (Default Isolation Level)
- Read Uncommitted
- Repeatable Read
- Snapshot
- Serializable
What are the transaction isolation level effects for:
Read Uncommitted
Dirty Read - YES
NonRepeatable Read - YES
Phantom - YES
What are the transaction isolation level effects for:
Read Committed
Dirty Read - NO
NonRepeatable Read - YES
Phantom - YES
What are the transaction isolation level effects for:
Repeatable Read
Dirty Read - NO
NonRepeatable Read - NO
Phantom - YES
What are the transaction isolation level effects for:
Snapshot
Dirty Read - NO
NonRepeatable Read - NO
Phantom - NO
What are the transaction isolation level effects for:
Serializable
Dirty Read - NO
NonRepeatable Read - NO
Phantom - NO
What is the syntax for changing the isolation level?
SET TRANSACTION ISOLATION LEVEL [Read Uncommitted, Read Committed, Repeatable Read, Snapshot, Serializable]
What is a heap?
A set of rows contained in pages that are connected through an Index Allocation Map, not indexed and not ordered.
What is the estimated size of a page within a heap?
8000 characters.
When would a heap be a better way to store data?
If you have to input large amounts of data at one time and sort through it later.
What is the better way to store data than a heap?
B-Tree (Balance tree)
How is a B-Tree organized?
It’s a hierarchal design with equal rows being stored in pages and balanced all the way around.
What is the syntax to create a clustered index?
CREATE CLUSTERED INDEX indexName ON tableName (RowsToSortBy)
What’s the syntax to drop an index?
DROP INDEX IndexName ON tableName
What’s the difference between a clustered index and a primary key (Regarding Indexes)?
A clustered index will just sort the data, but it doesn’t care about duplicates, whereas a primary key does care about duplicates.
What are the two ways to convert a heap to a B-Tree?
Create a primary key or a clustered index on the table.
What is the syntax to create a non clustered index?
CREATE NONCLUSTERED INDEX indexName ON tableName(Columns)
What is the syntax to create a non clustered index that is filtered on specific data? (Filtered Index)
CREATE NONCLUSTERED INDEX indexName ON tableName(Columns) WHERE ColumnToFilter = ‘filter’
What are the different nodes in a B-Tree
Root level/node
Intermediate Level Branch Node
Leaf level/node
What node contains actual data in a B-Tree?
Leaf Level/Node
What do the Root level/nodes and Intermediate Level branch nodes contain?
The Key Fields that the index was created on.
What is the syntax for creating a non clustered index that also contains other data columns, but not storing them as key fields?
CREATE NONCLUSTERED INDEX indexName ON tableName(KeyColumn) INCLUDES(columnsToInclude)
What is the benefit to including columns in a nonclustered index, but not as key fields?
The index doesn’t have to store the data in the Root and intermediate nodes, only in the leaf node.
What are the different join types regarding query plans?
Hash Match - Least Efficient
Nested Loop Match (One Small Table to One Large Table) - Efficient
Merge Match (Larger Tables, sorted on join) - Very Efficient
Do Hash Match and Nested Loop Match require indexes?
No.
How does a nested loop work?
It will query the small table first, then for the rows that are returned, it will do the select query for every row returned. Much more efficient if the bigger table is indexed.
When can you get a merge join?
When the two larger tables that are queried are indexed and the data is found from a clustered index scan.
What is a SARG and why is it important?
A SARG is a Search ARGument. It is a where clause which can use an index. (Do not use functions in the where clause)
A proper SARG will improve your query performance immensely.
What are the query cost implications of sorting your query?
It will slow down your query significantly. Do not sort unless you absolutely have to.
What are the query cost implications when you run a stored procedure? How can you overcome this?
The first time you run the procedure, the system creates an execution plan and then will reuse that every time after.
Use CREATE PROC procName WITH RECOMPILE
That will recompile the procedure and create a plan every time the procedure is called.
What are all of the hints that you can use with a query?
WITH () –separated with comma:
- NOLOCK
- READUNCOMMITTED
- UPDLOCK
- REPEATABLEREAD
- SERIALIZABLE
- READCOMMITTED
- TABLOCK
- TABLOCKX
- PAGLOCK
- ROWLOCK
- NOWAIT
- READPAST
- XLOCK
- SNAPSHOT
- NOEXPAND
- FORCESEEK
- FORCESCAN
How would you force SQL to use a different join type (Hash, Merge, Loop)
put it before JOIN:
LEFT HASH JOIN
RIGHT MERGE JOIN
INNER LOOP JOIN
What is the syntax used to show statistics about the disk usage that happens from a query?
SET STATISTICS IO ON
GO
What does STATISTICS IO show you when you run a query?
The tables read
Scan (or seek) count (after leaf level in any direction)
Logical Reads - Number of pages from cache
Physical reads - How often gone to disk
read-ahead reads - Pages placed in cache
lob logical reads - Large object blocks of data
lob physical reads
lob read ahead reads
What is the syntax used to see the query plan in a table format?
SET SHOWPLAN_ALL ON
GO
What are the other SHOWPLANS that can be used?
SHOWPLAN_XML
SHOWPLAN_TEXT
Both are for use outside of SSMS
What does Client Statistics show you?
The number of statements, rows affected, select statements, transactions run by the client.
Network statistics
Time Statistics
What is the syntax to show the CPU time used for a query?
SET STATISTICS TIME ON
GO
What is the stored procedure used for running dynamic SQL? What are the parameters?
SYS.sp_executesql
@statement, @params, @ParamName = ‘value’, @Param2 = ‘value2’
@statement and @params both need to be NVarchar datatypes
What is a DMV?
Dynamic Management View or Dynamic Management Function. (SYS.)
How can you run a dynamic SQL without running sys.sp_executesql?
Just run:
exec (‘query statement’)
Drawback is that this is prone to SQL injection. DO NOT USE!
What is the DMV used for seeing information about how an index is used, including seeks, scans, lookups and so forth?
SYS.dm_db_index_usage_stats
What is the function used to get a database name?
db_name(database_id)
What is the function used to get the database id you are currently using?
db_id()
What is the function used to get the object name from an object ID?
object_name(object_id)
What is the DMV for getting physical statistics for an index? What are the parameters?
sys.dm_db_index_physical_stats
DatabaseID
ObjectID (or null if you want all)
IndexID (or null if you want all)
PartitionNumber (or null if you want all)
Mode (Detailed, null, limited or sampled) – scan level
This shows tons of information about indexes including fragmentation, usage, disk and so forth
What DMV would I use to be able to show Indexes that the system suggests should be created?
sys.dm_db_missing_index_details
Shows Key columns to include as well as columns that should be included
What DMV can you use that shows you indexes that are missing columns?
sys.dm_db_missing_index_columns(indexName)
What is a cursor?
It is a row-based operator (allowing you to go RBAR)
FOR or WHILE loop
What are the steps for using a cursor?
How do you know when the cursor is finished?
- Declare cursorName CURSOR FOR SELECT statement FROM table
- OPEN cursorName
- FETCH NEXT FROM cursorName INTO @VAR
WHILE @@FETCH_STATUS = 0
BEGIN
STATEMENT
FETCH NEXT FROM cursorName INTO @VAR
END - CLOSE cursorName
@@FETCH_STATUS - returns 0 if it is still returning data, anything else if it’s failed
What is the drawback to scalar functions and what is one way to resolve that?
They can be a large drag on resources.
Don’t use the functions unless there isn’t an alternative
What are ways that you can combine multiple DML operations?
Merge Use of OR and AND in WHERE clause Other uses of WHERE clause JOINs Procedures