SQL 을 작성 할 때 주의해 야 할 세부 사항 Checklist 요약
9356 단어 Checklist
/*
-- : ( , )
CREATE TABLE CHECK1_T1
(
ID INT,
C1 CHAR(8000)
)
CREATE TABLE CHECK1_T2
(
ID INT,
C1 CHAR(8000)
)
DECLARE @I INT
SET @I=1
WHILE @I<=10000
BEGIN
INSERT INTO CHECK1_T1 SELECT @I,'C1'
INSERT INTO CHECK1_T2 SELECT 10000+@I,'C1'
SET @I=@I+1
END
CREATE TABLE CHECK2_T1
(
ID INT,
C1 CHAR(8000)
)
DECLARE @I INT
SET @I=1
WHILE @I<=10000
BEGIN
INSERT INTO CHECK2_T1 SELECT @I,'C1'
SET @I=@I+1
END
INSERT INTO CHECK2_T1 VALUES(10001,'C2')
INSERT INTO CHECK2_T1 VALUES(10002,'C1')
CREATE TABLE CHECK3_T1
(
ID INT,
C1 CHAR(7000)
)
CREATE TABLE CHECK3_T2
(
ID INT,
C1 CHAR(7000)
)
DECLARE @I INT
SET @I=1
WHILE @I<=20000
BEGIN
IF @I%2 =0
BEGIN
INSERT INTO CHECK3_T1 SELECT @I,'C1'
END
ELSE
BEGIN
INSERT INTO CHECK3_T1 SELECT @I,'C2'
END
IF @I%100=0
BEGIN
INSERT INTO CHECK3_T2 SELECT @I,'C1'
INSERT INTO CHECK3_T2 SELECT @I+50000,'C2'
END
SET @I=@I+1
END
CREATE TABLE CHECK4_T1
(
ID INT,
C1 CHAR(500),
)
DECLARE @I INT
SET @I=1
WHILE @I<=500000
BEGIN
IF @I%100000 =0
BEGIN
INSERT INTO CHECK4_T1 SELECT @I,'C2'
END
ELSE
BEGIN
INSERT INTO CHECK4_T1 SELECT @I,'C1'
END
SET @I=@I+1
END
CREATE NONCLUSTERED INDEX NCIX_C1 ON CHECK4_T1(C1)
CREATE TABLE CHECK5_T1
(
ID INT,
C1 CHAR(10),
)
DECLARE @I INT
SET @I=1
WHILE @I<=10000
BEGIN
INSERT INTO CHECK5_T1 SELECT @I,'C1'
IF @I%2=0
BEGIN
INSERT INTO CHECK5_T1 SELECT @I,'C1'
END
SET @I=@I+1
END
*/
--=====================================
--1、 Union all Union
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
-- :(26s) : -> ->
SELECT ID,C1 FROM CHECK1_T1 --1W
UNION
SELECT ID,C1 FROM CHECK1_T2 --1W
-- : (4s) : ->
SELECT ID,C1 FROM CHECK1_T1 --1W
UNION ALL
SELECT ID,C1 FROM CHECK1_T2 --1W
-- : union , , ALL
--=====================================
--2、 Exists Count(*)
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
---- : (7s) : -> ->
DECLARE @COUNT INT
SELECT @COUNT=COUNT(*) FROM CHECK2_T1 WHERE C1='C1' --1W
IF @COUNT>0
BEGIN
PRINT 'S'
END
---- : (0s) : / -> ->
IF EXISTS(SELECT 1 FROM CHECK2_T1 WHERE C1='C1') --1W
BEGIN
PRINT 'S'
END
-- : , Exist , COUNT(*) ,
--=====================================
--3、 IN(Select COL1 From Table)
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
-- : (3s) : ->
SELECT ID,C1 FROM CHECK3_T2 --400
WHERE ID IN (SELECT ID FROM CHECK3_T1 WHERE C1='C1') --2W
-- :(1s) : -> -> -> -> ->
SELECT A.ID,A.C1 FROM CHECK3_T2 A
INNER JOIN CHECK3_T1 B ON A.ID=B.ID WHERE B.C1='C1'
-- :(3s) : ->
SELECT A.ID,A.C1 FROM CHECK3_T2 A
WHERE EXISTS (SELECT 1 FROM CHECK3_T1 B WHERE B.ID=A.ID AND B.C1='C1')
-- : INNER JOIN ,SQL SERVER
--=====================================
--4、 Not Exists Not In
-- :(8s) : -> ->
SELECT ID,C1 FROM CHECK3_T1 --2W
WHERE ID NOT IN (SELECT ID FROM CHECK3_T2 WHERE C1='C1') --400
-- :(4s) : ->
SELECT A.ID,A.C1 FROM CHECK3_T1 A
WHERE NOT EXISTS (SELECT 1 FROM CHECK3_T2 B WHERE B.ID=A.ID AND B.C1='C1')
-- : NOT IN , , NOT EXISTS NOT IN
--=====================================
--5、
DROP TABLE CHECK4_T1
CREATE NONCLUSTERED INDEX NCIX_C1 ON CHECK4_T1(C1) --
--- :(4s) :
SELECT * FROM CHECK4_T1 WHERE RTRIM(C1)='C2'
--- :(0s) :
SELECT * FROM CHECK4_T1 WHERE C1='C2'
-- :where , ,
--=====================================
--6、 sp_executesql sql
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
CREATE PROC UP_CHECK5_T1 (
@ID INT
)
AS
SET NOCOUNT ON
DECLARE @count INT,
@sql NVARCHAR(4000)
SET @sql = 'SELECT @count=count(*) FROM CHECK5_T1 WHERE ID = @ID'
EXEC sp_executesql @sql,
N'@count INT OUTPUT, @ID int',
@count OUTPUT,
@ID
PRINT @count
CREATE PROC UP_CHECK5_T2 (
@ID INT
)
AS
SET NOCOUNT ON
DECLARE @sql NVARCHAR(4000)
SET @sql = 'DECLARE @count INT;SELECT @count=count(*) FROM CHECK5_T1 WHERE ID = ' + CAST(@ID AS VARCHAR(10)) + ';PRINT @count'
EXEC(@sql)
--- :
DECLARE @N INT
SET @N=1
WHILE @N<=1000
BEGIN
EXEC UP_CHECK5_T1 @N
SET @N=@N+1
END
--- :2s
DECLARE @N INT
SET @N=1
WHILE @N<=1000
BEGIN
EXEC UP_CHECK5_T2 @N
SET @N=@N+1
END
CREATE CLUSTERED INDEX CIX_ID ON CHECK5_T1(ID)
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
--
SELECT a.size_in_bytes ' ',
total_elapsed_time / execution_count ' ',
total_logical_reads / execution_count ' ',
usecounts ' ',
SUBSTRING(d.text, (statement_start_offset / 2) + 1, ((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(text)
ELSE statement_end_offset
END - statement_start_offset) / 2) + 1) ' '
FROM sys.dm_exec_cached_plans a
CROSS apply sys.dm_exec_query_plan(a.plan_handle) c,
sys.dm_exec_query_stats b
CROSS apply sys.dm_exec_sql_text(b.sql_handle) d
WHERE a.plan_handle = b.plan_handle
ORDER BY total_elapsed_time / execution_count DESC;
-- : , , ;
-- , ,
--=====================================
--7、 Left Join
-- : ->
SELECT A.ID,A.C1 FROM CHECK3_T1 A --2W
LEFT JOIN CHECK3_T2 B ON A.ID=B.ID WHERE B.C1='C1' --400
-- : ->
SELECT A.ID,A.C1 FROM CHECK3_T1 A
RIGHT JOIN CHECK3_T2 B ON A.ID=B.ID WHERE a.C1='C1'
-- : ->
SELECT A.ID,A.C1 FROM CHECK3_T1 A
INNER JOIN CHECK3_T2 B ON A.ID=B.ID WHERE B.C1='C1'
-- : , , INNER JOIN ,
-- ,WHERE LEFT RIGHT ,SQLSERVER , INNER JOIN
-- :(1s) : -> -> -> -> ->
SELECT A.ID,A.C1 FROM CHECK3_T2 A --400
INNER JOIN CHECK3_T1 B ON A.ID=B.ID WHERE A.C1='C1' --2W
-- : , ,
--=====================================
--8、 ON(a.id=b.id AND a.tag=3)
--
SELECT A.ID,A.C1 FROM CHECK3_T1 A
INNER JOIN CHECK3_T2 B ON A.ID=B.ID AND A.C1='C1'
--
SELECT A.ID,A.C1 FROM CHECK3_T1 A
INNER JOIN CHECK3_T2 B ON A.ID=B.ID WHERE A.C1='C1'
-- : : WHERE
--
SELECT A.ID,A.C1,B.C1 FROM CHECK3_T1 A
LEFT JOIN CHECK3_T2 B ON A.ID=B.ID AND B.C1='C1'
--
SELECT A.ID,A.C1,B.C1 FROM CHECK3_T1 A
LEFT JOIN CHECK3_T2 B ON A.ID=B.ID WHERE B.C1='C1'
-- : : ON WHERE
--=====================================
--9、 , Top 1
-- :(3s) :
DECLARE @ID INT
SELECT @ID=ID FROM CHECK1_T1 WHERE C1='C1'
SELECT @ID
-- :(0s) : ->
DECLARE @ID INT
SELECT TOP 1 @ID=ID FROM CHECK1_T1 WHERE C1='C1'
SELECT @ID
-- : TOP 1, , , CHECK1_T1 , @ID
--=====================================
--10、 CASE
DECLARE @S INT=1
SELECT * FROM CHECK5_T1
WHERE C1=(CASE @S WHEN 1 THEN C1 ELSE 'C2' END)
SELECT * FROM CHECK5_T1
WHERE @S=1 OR C1='C2'
/*--=====================================
、 Distinct. : -> -> ->
select distinct c1 from CHECK3_T1
、 Select *,
select c1 from CHECK4_T1
select * from CHECK4_T1
、Insert into Table(*),
、Isnull, isnull, ,
。
、 , ,
--=====================================*/