, , , ( , SQL Server). , , , - , . SQL Server, , - :
CREATE TABLE smb_header (keycol INTEGER NOT NULL
, name1 VARCHAR2(255)
, name2 VARCHAR2(255));
INSERT INTO smb_header
VALUES (1
, 'This is column 1'
, 'This is column 2'
);
INSERT INTO smb_header
SELECT (SELECT MAX(keycol)
FROM smb_header
) + keycol
, name1
, name2
FROM smb_header;
REM (repeat 20 times to generate ~1 million rows)
ALTER TABLE smb_header ADD PRIMARY KEY (keycol);
CREATE TABLE smb_detail (keycol INTEGER
, commentno INTEGER
, commenttext VARCHAR2(255));
INSERT INTO smb_detail
SELECT keycol
, 1
, 'A comment that describes this issue'
FROM smb_header;
ALTER TABLE smb_detail ADD PRIMARY KEY (keycol, commentno);
ALTER TABLE smb_detail ADD FOREIGN KEY (keycol)
REFERENCES smb_header (keycol);
INSERT INTO smb_detail
SELECT keycol
, (SELECT MAX(commentno)
FROM smb_detail sd2
WHERE sd2.keycol = sd1.keycol
) + commentno
, 'A comment that follows comment number '
+ CAST(sd1.commentno AS VARCHAR(32))
FROM smb_detail sd1
WHERE MOD(keycol, 31) = 0;
REM repeat 5 times, to create some records that have 64 comments
REM where others have one.
1 , - 1 64 .
( , , ) :
alter table dbo.smb_header add CommentCountPersist as dbo.CountComments(keycol)
, PERSISTED , - SQL Server , , , PERSISTED :
Msg 4934, Level 16, State 3, Line 1
Computed column 'CommentCountPersist' in table 'smb_header' cannot be
persisted because the column does user or system data access.
- , SQL Server , , , , , , .
. #holder : , , , , Mgmt Studio.
SELECT h.keycol
, h.name1
, CommentCount
INTO
FROM smb_header h
WHERE h.keycol < 0
. :
INSERT
INTO
SELECT h.keycol
, h.name1
, CommentCount
FROM smb_header h
WHERE h.keycol between 5000 and 10000
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
Table 'Worktable'. Scan count 1, logical reads 10160, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
Table 'smb_header'. Scan count 1, logical reads 44, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 265 ms, elapsed time = 458 ms.
(5001 row(s) affected)
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
GROUP BY, :
INSERT
INTO
SELECT h.keycol
, h.name1
, COUNT(*)
FROM smb_header h
, smb_detail d
WHERE h.keycol between 5000 and 10000
AND h.keycol = d.keycol
GROUP BY h.keycol, h.name1
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
Table 'smb_header'. Scan count 1, logical reads 44, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
Table 'smb_detail'. Scan count 1, logical reads 366, physical reads 0,
read-ahead reads 0, lob logical reads 0,
lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 15 ms, elapsed time = 13 ms.
(5001 row(s) affected)
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
SELECT, Remus, , GROUP BY ( ).
, . , (*) , .
, - . , marc_s .