SQL Server
Transkrypt
SQL Server
przygotował: [email protected] Installation MS SQL Server przygotował: [email protected] Conditions 1. Every laboratory must be passed and student gets mark for every laboratory 2. Student must pass finall test, what gives opportunity to get original Microsoft diploma. 3. Final mark will be an average of marks got during laboratory and test calculated with the formula: Final mark = (lab_mark + 2*test_mark)/3 przygotował: [email protected] Plan of lecture 1. Installation and configuration of MS SQLServer. 2. Creation and configuration of databases. 3. Users managment. 4. Server administration. 5. Transact SQL. 6. Integration Services. 7. Tuning queries. 8. OLAP. przygotował: [email protected] MS SQL Server MS SQL Server – relational database with services enabling management of stored data. Data models: OLTP – On-Line Transaction Processing, OLAP – On-Line Analitycal Processing. przygotował: [email protected] Overview • SQL Server versions – First usable: 6.5 – First popular: 7.0 – Other: 2000, 2005 – Current: 2008 przygotował: [email protected] MS SQLServer’s tasks MS SQLServer as database management system: stores data for transactional and analytical processing, stores and handles relationships among data, ensures correctness of data and controls relational rules, answers client applications requests, preserves and secures data, restores data from backups after hardware or software failures. przygotował: [email protected] MS SQL Server Client SQL Server Results Query OLTP OLAP Client application Relational Database System przygotował: [email protected] Client – server architecure Client components: 1. Client Application 2. Database API – OLE DB, ODBC, JDBC 3. Client Net–Libraries Server components: 1. Server Net–Libraries 2. Open Data Service 3. Relational Engine 4. Storage Engine przygotował: [email protected] Communication between client and server 1 Query Result Set Database API (OLE DB, ODBC, DB-Library) 2 Client SQL Server Client Application Client Net-Library Server Net-Libraries 3 Open Data Services Query 5 Result Set Relational Engine Server 4 Storage Engine Processor Memory Local Database przygotował: [email protected] MS SQL Server components 1. SQL Server Database Engine 2. SQL Server Agent 3. Analysis Services 4. Reporting Services 5. Notification Services 6. Integration Services przygotował: [email protected] SQL Server versions 1. Standard Edition 2. Workgroup Edition 3. Enterprise Edition 4. Developer Edition 5. Microsoft Database Engine (MSDE) (2k) 6. Express Edition (2k5) 7. Mobile Edition przygotował: [email protected] Requirements 2k5 (32(32-bit) Component Minimal requirements Processor Pentium III, 600 MHz, better 1 GHz Memory Minimum 512 MB better 1 GB Hard disk 356 MB MB 150 MB (database server) 80 MB (client tools) 75 MB (Analysis, Reporting Services) 80 MB (English Query) przygotował: [email protected] Instalation options 2k5 1. x86 – based operating systems 2. x64 – based operating systems 3. Itanium – based operating systems przygotował: [email protected] Installation – parameters Instance naming: Default name – computer Windows name Named instance –format: <computer name>\<instance name>. Multiple instances przygotował: [email protected] Instalation parameters Naming instances - rules: Shorter than 16 characters case sensitive, Reserved names Default and MSSQLServer, Classic rules – no spaces, First character must be a letter, &, _, or #. przygotował: [email protected] Installation parameters Authetication modes: Windows Authentication Mode – SQL Server uses Windows 2000 or NT accounts. Mixed Mode – SQL Sever uses Windows 2000 or NT accounts (Windows Authentication) or SQL Server accounts (SQL Server Authentication) Other paramaters: Collation Settings przygotował: [email protected] Server components SQL Server, SQL Server Agent, SQL Server Integration Service, SQL Server Browser SQL Server FullText Search SQL Server Reporting Services SQL Server Analysis Services SQL Server VSS Writer Distributed Transaction Coordinator przygotował: [email protected] Other components Client Connectivity Books Online Business Intelligence Development Studio (2k5) Code Samples przygotował: [email protected] Database service 1. MS SQL Server: database engine, executes Transact-SQL queries and manages all files containing database: a. handles resources for users, b. coordinates user’s requests, c. ensures data consistency. przygotował: [email protected] Some other services SQL Server Reporting Service SQL Server Analysis Service SQL Server Notification Service ... przygotował: [email protected] Verification – services Running services: Control Panel/Administration Tools/Services SQL Service Manager SQL Server Management Studio przygotował: [email protected] Databases System Databases master model tempdb msdb Adventure Works User_1 User_2 User Databases distribution przygotował: [email protected] Creating database • The simplest command: CREATE DATABASE baza • Copy of model database is created C:\...\Data\baza.mdf C:\...\Data\baza.ldf przygotował: [email protected] Data files CREATE DATABASE baza ON (NAME=nazwa, FILENAME=‘nazwa_pliku’, SIZE=10MB, MAXSIZE = 20MB | UNLIMITED FILEGROWTH = 20% | 2MB),... LOG ON (NAME=nazwa, FILENAME=‘nazwa_pliku’, SIZE=10MB, MAXSIZE = 20MB | UNLIMITED FILEGROWTH = 20% | 2MB),... przygotował: [email protected] Why do we need log? 1 Data modification Cache 2 Read page from disk 3 Save to log Disk Disk 4 Save change to database przygotował: [email protected] CHECKPOINT update tematy set nazwa=‘dysk’ where nr>10 insert into pracownicy values(19,’Kowalski’) update tablica set pole1=‘xyz’ where pole2>10 update tematy set nazwa=‘dysk’ where nr>10 insert into pracownicy values(19,’Kowalski’) update tablica set pole1=‘xyz’ where pole2>10 update tablica set pole1=‘xyz’ where pole2>10 update tematy set nazwa=‘dysk’ where nr>10 insert into pracownicy values(19,’Kowalski’) update tablica set pole1=‘xyz’ where pole2>10 update tablica set pole1=‘xyz’ where pole2>10 przygotował: [email protected] Transaction log • • • • • • Every data change is stored in log file first Every operation has transaction defined Begins and commits are stored in log CHECKPOINT – place to which data is stored on disk Every data write to disk changes CHECKPOINT position In case of failure – rollback of not commited transaction and rollforward of commited przygotował: [email protected] Format of datafiles • Page – 8kB • Extent – 8 consecutive pages – Uniform extent – pages from one object – Mixed extent – contains pages from different objects przygotował: [email protected] Begining of the file Header PFS GAM SGAM Page0: File Header – logical name, prameters (size chcnge params Page1: PFS (Page Free Space)– 1 byte about every page (8000 first pages– 64MB) Page2: GAM (Global Allocation Map) – bit for every extend – free or full (64k extents = 2GB) Page3: SGAM (Shared Global Allocation Map) – bit for every extent – are there free pages? (64k extents = 2GB) przygotował: [email protected] Meaning of GAM & SGAM bits Content of extent Bit GAM Bit SGAM Free 1 0 Uniform or full 0 0 Mixed with free pages 0 1 przygotował: [email protected] Objects allocation • IAM page (Index Allocation Map) – Pointers to first 8 pages – Bitmap of extents (for 512 000 pages) przygotował: [email protected] Max values... Databases 32.767 Databse size 1.048.516 TB Files for database 32.767 File size 32 TB Objects in database (tables, views, 2.147.483.647 stored procedures, extended stored procedures, triggers, rules, defaults, and constraints) Columns in table 1024 przygotował: [email protected] Structure of database Log Database Log Primary Filegroup File.mdf File.ndf Filegroup File.ndf File.ndf przygotował: [email protected] Division to files CREATE DATABASE baza ON PRIMARY (NAME=nazwa,FILENAME=‘nazwa_pliku’,SIZE=10MB,MAXSIZE=20MB FILEGROWTH = 20%), (NAME=nazwa,FILENAME=‘nazwa_pliku’,SIZE=10MB,MAXSIZE=20MB FILEGROWTH = 20%), FILEGROUP fg1 ( (NAME=nazwa,FILENAME=‘nazwa_pliku’,SIZE=10MB,MAXSIZE=20MB FILEGROWTH = 20%), (NAME=nazwa,FILENAME=‘nazwa_pliku’,SIZE=10MB,MAXSIZE=20MB FILEGROWTH=20%)) przygotował: [email protected] Filegroups • PRIMARY – for system objects • User-defined – users objects explicitly placed here using ON filegroup CREATE TABLE tablica (int pole1,int pole2) ON fg1; • DEFAULT – default place for objects przygotował: [email protected] Creating new filegroup ALTER DATABASE baza ADD FILEGROUP newFG GO ALTER DATABASE baza ADD FILE (NAME = ‘mojNowyPlik', FILENAME = 'c:\...\plik.ndf, SIZE = 5MB) TO FILEGROUP newFG GO przygotował: [email protected] Why more than one file? • Performance – faster access when more hd's • Easier to maintain – small files easier to handle – backup of important files only • Protection of data – read-only files • Protection of system data – separate file for system tables przygotował: [email protected] Database parameters • • • • • Auto options Cursor options Recovery options SQL options State options przygotował: [email protected] Auto options • • • • AUTO_CLOSE AUTO_CREATE_STATISTICS AUTO_UPDATE_STATISTICS AUTO_SHRINK przygotował: [email protected] Cursor options • CURSOR_CLOSE_ON_COMMIT • CURSOR_DEFAULT [LOCAL | GLOBAL] przygotował: [email protected] Recovery options • RECOVERY – FULL – BULK_LOGGED – SIMPLE • PAGE_VERIFY – CHECKSUM – TORN_PAGE_DETECTION – NONE przygotował: [email protected] Snapshot isolation (2k5) • ALLOW_SNAPSHOT_ISOLATION – ON: SNAPSHOT isolation level allowed – transaction sees data as it was when it started – OFF: SNAPSHOT isolation level switched off • READ_COMMITED_SNAPSHOT – ON: transaction on READ COMMITTED isolation level sees last committed data – no waits – OFF: transaction on READ COMMITTED isolation level locks during dirty read (like in SQL2k) przygotował: [email protected] SQL options • • • • • • • ANSI_NULL_DEFAULT ANSI_NULLS ANSI_PADDING ANSI_WARNINGS ARITHABORT – arithmetic abort NUMERIC_ROUNDABORT CONCAT_NULL_YIELDS_NULL przygotował: [email protected] QUOTED_IDENTIFIER -- doesn’t work! SET QUOTED_IDENTIFIER OFF CREATE TABLE "select" ("identity" int IDENTITY, "order" int) -- works fine! SET QUOTED_IDENTIFIER ON CREATE TABLE "select" ("identity" int IDENTITY, "order" int) SELECT "identity","order" FROM "select" ORDER BY "order" -- always works CREATE TABLE [select] ([identity] int IDENTITY, [order] int) SELECT [identity],[order] FROM [select] ORDER BY [order] -- but it’s not a good idea! przygotował: [email protected] State options • ON_LINE | OFF_LINE • READ_ONLY | READ_WRITE • SINGLE_USER | RESTRICTED_USER | MULTI_USER przygotował: [email protected] Execution condition WITH <termination> • WAIT (default) • ROLLBACK AFTER 10 [SECONDS] • ROLLBACK IMMEDIATE • NO_WAIT przygotował: [email protected] Examples ALTER DATABASE baza SET QUOTED_IDENTIFIER ON, RECOVERY FULL, MULTI_USER GO ALTER DATABASE baza SET SINGLE_USER WITH ROLLBACK AFTER 10 GO przygotował: [email protected] Transact SQL • Stored procedures EXEC sp_helpdb • Functions SELECT DATABASEPROPERTYEX(‘baza','IsOnCommitEnabled') • DBCC DBCC SQLPERF DBCC SHRINKFILE przygotował: [email protected] Data types • Numeric – – – – • • • • Integer Exact floating point Floating point Currency Date and time Text BLOB Special przygotował: [email protected] Numeric types int 4 bigint 8 smallint 2 tinyint 1 decimal(p[,s]) 2-17 numeric (p[,s]) 2-17 float(n) n(1-24) 7 digits – 4B n(25-53) 15 digits – 8B real 4 money 8 smallmoney 4 przygotował: [email protected] Date and text types datetime 8 smalldatetime 4 char(n) 0-8000 varchar(n) 0-8000 nchar(n) 0-8000 (4000 chars!) nvarchar(n) 0-8000 (4000 chars!) binary 0-8000 varbinary 0-8000 przygotował: [email protected] Other types image 0-2 GB text 0-2 GB ntext 0-2 GB uniqueidentifier 16 bit 1 timestamp 8 sql_variant up to 8000 przygotował: [email protected] User defined types • Type creation EXEC sp_addtype nazwisko, 'nvarchar(25)', NULL • Type deletion EXEC sp_droptype nazwisko przygotował: [email protected] Data row organization Header Normal fields Null Block Variable Block Variable length fields • Header – 4 bytes • Null block – 2B for columns number, Null Bitmap (1 bit for column – if its null) • Variable block – 2B for columns number, 2B for column with pointer to column position • Max row length – 8060 B przygotował: [email protected] Storing BLOBs • Special tree structures • It can be defined how big value will be directly in row. EXEC sp_tableoption 'tablica', 'text in row', '1000' • Max row size: 8060 B. Big BLOBs are stored as pointers (16B) przygotował: [email protected] IDENTITY column • One in table • Definition: IDENTITY[(seed,incr)] CREATE TABLE pracownicy ( nr_prac int IDENTITY(100,1), nazwisko varchar(30))) insert into pracownicy values (‘Kowalski’); insert into pracownicy values (‘Nowak’); insert into pracownicy values (‘Iksiński’); nr_prac ----------100 101 102 nazwisko ---------Kowalski Nowak Iksiński przygotował: [email protected] IDENTITY control • select IDENT_CURRENT('table') – globally highest value of the column • select @@identity – last value inserted in the session (NULL if nothing was inserted) • select IDENT_SEED(‘table’) • select IDENT_INCR(‘table’) • select SCOPE_IDENTITY() – last IDENTITY value inserted (to any table) przygotował: [email protected] Disabling IDENTITY CREATE TABLE pracownicy ( nrp int IDENTITY(100,1), nazw varchar(30))) insert into pracownicy(nazw) values (‘Kowalski’); SET IDENTITY_INSERT pracownicy ON insert into pracownicy(nrp,nazw) values (200,‘Nowak’); SET IDENTITY_INSERT pracownicy OFF insert into pracownicy(nazw) values (‘Iksiński’); nr_prac ----------100 200 201 nazwisko ---------Kowalski Nowak Iksiński przygotował: [email protected] Not-unique IDENTITY CREATE TABLE pracownicy ( nrp int IDENTITY(100,1), nazw varchar(30))) insert into pracownicy(nazw) values (‘Kowalski’); insert into pracownicy(nazw) values (‘Iksiński’); SET IDENTITY_INSERT pracownicy ON insert into pracownicy(nrp,nazw) values (100,‘Nowak’); SET IDENTITY_INSERT pracownicy OFF nr_prac ----------100 101 100 nazwisko ---------Kowalski Iksiński Nowak przygotował: [email protected] Uniqueidentifier type • 16B (128b) – GLOBALLY unique (GUID) CREATE TABLE pracownicy ( nrp uniqueidentifier NOT NULL DEFAULT newid(), nazw varchar(30))) insert into pracownicy(nazw) values (‘Kowalski’); insert into pracownicy(nazw) values (‘Iksiński’); insert into pracownicy(nazw) values (‘Nowak’); nr_prac -----------------------------------075860D6-FBAC-466E-A0F5-5EAF0D69543B A5B7C0BD-0F23-4F79-9758-AF35A6D20D3D FF8BEBC6-21F8-46A1-8108-8507BA9509E0 nazwisko ---------Kowalski Iksiński Nowak przygotował: [email protected] timestamp (rowversion) CREATE TABLE tt (a int, b timestamp) insert into tt(a) values (1); insert into tt(a,b) values(2,null) a ----------1 2 b -----------------0x00000000000000CC 0x00000000000000CD update tt set a=3 where a=1 a ----------3 2 b -----------------0x00000000000000CF 0x00000000000000CD przygotował: [email protected] CONSTRAINTS • • • • • • Default Check Primary Key Unique Foreign Key CREATE DEFAULT przygotował: [email protected] Default constraints • Not for IDENTITY • Works only for inserts • We may use variables or system functions: newid(), user, getdate() CREATE TABLE tablica( tekst varchar(50), wpisal varchar(30) default user, data_wpisu datetime default getdate() ) insert into tablica(tekst) values(‘Nowy tekst’) tekst wpisal data_wpisu ------------ ---------------- ----------------------Nowy tekst pawel 2004-10-10 20:52:02.513 przygotował: [email protected] Check constraint • Works for every data change CREATE TABLE pracownicy( pensja int check (pensja>0), data_ur datetime, opis varchar(50), constraint data_mniejsza check(data_ur<getdate())) insert into pracownicy values(10,’2100’,’’) INSERT statement conflicted with COLUMN CHECK constraint 'data_mniejsza'. insert into pracownicy values(-10,’1980’,’’) INSERT statement conflicted with COLUMN CHECK constraint 'CK__pracownic__pensj__7E6CC920'. przygotował: [email protected] Check constraint • May be used for more than one column CREATE TABLE projekty ( start datetime, koniec datetime, constraint koniec_po_starcie check (start<koniec)) • Logic values may be used ALTER TABLE tablica ADD CONSTRAINT dataur CHECK (dataur>'01-01-1900' AND dataur<getdate()) przygotował: [email protected] Check constraint • Usage of patterns CREATE TABLE adresy ( miasto varchar(30), kod char(6) check(kod LIKE '[0-9][0-9]-[0-9][0-9][0-9]'), ulica varchar(40) check(ulica like '[a-z]%[0-9]') ) insert insert insert insert into into into into adresy adresy adresy adresy values(’Gliwice','22-445',‘Ala 6') values(’Gliwice','22-44',‘Ala 6') values(’Gliwice','22-442',‘5Ala 6') values(’Gliwice','22-442',‘5 Ewa') przygotował: [email protected] NOCHECK • Doesn’t check constraints on already inserted columns CREATE TABLE tablica ( a INT); INSERT INTO tablica VALUES (-1); ALTER TABLE tablica ADD CONSTRAINT mycheck CHECK (a > 1); ALTER TABLE statement conflicted with COLUMN CHECK constraint 'mycheck'. ALTER TABLE tablica WITH NOCHECK ADD CONSTRAINT mycheck CHECK (a > 1); The command(s) completed successfully. przygotował: [email protected] Primary key • One in one table (may be more than one column) • Automatic index creation CREATE TABLE tablica ( a INT [[NON]CLUSTERED] PRIMARY KEY, b varchar(20)); CREATE TABLE tablica ( a INT , b varchar(20), PRIMARY KEY (a)); ALTER TABLE tablica ADD CONSTRAINT primary_key PRIMARY KEY NONCLUSTERED (a); przygotował: [email protected] Unique • • • • May have one NULL value May be more than one in table Cannot use it with PK columns Automatic index creation przygotował: [email protected] Foreign key • No SET NULL • May be for more than one column • WITH NOCHECK may be used CREATE TABLE tablica ( a INT, b varchar(20), PRIMARY KEY (a,b)); CREATE TABLE tab2( x int, y varchar(20), foreign key (x,y) references tablica(a,b) on update cascade); przygotował: [email protected] Turning off constraints When big amount of data is loaded • When we know they fulfill conditions • When they are not but it is easier to correct it in the database ALTER TABLE tablica NOCHECK CONSTRAINT mój_warunek ... Data loading ... ALTER TABLE tablica CHECK CONSTRAINT mój_warunek przygotował: [email protected] CREATE DEFAULT • One definition used in different places (also in user types) CREATE DEFAULT mojkraj AS ‘Polska’ sp_bindefault mojkraj, ‘pracownicy.kraj' sp_bindefault mojkraj, ‘studenci.kraj' sp_bindefault mojkraj, ‘adresy.kraj' • Turning off default sp_unbindefault mojkraj, ‘adresy.kraj' przygotował: [email protected] Computed columns create table prac(nrp int, adr_domowy varchar(30), adr_praca varchar(30), koresp int, adr_koresp as CASE koresp WHEN 0 THEN adr_domowy ELSE adr_praca END ) insert into prac values(1,'Chorzów','Gliwice',0) select * from prac nrp adr_domowy adr_praca koresp adr_koresp --- ------------ ----------- ------ ----------1 Chorzów Gliwice 0 Chorzów update prac set koresp=1 select * from prac nrp adr_domowy adr_praca koresp adr_koresp --- ------------ ----------- ------ ----------1 Chorzów Gliwice 1 Gliwice przygotował: [email protected] SQL and indexes • Index creation create index nazwa on tablica(kolumna,...) create unique index nazwa on tablica(kolumna,...) create clustered index nazwa on tablica(kolumna,...) create unique clustered index nazwa on tablica(kolumna,...) • Index dropping drop index tablica.nazwa przygotował: [email protected] How to use indexes • • • • • PK and UNIQUE are better First clustered Do not duplicate indexes! Indexed fields should be short Use UNIQUE – if it is possible SELECT id, COUNT(*) FROM tablica GROUP BY id HAVING COUNT(*)>1 przygotował: [email protected] Indexes pros and cons • Pros: – Faster data searching – Control of data uniqueness • Cons: – – – – Slower data insertion Slower updates of indexed columns Storage (depends on column size) Difficult database administration przygotował: [email protected] Composite indexes • Max 16 columns (900 bytes) • Order is important! create index xyz on prac(nazw,imie) create index xyz on prac(imie,nazw) • First the most unique column • Index is used only when query has first column in WHERE column! przygotował: [email protected] FILLFACTOR option • States how to fill pages • Default 100% • Decreasing increases disk storage but simplifies insert and update • Without PAD_INDEX used only for leaves create index xyz on prac(nazw,imie) with [PAD_INDEX,] FILLFACTOR=70 przygotował: [email protected] DBCC application • • • • • • • DBCC – Database Consistency Checker About 130 different operations DBCC checkdb DBCC cleantable DBCC perflog DBCC showcontig DBCCC indexdefrag przygotował: [email protected] DBCC showcontig DBCC SHOWCONTIG scanning 'tablica' table... Table: 'tablica' (2073058421); index ID: 1, database ID: 6 TABLE level scan performed. Pages Scanned: 3 Extents Scanned: 2 Extent Switches: 1 Avg. Pages per Extent: 1.5 Scan Density [Best Count:Actual Count]: 50.00% [1:2] Logical Scan Fragmentation 0.00% Extent Scan Fragmentation: 50.00% Avg. Bytes Free per Page: 246.7 Avg. Page Density (full): 96.95% DBCC execution completed. If DBCC printed error messages, contact your system administrator.