70-461session7 SQL Server Exam
70-461session7 SQL Server Exam
XML indexing
Manage transactions
Trancount
isolation levels;
Transaction 1
begin transaction
commit tran
Transaction 2
set transaction isolation level read committed
Query and manage XML data
XML indexing
begin tran
select * from [dbo].[tblEmployee]
waitfor delay '00:00:20'
select * from [dbo].[tblEmployee]
commit tran
Clustered Index
create clustered index idx_tblEmployee on [dbo].[tblEmployee]([EmployeeNumber])
select *
into [dbo].[tblEmployee2]
from [dbo].[tblEmployee]
where EmployeeNumber <> 131
Non-clustered Index
create nonclustered index idx_tblEmployee_DateOfBirth on [dbo].[tblEmployee]
([DateOfBirth])
create nonclustered index idx_tblEmployee_DateOfBirth_Department on [dbo].
[tblEmployee]([DateOfBirth],Department)
Filtered indices
INCLUDE
CREATE NONCLUSTERED INDEX idx_tblEmployee_Employee
ON dbo.tblEmployee(EmployeeNumber) include (EmployeeFirstName);
Table Scan
select *
from [dbo].[tblEmployee] as E
where E.EmployeeNumber = 134
Hash match:
select *
from [dbo].[tblDepartment] as D
left join [dbo].[tblEmployee] as E
on D.Department = E.Department
Nested Loop
select *
from [dbo].[tblEmployee] as E
left join [dbo].[tblTransaction] as T
on E.EmployeeNumber = T.EmployeeNumber
Merge Joins
CREATE UNIQUE CLUSTERED INDEX [idx_tblEmployee] ON [dbo].[tblEmployee]
([EmployeeNumber])
GO
GO
select *
into dbo.tblEmployeeNoIndex
from dbo.tblEmployee
select *
into dbo.tblTransactionNoIndex
from dbo.tblTransaction
plan guides
select *
into dbo.tblTransactionBig
from [dbo].[tblTransaction]
exec procTransactionBig 1
Hints
select D.Department, D.DepartmentHead, E.EmployeeNumber, E.EmployeeFirstName,
E.EmployeeLastName
from [dbo].[tblDepartment] as D WITH (NOLOCK)
left join [dbo].[tblEmployee] as E
on D.Department = E.Department
where D.Department = 'HR'
EXECUTE (@sql);
EXECUTE (@sql);
EXECUTE sys.sp_executesql
@statement =
N'
SELECT *
FROM [dbo].[tblTransaction] AS T
WHERE T.EmployeeNumber = @EmployeeNumber;',
@params = N'@EmployeeNumber varchar(1000)',
@EmployeeNumber = @param;
Query and manage XML data
XML indexing
sys.dm_db_missing_index_details
select T.*
into dbo.tblTransactionBigger
from [dbo].[tblTransaction] as T
cross join [dbo].[tblTransaction] as T2
sys.dm_db_index_physical_stats
SELECT * FROM sys.dm_db_index_physical_stats
(DB_ID(N'70-461'), OBJECT_ID(N'dbo.tblEmployee'), NULL, NULL , 'DETAILED');
database_id object_id
index_id/partition_number/mode
Query and manage XML data
XML indexing
open csr
fetch next from csr into @EmployeeID
while @@FETCH_STATUS = 0
begin
select * from [dbo].[tblTransaction] where EmployeeNumber = @EmployeeID
fetch next from csr into @EmployeeID
end
close csr
deallocate csr
Alternatives
select T.*
from tblTransaction as T
right join tblEmployee as E
on T.EmployeeNumber = E.EmployeeNumber
where E.EmployeeNumber between 120 and 299
and T.EmployeeNumber is not null
set showplan_all on
go
set showplan_text on
go
select [EmployeeNumber], dbo.fnc_TransactionTotal([EmployeeNumber])
from [dbo].[tblEmployee]
-- You can optionally specify the TYPE directive to retrieve the results as xml
type. The TYPE directive does not change the content of the results. Only the
data type of the results is affected. +
<row>
<ProductID>706</ProductID>
<Name>HL Road Frame - Red, 58</Name>
<SubcategoryName>Road Frames</SubcategoryName>
</row>
<row>
<ProductID>707</ProductID>
<Name>Sport-100 Helmet, Red</Name>
<SubcategoryName>Helmets</SubcategoryName>
</row>
<row>
<ProductID>708</ProductID>
<Name>Sport-100 Helmet, Black</Name>
<SubcategoryName>Helmets</SubcategoryName>
</row>
<row>
<ProductID>709</ProductID>
<Name>Mountain Bike Socks, M</Name>
<SubcategoryName>Socks</SubcategoryName>
</row>
AUTO
select P.ProductID, P.Name, S.Name as SubcategoryName
from [Production].[Product] as P
left join [Production].[ProductSubcategory] as S
on P.ProductSubcategoryID = S.ProductSubcategoryID
where P.ProductID between 700 and 709
for xml auto
<P>
<ProductID>706</ProductID>
<Name>HL Road Frame - Red, 58</Name>
<S>
Query and manage XML data
XML indexing
<SubcategoryName>Road Frames</SubcategoryName>
</S>
</P>
<P>
<ProductID>707</ProductID>
<Name>Sport-100 Helmet, Red</Name>
<S>
<SubcategoryName>Helmets</SubcategoryName>
</S>
</P>
<P>
<ProductID>708</ProductID>
<Name>Sport-100 Helmet, Black</Name>
<S>
<SubcategoryName>Helmets</SubcategoryName>
</S>
</P>
<P>
<ProductID>709</ProductID>
<Name>Mountain Bike Socks, M</Name>
<S>
<SubcategoryName>Socks</SubcategoryName>
</S>
</P>
EXPLICIT
select 1 as Tag, NULL as Parent
, P.ProductID as [Product!1!ProductID]
, P.Name as [Product!1!ProductName]
, S.Name as [Product!1!SubcategoryName]
from [Production].[Product] as P
left join [Production].[ProductSubcategory] as S
on P.ProductSubcategoryID = S.ProductSubcategoryID
where P.ProductID between 700 and 709
for xml explicit
PATH
select P.ProductID, P.Name, S.Name as SubcategoryName
from [Production].[Product] as P
left join [Production].[ProductSubcategory] as S
on P.ProductSubcategoryID = S.ProductSubcategoryID
where P.ProductID between 700 and 709
for xml path
<row>
<ProductID>706</ProductID>
<Name>HL Road Frame - Red, 58</Name>
Query and manage XML data
XML indexing
<SubcategoryName>Road Frames</SubcategoryName>
</row>
<row>
<ProductID>707</ProductID>
<Name>Sport-100 Helmet, Red</Name>
<SubcategoryName>Helmets</SubcategoryName>
</row>
<row>
<ProductID>708</ProductID>
<Name>Sport-100 Helmet, Black</Name>
<SubcategoryName>Helmets</SubcategoryName>
</row>
<row>
<ProductID>709</ProductID>
<Name>Mountain Bike Socks, M</Name>
<SubcategoryName>Socks</SubcategoryName>
</row>
<Products>
<ProductID>706</ProductID>
<Name>HL Road Frame - Red, 58</Name>
<SubcategoryName>Road Frames</SubcategoryName>
</Products>
<Products>
<ProductID>707</ProductID>
<Name>Sport-100 Helmet, Red</Name>
<SubcategoryName>Helmets</SubcategoryName>
</Products>
<Products>
<ProductID>708</ProductID>
<Name>Sport-100 Helmet, Black</Name>
<SubcategoryName>Helmets</SubcategoryName>
</Products>
<Products>
<ProductID>709</ProductID>
<Name>Mountain Bike Socks, M</Name>
<SubcategoryName>Socks</SubcategoryName>
</Products>
<Products ProductID="706">
<Name>HL Road Frame - Red, 58</Name>
<SubcategoryName>Road Frames</SubcategoryName>
</Products>
<Products ProductID="707">
Query and manage XML data
XML indexing
<Name>Sport-100 Helmet, Red</Name>
<SubcategoryName>Helmets</SubcategoryName>
</Products>
<Products ProductID="708">
<Name>Sport-100 Helmet, Black</Name>
<SubcategoryName>Helmets</SubcategoryName>
</Products>
<Products ProductID="709">
<Name>Mountain Bike Socks, M</Name>
<SubcategoryName>Socks</SubcategoryName>
</Products>
SELECT @x.query('
for $Item in /Shopping/ShoppingTrip/Item
return string($Item)
')
SELECT @x.query('
for $Item in /Shopping/ShoppingTrip/Item
return concat(string($Item),";")
')
SELECT @x.query('
for $Item in /Shopping/ShoppingTrip[1]/Item
order by $Item/@Cost
return concat(string($Item),";")
')
Bananas; Cherries; Apples;
SELECT @x.query('
for $Item in /Shopping/ShoppingTrip[1]/Item
let $Cost := $Item/@Cost
where $Cost = 4
order by $Cost
return concat(string($Item),";")
')
Query and manage XML data
XML indexing
Apples;
Modify
SET @x.modify('
replace value of (/Shopping/ShoppingTrip[1]/Item[3]/@Cost)[1]
with "5.0"
')
SELECT @x
SET @x.modify('
insert <Item Cost="5">Manu Item 5 at Loc 1</Item>
into (/Shopping/ShoppingTrip)[1]
')
SELECT @x
SET @x.modify('
delete (/Shopping/ShoppingTrip)[1]
')
SELECT @x
Value
SELECT @x.value('(/Shopping/ShoppingTrip/Item)[1]','varchar(50)')
Query and manage XML data
XML indexing
Apples
SELECT @x.value('(/Shopping/ShoppingTrip/Item/@Cost)[1]','varchar(50)')
Nodes
select T2.Loc.query('.') from @x.nodes('/Shopping/ShoppingTrip') as T2(Loc) –
Table(Column) –shreds xml into relational data
<ShoppingTrip
ShoppingTripID="L2"><Item>Diamonds</Item><Item>Emeralds</Item><Item>Furniture</
Item></ShoppingTrip>
https://docs.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type
select T2.Loc.value('@Cost','varchar(50)')
NULL
NULL
NULL
declare @x xml
set @x='<Shopping ShopperName="Phillip Burton" >
<ShoppingTrip ShoppingTripID="L1" >
<Item Cost="5">Bananas</Item>
<Item Cost="4">Apples</Item>
<Item Cost="3">Cherries</Item>
</ShoppingTrip>
<ShoppingTrip ShoppingTripID="L2" >
<Item>Emeralds</Item>
<Item>Diamonds</Item>
<Item>Furniture</Item>
</ShoppingTrip>
</Shopping>'
SELECT MyTable.ColXML.query('.')
FROM #tblXML
Query and manage XML data
XML indexing
CROSS APPLY xmlCol.nodes('Shopping/ShoppingTrip') as MyTable(ColXML)
<ShoppingTrip ShoppingTripID="L2"><Item>Emeralds</Item><Item>Diamonds</Item><Item>Furniture</Item></
ShoppingTrip>
SELECT MyTable.ColXML.value('@Cost','varchar(50)')
FROM #tblXML
CROSS APPLY xmlCol.nodes('Shopping/ShoppingTrip/Item') as MyTable(ColXML)
NULL
NULL
NULL
XML data: how to handle it in SQL Server and when and when not to use it,
including XML namespaces
select P.ProductID, P.Name, S.Name as SubcategoryName
from [Production].[Product] as P
left join [Production].[ProductSubcategory] as S
on P.ProductSubcategoryID = S.ProductSubcategoryID
where P.ProductID between 700 and 709
for xml raw,xmldata --this is being depreciated
<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes"
elementFormDefault="qualified">
<xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes"
schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/
sqltypes.xsd" />
<xsd:element name="row">
<xsd:complexType>
<xsd:attribute name="ProductID" type="sqltypes:int" use="required" />
<xsd:attribute name="Name" use="required">
<xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].
[Name]">
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033"
sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth"
sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="SubcategoryName">
<xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].
[Name]">
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033"
sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth"
sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ProductID="706" Name="HL
Road Frame - Red, 58" SubcategoryName="Road Frames" />
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ProductID="707"
Name="Sport-100 Helmet, Red" SubcategoryName="Helmets" />
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ProductID="708"
Name="Sport-100 Helmet, Black" SubcategoryName="Helmets" />
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet2" ProductID="709"
Name="Mountain Bike Socks, M" SubcategoryName="Socks" />
GO
Query and manage XML data
XML indexing
XML indexing
CREATE XML INDEX secpk_tblXML_Path on #tblXML(xmlCol)
USING XML INDEX pk_tblXML FOR PATH;
CREATE XML INDEX secpk_tblXML_Value on #tblXML(xmlCol)
USING XML INDEX pk_tblXML FOR VALUE;
CREATE XML INDEX secpk_tblXML_Property on #tblXML(xmlCol)
USING XML INDEX pk_tblXML FOR PROPERTY;