Dbspace management example

This section illustrates the dbspace management process from creating a new database and adding objects and data to the database, through relocating objects and dropping the empty dbspace. This example includes sample SQL code and the output of the related system stored procedures.

Creating the database objects

Create a small database dbspacedb using the following CREATE DATABASE statement:

CREATE DATABASE 'D:\IQ\dbspacedb'
     IQ PATH 'D:\IQ\dbspacedb.iq'
     IQ SIZE 10
     IQ RESERVE 100
     TEMPORARY SIZE 10
     TEMPORARY RESERVE 10
     JAVA OFF
     JCONNECT OFF;

Connect to the dbspacedb database:

CONNECT DATABASE dbspacedb
     user DBA identified by SQL;

Add two dbspaces to the dbspacedb database:

CREATE DBSPACE dbspacedb2 as
     'D:\IQ\dbspacedb.iq2'
     SIZE 10 RESERVE 20;CREATE DBSPACE dbspacedb3 as
     'D:\IQ\dbspacedb.iq3'
     SIZE 10 RESERVE 40;

Create two tables in the dbspacedb database, create indexes, and add some data:

CREATE TABLE t1(c1 int);
CREATE TABLE t2(c1 int);
CREATE hg INDEX t1c1hg ON t1(c1);
CREATE hng INDEX t2c1hng ON t2(c1);
INSERT t1 VALUES(1);
INSERT t2 VALUES(2);
COMMIT;

Displaying information about dbspaces

Use the sp_iqdbspace system stored procedure to display information about all dbspaces in the dbspacedb database:

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ__ SYSTEM_MAIN

D:\IQ\ dbspacedb.iq

MAIN

RW

   24

10M

100M

8K

1H,64F,32D,62A,20X,128M

            1

      1280

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RW

    9

10M

20M

8K

1H,32F,56A, 19X

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

   12

10M

40M

8K

1H,32F,59A, 49X

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

    8

10M

10M

8K

1H,64F,12A, 20X

            1

      1280

Use the sp_iqdbspaceinfo system stored procedure to display information about object placement and space usage for a specific dbspace:

sp_iqdbspaceinfo IQ_SYSTEM_MAIN;

dbspace_name

Object

MinBlk

MaxBlk

ObjSize

DBSpSz

IQ_SYSTEM_MAIN

t1

 82

125

40K

10M

IQ_SYSTEM_MAIN

t1.DBA.ASIQ_IDX_T429_C1_FP

109

322

136K

10M

IQ_SYSTEM_MAIN

t1.DBA.t1c1hg

127

305

152K

10M

IQ_SYSTEM_MAIN

t2

 84

107

32K

10M

IQ_SYSTEM_MAIN

t2.DBA.ASIQ_IDX_T430_C1_FP

126

321

136K

10M

Use the sp_iqindexinfo system stored procedure to display object placement and space usage for a specific table or index:

sp_iqindexinfo 'table t2';

Object

dbspace_name

ObjSize

DBSpPct

MinBlk

MaxBlk

t2

IQ_SYSTEM_MAIN

32K

               1

         84

        107

t2

dbspacedb2

160K

               2

1045495

1045556

t2

dbspacedb3

8K

               1

2090930

2090930

t2.DBA.ASIQ_IDX_T430_C1_FP

IQ_SYSTEM_MAIN

136K

               2

        126

        321

t2.DBA.ASIQ_IDX_T430_C1_FP

dbspacedb3

152K

               2

2091032

2091053

t2.DBA.t2c1hng

dbspacedb2

136K

               2

1045537

1045553

For the full syntax of the sp_iqdbspace, sp_iqdbspaceinfo, and sp_iqindexinfo system stored procedures, see Chapter 10, “System Procedures” in the Sybase IQ Reference Manual.

Changing the size of a dbspace

The ALTER DBSPACE commands in this section show you how to change the dbspace size, if necessary.

The database dbspacedb has a reserve size of 100MB for the IQ Main Store, which was set using the IQ RESERVE parameter of the CREATE DATABASE statement. This IQ Main Store (the IQ_SYSTEM_MAIN dbspace) can be extended by 100MB. The original IQ_SYSTEM_MAIN is created with a size of 10 MB (the IQ SIZE parameter of CREATE DATABASE). The following ALTER DBSPACE command with the ADD parameter extends the IQ_SYSTEM_MAIN dbspace by 10MB to 20MB:

ALTER DBSPACE IQ_SYSTEM_MAIN ADD 10mb;

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ__ SYSTEM_MAIN

D:\IQ\ dbspacedb.iq

MAIN

RW

   12

20M

90M

8K

1H,64F, 32D,62A,128M

            1

      2560

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RW

    7

10M

20M

8K

1H,32F, 56A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

    8

10M

40M

8K

1H,32F, 59A

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

    8

10M

10M

8K

1H,64F, 32A

            1

      1280

Note that if the dbspacedb database is not created with an IQ RESERVE value, the dbspace cannot be extended. The dbspace can be made smaller, however, and the size taken away from the dbspace is added to the reserve.

The IQ_SYSTEM_MAIN dbspace is now 20MB in size. This dbspace can be resized to 15MB using the ALTER DBSPACE command with the SIZE parameter:

ALTER DBSPACE IQ_SYSTEM_MAIN SIZE 15mb;

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ__ SYSTEM_MAIN

D:\IQ\ dbspacedb.iq

MAIN

RW

   15

15M

95M

8K

1H,64F, 32D,62A,128M

            1

      1920

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RW

    7

10M

20M

8K

1H,32F, 56A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

    8

10M

40M

8K

1H,32F, 59A

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

    8

10M

10M

8K

1H,64F, 32A

            1

      1280

Note that the dbspace can be decreased in size only if the truncated portion is not in use. Use sp_iqdbspaceinfo to determine which blocks are in use by the objects on a dbspace.

For a description of the full syntax and actions of the ALTER DBSPACE command, see Chapter 6, “SQL Statements” in the Sybase IQ Reference Manual.

Changing the mode of a dbspace

The read-write mode of a dbspace controls whether writes to the dbspace are performed and if data should be relocated from the dbspace. A dbspace has one of three read-write modes, which is changed using the ALTER DBSPACE command:

The system stored procedure sp_iqdbspace lists the read-write mode of one or all dbspaces in the RWMode column.

Set the read-write mode of a dbspace to read-only (RO) to prevent further writes to the dbspace. The following ALTER DBSPACE statement changes the read-write mode of the dbspace dbspacedb2 to read-only. The INSERT statement allocates storage for the new version from the remaining read-write (RW) dbspaces.

ALTER DBSPACE dbspacedb2 READONLY;
INSERT t1 VALUES(2);
sp_iqdbspace;

In the sp_iqdbspace output, note that the dbspace dbspacedb2 now has a read-write mode of read-only (RO in the RWMode column).

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ__ SYSTEM_MAIN

D:\IQ\ dbspacedb.iq

MAIN

RW

   20

15M

95M

8K

1H,64F, 33R,32D,78A,37O,128M

           1

      1920

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

    5

10M

20M

8K

1H,32F, 37A,19O

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

   11

10M

40M

8K

1H,32F, 62A,38O

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

    9

10M

10M

8K

1H,64F, 11A,37O

           1

      1280

Relocating objects and data

Tables and user data in a dbspace must be dropped or relocated to other dbspaces before the dbspace can be dropped. The two step relocation process in this example first uses the ALTER DBSPACE command to change the mode of the IQ_SYSTEM_MAIN dbspace to relocate mode, so that no further allocations are made from the dbspace and objects can be relocated:

ALTER DBSPACE IQ_SYSTEM_MAIN relocate;

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ__ SYSTEM_MAIN

D:\IQ\ dbspacedb.iq

MAIN

RR

    4

15M

95M

8K

1H,64F, 62A

            1

      1920

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

    5

10M

20M

8K

1H,32F, 56A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

   25

10M

40M

8K

1H,64F 33R,32D,59A, 128M

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

    8

10M

10M

8K

1H,64F, 32A

            1

      1280

Note that the read-write mode of the IQ_SYSTEM_MAIN dbspace is now RR for relocate. Setting the dbspace read-write mode to relocate also has the effect that no future writes to the dbspace will be performed.

The second step in the process is relocating objects and data in the relocate dbspace. Individual objects in the dbspace are relocated using the sp_iqrelocate system stored procedure. For example, the following command relocates the index t1c1hg on table t1 and relocates the entire table t2:

sp_iqrelocate 'index t1c1hg table t2';

ObjectName

NRelocated

RelocStatus

t1.DBA.t1c1hg

                 19

relocated

t2

                   4

relocated

t2.DBA.ASIQ_IDX_T430_C1_FP

                 17

relocated

t2.DBA.t2c1hng

                   0

no relocs

The sp_iqrelocate stored procedure works much like the UPDATE statement. Queries can continue to access tables while sp_iqrelocate is running. Data manipulation (DML) and data definition (DDL) commands may return errors, since the tables are locked for update. Likewise, sp_iqrelocate may return an error if another connection is updating a table. To avoid these write access conflict errors, you can change object-level permission to ensure that there is no write activity on them or you can limit connections with single-user mode.

All data on dbspaces with the read-write mode of relocate can be relocated using a single sp_iqrelocate command. The following command relocates all data on relocate dbspaces in the empdb database, which in this case is all data in the IQ_SYSTEM_MAIN dbspace:

sp_iqrelocate 'empdb';

ObjectName

NRelocated

RelocStatus

t1

                   5

relocated

t1.DBA.ASIQ_IDX_T429_C1_FP

                 17

relocated

t1.DBA.t1c1hg

                   0

no relocs

t2

                   0

no relocs

t2.DBA.ASIQ_IDX_T430_C1_FP

                   0

no relocs

t2.DBA.t2c1hng

                   0

no relocs

Note that the four objects with relocation status of no relocs were relocated by the earlier sp_iqrelocate command.

The column NRelocated in the sp_iqrelocate output is the number of blocks that were relocated for each object. In addition to relocated (all blocks in relocate dbspaces were relocated), other possible values of the RelocStatus column are partial (the number of blocks in relocate dbspaces exceeds the sp_iqrelocate maxsize parameter) and no relocs (the object has no blocks in relocate dbspaces).

sp_iqrelocate does not automatically commit. You must commit the changes before they are persistent.

For a full description of the syntax, actions, and output of sp_iqrelocate, see Chapter 10, “System Procedures” in the Sybase IQ Reference Manual.

Dropping a main dbspace

After all objects and user data are relocated to other dbspaces, the dbspace is ready to drop using the DROP DBSPACE command. Before executing the DROP DBSPACE command, check the block type information in the sp_iqdbspace output to be sure the dbspace does not have blocks of type ‘A’ or ‘O’ (blocks with data from active table versions or old versions that may still be in use).

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ__ SYSTEM_MAIN

D:\IQ\ dbspacedb.iq

MAIN

RR

    0

15M

95M

8K

1H,64F

            1

      1920

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

    2

10M

20M

8K

1H,32F, 21A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

   34

10M

40M

8K

1H,64F, 33R,32D,156A,18X,128M

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

    8

10M

10M

8K

1H,64F, 32A

            1

      1280

The following command drops the dbspace IQ_SYSTEM_MAIN, from which all data was relocated:

DROP DBSPACE IQ_SYSTEM_MAIN;

The sp_iqdbspace output shows that the dbspace IQ_SYSTEM_MAIN has been dropped:

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

      2

10M

20M

8K

1H,32F, 21A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

   33

10M

40M

8K

1H,64F, 33R,32D,156A, 128M

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

     8

10M

10M

8K

1H,64F, 32A

            1

     1280

Reusing space

The blocks formerly used by the IQ_SYSTEM_MAIN dbspace can now be used by a new dbspace, if the new dbspace is not larger than the old dbspace that was dropped. The following CREATE DBSPACE command creates the new 10MB dbspace dbspacedb1, which is the same size as the original IQ_SYSTEM_MAIN dbspace:

create dbspace dbspacedb1 as
     'D:\IQ\dbspacedb.iq1'
     size 10 reserve 20;

Note in the sp_iqdbspace output that the new dbspace dbspacedb1 uses the same blocks that were made available by dropping IQ_SYSTEM_MAIN (see the columns FirstBlk and LastBlk).

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

dbspacedb1

D:\IQ\ dbspacedb.iq1

MAIN

RW

      4

10M

20M

8K

1H,32F, 17R

            1

      1280

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

      2

10M

20M

8K

1H,32F ,21A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

    32

10M

40M

8K

1H,64F, 16R,32D,156A, 128M

2090880

2092159

IQ_ SYSTEM_TEMP

dbspacedb. iqtmp

TEMPORARY

RW

     8

10M

10M

8K

1H,64F, 32A

            1

     1280

Dropping a temporary dbspace

To drop a temporary dbspace, use the same commands and stored procedures (except sp_iqrelocate) used to drop a main dbspace. This example drops the temporary dbspace IQ_SYSTEM_TEMP. First, create a second temporary dbspace dbspacedbtmp2 to replace the dbspace that you are dropping.

create dbspace dbspacedbtmp2 as
     'D:\IQ\dbspacedb.iqtmp2'
     iq temporary store size 20;

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

dbspacedb1

D:\IQ\ dbspacedb.iq1

MAIN

RW

        6

10M

20M

8K

1H,32F,33R

             1

      1280

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

        2

10M

20M

8K

1H,32F,21A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

      30

10M

40M

8K

1H,64F, 32D,156A, 128M

2090880

2092159

IQ_SYSTEM_TEMP

dbspacedb.iqtmp

TEMPORARY

RW

        8

10M

10M

8K

1H,64F,32A

             1

     1280

dbspacedbtmp2

D:\IQ\ dbspacedb.iqtmp2

TEMPORARY

RW

        2

20M

0B

8K

1H,32F

1045440

1047999

Use the ALTER DBSPACE command to change the read-write mode of the IQ_SYSTEM_TEMP dbspace to relocate (RR), so that no further allocations are made from the dbspace and objects can be relocated:

alter dbspace IQ_SYSTEM_TEMP relocate;
sp_iqdbspace IQ_SYSTEM_TEMP;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ_SYSTEM_TEMP

dbspacedb.iqtmp

TEMPORARY

RR

        7

10M

10M

8K

1H,64F,16A

             1

     1280

Temp space may be in use by other connections. Run the sp_iqconnection stored procedure to find temporary space that is in use. The columns TempTableSpaceKB and TempWorkSpaceKB indicate that temp space is being used by that connection. You may need to rollback or disconnect these connections to release this space.

sp_iqconnection; 

ConnHandle

Name

Userid

...

TempTableSpaceKB

TempWorkSpaceKB

...

                   2

DBA

...

                                   0

                               128

...

You may also need to issue a CHECKPOINT to release remaining Temp Store blocks:

checkpoint;

Run the sp_iqdbspace stored procedure to see if the IQ_SYSTEM_TEMP dbspace is empty. The temporary dbspace can be dropped if it contains only block types ‘F’ and ‘H’ (free list blocks and header blocks of the free list).

sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

dbspacedb1

D:\IQ\ dbspacedb.iq1

MAIN

RW

        6

10M

20M

8K

1H,32F, 33R

             1

     1280

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

        2

10M

20M

8K

1H,32F, 21A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

      30

10M

40M

8K

1H,64F, 32D,156A,128M

2090880

2092159

IQ_SYSTEM_TEMP

dbspacedb.iqtmp

TEMPORARY

RR

        6

10M

10M

8K

1H,64F

             1

      1280

dbspacedbtmp2

D:\IQ\ dbspacedb.iqtmp2

TEMPORARY

RW

        4

20M

0B

8K

1H,64F, 16A

1045440

1047999

You can also run sp_iqdbspace with the dbspace name to see if the IQ_SYSTEM_TEMP dbspace is empty:

sp_iqdbspace IQ_SYSTEM_TEMP;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

IQ_SYSTEM_TEMP

dbspacedb.iqtmp

TEMPORARY

RR

        6

10M

10M

8K

1H,64F

             1

      1280

When the original temporary dbspace is empty, run DROP DBSPACE to drop IQ_SYSTEM_TEMP:

drop dbspace IQ_SYSTEM_TEMP;
sp_iqdbspace;

Name

Path

Segment Type

RW Mode

Usage

DBS Size

Reserve

Stripe Size

Blk Types

First Blk

Last Blk

dbspacedb1

D:\IQ\ dbspacedb.iq1

MAIN

RW

        6

10M

20M

8K

1H,32F,33R

             1

     1280

dbspacedb2

D:\IQ\ dbspacedb.iq2

MAIN

RO

        2

10M

20M

8K

1H,32F,21A

1045440

1046719

dbspacedb3

D:\IQ\ dbspacedb.iq3

MAIN

RW

      30

10M

40M

8K

1H,64F,32D, 156A,128M

2090880

2092159

dbspacedbtmp2

D:\IQ\ dbspacedb.iqtmp2

TEMPORARY

RW

        4

20M

0B

8K

1H,64F,16A

1045440

1047999

The original temporary dbspace IQ_SYSTEM_TEMP is gone and the new temporary dbspace dbspacedbtmp2 is now used as the temporary dbspace for the database dbspacedb.