Académique Documents
Professionnel Documents
Culture Documents
Sybase Architecture
System Databases
master database model database sybsystemprocs tempdb sybsecurity sybsyntax
Master Database
User accounts (in syslogins) Remote user accounts (in sysremotelogins) Remote servers that this server can interact with (in sysservers) Ongoing processes (in sysprocesses) Configurable environment variables (in sysconfigures) System error messages (in sysmessages) Databases on SQL Server (in sysdatabases)
Master Database
Storage space to each database (sysusages) Tapes and disks mounted on the system (sysdevices) Active locks (in syslocks) Character sets (in syscharsets) and languages (in syslanguages) Users who hold server-wide roles (in sysloginroles)
Model Database
Provides a template for new databases Default is 2MB Databases cannot be smaller than model database Adding user-defined data types, rules, or defaults Adding users who should have access to all databases on SQL Server Granting default privileges, particularly for guest accounts
Tempdb database
Storage area for temporary tables and other temporary working storage needs (for example, intermediate results of group by and order by) Space shared among all users Default size is 2 MB
Tempdb contd
Default size is 2MB Restart of the server clears tempdb At Server restart model is copied on to tempdb Size can be altered by ALTER DATABASE
Sybsecurity database
Contains the audit system for SQL Server. Consists of :
sysaudits table, which contains the audit trail. All audit records are written into sysaudits sysauditoptions table, which contains rows describing the global audit options All other default system tables that are derived from model
Sybsyntax database
Contains syntax help for Transact-SQL commands System procedures SQL Server utilities eg. sp_syntax "select"
System Tables
Track Information
Server Wide Database Specific
Define Database Structure The master contains all system tables(31) User databases Contain a Subset of System Tables(17)
Database Components
Database Components System Tables
Objects table ,view,default,rule, sysobjects stored procedure, and trigger Indexes Datatype Constraint s sysindexes systypes sysconstraints sysreferences
System Procedures
An easy way to query system tables System Procedure is a precompiled collection of SQL statements Are located in sybsystemprocs but can be executed from any database
System Procedures
Allocating Space
Allocating Storage
Device Database Allocation Unit Extent Page
Devices
Devices are hard disk files that store databases, transaction logs, and backups One device can hold many databases and one database can span multiple devices Only SA can create devices
Creating a Device
DISK INIT name=logical_name, physname = physical name, vdevno=virtual_dev_no, size = num_of_2K_blocks [, VSTART= virtual_address]
DISK INIT
DISK INIT NAME = hcl_dev1, PHYSNAME=C:\HCL\DATA\hcl_dat, VDEVNO=3, SIZE = 8192 DISK INIT NAME = hcl_dev2, PHYSNAME=C:\HCL\DATA\hcl_log, VDEVNO=4, SIZE = 1024
DISK INIT
Maps the specified physical disk operating system file to a database device name Lists the new device in master..sysdevices Prepares the device for database storage
vdevno
Used to map sysdevices, sysusages and sydatabases Must be less than device parameter in master Total no. of devices available is 255
Devices
Configured Value Sp_configure Number of devices To see values of vdevno already in use select distinct low/16777216 from sysdevices order by low
Info on devices
Sp_helpdevice devicename eg sp_helpdevice master Which will be the table used for storing devices?
Managing Devices
Setting up default device sp_diskdefault database_device, {default_on | default_off} Dropping a device sp_dropdevice logical_name
Managing Devices
sp_diskdefault master, defaultoff Sp_diskdefault def_1, defaulton Sp_dropdevice tapedump1
Default devices
Devices not to be used as default devices
Master Device used for sybsecurity Devices used for transaction logs
Dropping of devices
Device in use cannot be dropped Server has to be restarted after dropping a device Corresponding file has to be dropped at OS level
Create Database
Verifies that the database name specified in the statement is unique. Makes sure that the database device names specified in the statement are available. Finds an unused identification number for the new database. Assigns space to the database on the specified database devices and updates sysusages to reflect these assignments. Inserts a row into sysdatabases. Makes a copy of the model database in the new database space, thereby creating the new database's system tables.
Create database
Create database newdb on alpha_disk = 10, beta_disk = 10, delta_disk =10, gamma_disk = 50
Create Database
create database newpubs on default = 4 Multiple default devices can be used Eg create newdb on default = 100 could use more than on device
Transaction logs
Every database has a write ahead log First the transaction is written to the log It is the system table syslogs Essential to have a log
Sp_dboption
Sets options for databases Displays a complete list of the database options when it is used without a parameter Changes a database option when used with parameters Options can be changed only for user databases
Database Options
Sp_helpdb in a database shows the options set for that database Only SA or dbo can change the options None of the master database options can be changed
Database Options
To use sp_dboption to change the pubs2 database to read only: use master sp_dboption pubs2, "read only", true Use pubs2 checkpoint
Tables
Entity represented as a table 2 billion tables per database 250 columns per database Column names have to be unique in a table
Create table
create table titles (title_id tid, title varchar(80) not null, type char(12), pub_id char(4) null, price money null, advance money null, royalty int null, total_sales int null, pubdate datetime)
Indexes
Indexes
Enforce uniqueness Speed up joins Speeds data retreival Speeds ORDER BY and GROUP BY
Indexing
Columns to consider for indexing
Primary Key Columns frequently used in joins Columns frequently searched in ranges Columns retrieved in sorted order
Indexing (contd)
Columns that should not be indexed
Columns seldom referenced in query Columns that contain few unique values Columns defined with text, image, or bit datatypes When Update performance has a higher priority than SELECT performance
Creating An Index
create [unique] [clustered | nonclustered] index index_name on [[database.]owner.]table_name ( column_name [, column_name]...) [with {{fillfactor |
Clustered Indexes
Physical order = Indexed order Leaf level = actual data pages of a table Only one clustered index per table Requires 1.21*table size space for creation Should be created on PK or column(s) searched for range of values
Nonclustered Indexes
Physical order is not the same as index order The leaf level contains pointers to the rows on the data pages Pointers add a level between index and data 249 nonclustered indexes per table can be created
Fill factor
Low fillfactor means free space on indexes Not maintained by Sybase Has to be maintained by dropping and recreating index Fillfactor of 0 means data and leaf pages are completely filled and nonleaf pages to 75%
Segments
Subsets of database devices Can be used in Create table and Create Index commands Every database can have upto 32 segments
Creating Segments
Initialize the physical device with disk init Make the database device available to the database by using the on clause to create database or alter database sp_addsegment segname, dbname, devname
Example
This statement creates the segment seg_mydisk1 on the database device mydisk1: sp_addsegment seg_mydisk1, mydata, mydisk1
Dropping a segment
sp_dropsegment segname, dbname drops segment from the specified database
Clustered Indexes
Table and Clustered Index on the same segment If you have placed a table on a segment, and you need to create a clustered index, be sure to use the on segment_name clause, or the table will migrate to the default segment.
Object Placement
Log on separate device Spread large , heavily tables across devices Tables and non-clustered indexes on separate devices Tempdb on separate device
Index Requirements
Only one clustered index per table, since the data for a clustered index is ordered by index key You can create a maximum of 249 nonclustered indexes per table A key can be made up of as many as 31 columns. The maximum number of bytes per index key is 600
Choosing Indexes
What indexes are associated currently with a given table? What are the most important processes that make use of the table? What is the ratio of select operations to data modifications performed on the table? Has a clustered index been created for the table? Can the clustered index be replaced by a nonclustered index? Do any of the indexes cover one or more of the critical queries? Is a composite index required to enforce the uniqueness of a compound primary key? What indexes can be defined as unique? What are the major sorting requirements? Do some queries use descending ordering of result sets? Do the indexes support joins and referential integrity checks?
Clustered Indexes
Clustered Indexes provide very good performance for range queries In high transaction environment do not create clustered index on a steadily increasing value such as IDENTITY column
Index Covering
Mechanism for using the leaf level of a nonclustered index the way data page of clustered index would work Index covering occurs when all columns referenced in the query are contained in the index itself Leaf level of the index has all the required data
Index Covering
As the leaf index rows are much smaller than data rows, a nonclustered index that covers a query is faster then clustered index eg. An index on royalty and price will cover the following query Select royalty from titles where price between $10 and $20
Index Covering
Provides performance benefits for queries containing aggregates Typically first column of the index has to be used in where clause for the index to be used but aggregates dont have to satisfy this condition
Index Covering
Select avg(price) from titles can use index on price and scan all leaf pages Select count(*) from titles where price > $7.95 will use index on price to find the firdt leaf row where price > 7.95 and then just scan to the end of the index counting the number of leaf rows
Index Covering
Select count(*) from titles also is satisfied by nonclustered index as the number of rows in any index will be the same as the total no. of rows in the table Sybase Optimiser uses the nonclustered index with the smallest row size
Locate Procedure
Stored Procedures
Main performance gain is the capability of Sybase server to save the optimised query plan generated by the first execution of the stored procedure in procedure cache and to reuse it for further execution
Execution of SP
FIRST Execution Locate SP on disk and load into cache Substitute parameter values Develop optimisation plan Compile optimisation plan Execute from cache
Execution of SP
Subsequent Executions Locate SP in cache Substitute parameter values Execute from cache
Stored Procedures
Advantaqge of cost based optimizer is that it has the capability of generating the optimal query plan for all plans based on search criteria For certain type of queries (eg range queries) optimiser may at times generate different plans
Stored Procedures
In situations where parameter values can be different for every execution, use CREATE PROCEDURE WITH RECOMPILE option In case a particular execution has to use a different plan use EXECUTE with RECOMPILE
Stored Procedures
If an index used by a stored procedure is dropped, Sybase detects it and recompiles the procedure Adding additional indexes or running UPDATE STATISTICS does not cause automatic recompilation
Stored Procedures
Statistics Updation has to be followed by sp_recompile <table name>to generate a new query plan Addition of an index has also to be followed by sp_recompile
Stored Procedures
Create proc get_order_data (@flag tinyint, @value smallint) as If @flag=1 Select * from orders where price=@value Else Select * from orders where qty=@value should be converted to ..
Stored Procedures
Create proc get_orders_by_price (@price smallint) as select * from orders where price =@value Create proc get_orders_by_qty (@qty smallint) as select * from orders where qty =@value
Stored Procedures
A separate procedure to call the appropriate procedure depending on the value of flag create proc get_order_data(@flag tinyint, @value smallint) as if @flag=1 exec get_orders_by_price else exec get_orders_by_qty
Triggers
274
Why Triggers
Cascading Actions are not available with DRI i.e. when Primary Key is Updated or Deleted, Corresponding Foreign Keys do not get automatically changed Maintaining duplicate data Keeping derived columns current
275
281
INSERT Trigger
Trigger Table
Inserted
282
INSERT Trigger
eg. CREATE TRIGGER loan_ins ON loan for insert AS UPDATE copy SET ON_LOAN=y FROM COPY,inserted WHERE copy.isbn=inserted.isbn AND copy.cop_no=inserted.copy_no
284
DELETE Trigger
Trigger Table
Deleted
285
UPDATE Trigger
Updated Table
Table
Inserted
Deleted
287
UPDATE Trigger
CREATE TRIGGER mem_upd ON member FOR UPDATE AS IF UPDATE(MEMBER_NO) BEGIN RAISERROR (Trnxn cannot be processed. \ **** Member cannot be updated.,10,1) ROLLBACK TRANSACTION END
288
Trigger Considerations
Overhead is very low Inserted and Deleted tables are in memory Location of other tables referenced by the trigger determines the amount of time required INSERT,DELETE or UPDATE in the trigger is a part of the transaction Nested triggers are set to true by default Self recursion of triggers does not happen unless set
293
Cursors
Benefits of Cursors
Allow a program to take action on each row of a query result set rather than on the entire set of rows Provide the ability to delete or update a row in a table based on cursor position
205
Cursors
A cursor consists of the following parts Cursor result set : set of rows resulting from execution of the associated select statement Cursor position : a pointer to one row within the cursor result set
206
Cursor scope
Session Starts when a client logs into SQL Server and ends on log out SP Starts when SP begins execution and ends when SP completes execution Trigger Starts when trigger begins execution and ends when it completes execution
207
Cursors
Declare declare the cursor for select statement. Checks SQL syntax Open Executes the query and creates the result set . Positions the cursor before the first row of the result set Fetch fetches the row to which cursor points Close Closes the result set, but the compiled query plan remains in memory Deallocate drops the query plan from memory
208
Updatable Cursors
Update mode uses update page locks. It is in effect if:
You specify for update. The select statement does not include distinct, group by, union, a subquery, aggregate functions,
Index requirements
For read only cursors any index can be used Same query result will be obtained as will be obtained for a select statement
Index Requirements
Updatable cursors require unique indexes on the tables whose columns are being updated
Query Optimisation
Optimisation Step
Phase 1 Query Analysis
Find the SARG Find the ORs Find the joins
SARG
SARG - Enables the optimiser to limit the rows searched to satisfy a query SARG is matched with an index SARG is a where clause comparing a column to a constant Col operator const_expr Valid operators are =,>,<,>=,<= != or <> cannot be used to match a value against an index unless query is covered
Search arguments
Valid SARGs flag = 7 salary > 10000 city = Pune and state = MH Invalid SARGs lname=fname ytd/months > 1000 ytd/12 = 1000 flag !=0
OR clauses
Format of OR clause is SARG or SARG [or ] with all columns involved in the OR belonging to the same table Column IN (const 1 ,const 2 , also is treated as OR Examples of OR where au_lname = X OR au_fname=Y Where (type=ty and price > $25) OR pub_id = 1 Where au_lname in (A,B,C)
The OR strategy
OR clause handled either by table scan or by using OR strategy OR strategy used only when indexes occur on both columns OR strategy breaks query into two parts. Each query is executed and row ids are kept into a work table. Duplicates are removed and qualifying rows are retrieved from the work table OR strategy used only if total I/O is less than the I/Os required for table scan
Join Clause
A join clause is a where clause in the format Table1.Column Operator Table2.Column
Index Selection
All SARGs, OR clauses and join clauses are matched with available indexes and I/O costs are estimated Index I/O costs are compared with each other and against the cost of table scan to determine the least expensive access path If no useful index is found, a table scan must be performed
Joins
The process of creating the result set for a join is to nest the tables, and to scan the inner tables repeatedly for each qualifying row in the outer table.
Update Operation
Direct Updates Deferred Updates Direct updates are faster and are performed whenever possible
Direct Updates
Sybase performs direct updates in a single pass, as follows: Locates the affected index and data rows Writes the log records for the changes to the transaction log Makes the changes to the data pages and any affected index pages
Deferred Updates
The steps involved in deferred updates are: Locate the affected data rows, writing the log records for deferred delete and insert of the data pages as rows are located Read the log records for the transaction. Perform the deletes on the data pages and delete any affected index rows At the end of the operation, re-read the log, and make all inserts on the data pages and insert any affected index rows
contd ..
Count vs Exists
Do not use the count aggregate in a subquery to do an existence check: select * from tab where 0 < (select count(*) from tab2 where ...) Instead, use exists (or in): select * from tab where exists (select * from tab2 where ...)
Count vs Exists
When you use count, SQL Server does not know that you are doing an existence check. It counts all matching values, When you use exists, SQL Server knows you are doing an existence check. When it finds the first matching value, it returns TRUE and stops looking. The same applies to using count instead of in or any.
Aggregates
SQL Server uses special optimizations for the max and min aggregates when there is an index on the aggregated column For min, it reads the first value on the root page of the index For max, it goes directly to the end of the index to find the last row
Aggregates
min and max optimizations are not applied if: The expression inside the max or min is anything but a column. Compare max(numeric_col*2) and max(numeric_col )*2, where numeric_col has a nonclustered index. The second uses max optimization; the first performs a scan of the nonclustered index. The column inside the max or min is not the first column of an index. For nonclustered indexes, it can perform a scan on the leaf level of the index; for clustered indexes, it must perform the table scan. There is another aggregate in the query There is a group by clause
Aggregates
Do not write two aggregates together eg. select max(price), min(price) from titles results in a full scan of titles, even if there is an index on price Rewriting the query as: select max(price) from titles select min(price) from titles uses index for both the queries
If a join between different datatypes is unavoidable, and it hurts performance, you can force the conversion to the other side of the join