You are on page 1of 3

use Lab4tema;

--creare tabele
----------------------------------------------------------------------------
Create Table Ta(coda INT Identity Primary Key, a2 INT UNIQUE, a3 INT);
Create Table Tc(codc INT Identity Primary Key, coda INT Foreign Key REFERENCES
Ta(coda), c3 INT);

select*from Ta;
select*from Tc;

--populare tabel Ta
----------------------------------------------------------------------------
GO
CREATE PROCEDURE addValuesTa
AS
BEGIN
DECLARE @coda INT
Set @coda = 1
WHILE @coda <= 5000
BEGIN
INSERT INTO Ta(a2,a3) VALUES (@coda, @coda + 10);
SET @coda= @coda+1;
Print @coda
END;
END;
EXEC addValuesTa ;
SELECT * FROM Ta;

--populare tabel Tc
----------------------------------------------------------------------------
GO
CREATE PROCEDURE addValuesTc
AS
BEGIN

Declare @randomcoda int


Declare @c3 int

Declare @LowerLimitForCoda int


Declare @UpperLimitForCoda int

Set @LowerLimitForCoda = 1
Set @UpperLimitForCoda = 5000

Declare @LowerLimitForC3 int


Declare @UpperLimitForC3 int

Set @LowerLimitForC3 = 70
Set @UpperLimitForC3 = 2500

Declare @count int


Set @count = 1

While @count <= 30000


Begin
Select @randomcoda = Round(((@UpperLimitForCoda - @LowerLimitForCoda) *
Rand()) + @LowerLimitForCoda, 0);
Select @c3 = Round(((@UpperLimitForC3 - @LowerLimitForC3) * Rand()) +
@LowerLimitForC3, 0);
BEGIN
Insert Into Tc values (@randomcoda, @c3)
Set @count = @count + 1
END;
END;
END;
EXEC addValuesTc;

GO
select*from Tc order by coda;
select*from Tc order by codc;

--a -verifica indecsii


existenti--------------------------------------------------------------------------
-------------------------
EXEC sys.sp_helpindex @objname = N'Ta'
EXEC sys.sp_helpindex @objname = N'Tc'

SELECT *
FROM sys.indexes
WHERE object_id = OBJECT_ID('Ta')

-- Ta -- PK__Ta__357D4C04C3C5D2B8 (clustered, unique, primary key located on


PRIMARY)
-- UQ__Ta__3213A9FA56DDF5BA (nonclustered, unique, unique key located
on PRIMARY)
-- Tc -- PK__Tc__357D4CFAA7D89843 (clustered, unique, primary key located on
PRIMARY)

---Scrieti doua interogari SELECT pe tabelul Ta ale caror planuri de executie


sa contina operatorii---------------------------
--------clustered index seek-------------------------------
SELECT coda from Ta where coda > 50 AND coda < 100

--------nonclustered index seek------------------------------


SELECT a2 from Ta where a2 < 2000;

--b - Scrieti o interogare SELECT pe tabelul Ta cu o clauza de forma WHERE a3 =


valoare si analizati planul de executie. -----------

SELECT a3 from Ta where a3 = 3588; -- clustered index scan - primary key;


--5000 randuri citite,
estimated subtree cost 0.0176709
select * from Ta;

-- Creati un index nonclustered pe coloana a3 util pentru interogare.


---------------------------------------------------------------

create nonclustered index PentruA3 on Ta(a3);


drop index Ta.PentruA3;

-- evidentiati schimbarile din planul de executie: operatori, estimated subtree


cost pe SELECT.

SELECT a3 from Ta where a3 = 3588; -- index seek (NonClustered)


-- 1 rand citit, estimated
subtree cost 0.0032831
-----------------------------------------------------------------------------------
--------------------------
--c- Scrieti o interogare SELECT cu INNER JOIN intre Tc si Ta (cheie externa =
cheie primara) --------------

Select *
from Tc inner join Ta
on Tc.coda = Ta.coda

--si o filtrare de forma coloana utilizata in JOIN = valoare. Analizati planul de


executie.

Select *
from Tc inner join Ta
on Tc.coda = Ta.coda
where Tc.coda = 1999

--face un Clustered index scan cost


-- apoi un clustered index seek cost
--inner join ( Nested Loops)
--select -- estimated subtree cost 0.111028
--(missing index - impact 93.5381) create nonclustered index

--Creati un index nonclustered pe coloana care este cheie externa in Tc si


analizati din nou planul de executie
create nonclustered index IndexCoda on Tc(coda);

Select *
from Tc inner join Ta
on Tc.coda = Ta.coda
where Tc.coda = 1999

-- key lookup (Clustered)


--index seek (nonClustered)
--nested Loops (inner join)
--clustered index seek
--nested Loops (inner join)
--select-- estimated subtree cost 0.0238366 - mai rapid

drop index Tc.IndexCoda;

You might also like