Segment Remapping With Load Database When Moving A Database
Segment Remapping With Load Database When Moving A Database
id=1324
SEGMENT
REMAPPING WITH
LOAD DATABASE
WHEN MOVING A
DATABASE
To safely load an old database onto a new
database, create the target database exactly the
same as the source.
Contents
To move a database to new devices on your system or to rebuild a database on the old devices, you
have to first create the new database, then load a dump of the old database onto it. SQL Server
preserves the logical structure of the source database when it is loaded onto the target database.
From System 10 on, SQL Server also preserves the segment mapping.
In order to successfully load the dump, you have to create the target database to be identical to the
source, with devices of the same size and in the same order. If you can't or don't create the
database just like the source, you may get unexpected results when you the load the database.
This TechNote discusses potential load database problems. It contains the following sections:
Terminology
Explanation of Results
Summary
References
Terminology
This TechNote uses the following terminology:
Source database the original database.
Target database the new database onto which a dump of the source database is loaded.
Fragment a disk piece containing part or all of a database. It is represented by a row in the
sysusages table in the master database. The create database and alter database commands
create new rows in sysusages. For example, this command:
segmap
vstart
, lstart,
segmap
size
vstart
----
------
--
lstart
-----
---
--------
3
1024
33554432
4
1024
5
2048
67108864
16
2048
1024
50331648
8
2048
4096
83886080
(4 rows affected)
The columns in this output are:
dbid the database ID number.
segmap a bitmapped representation of the values of all segments associated with a disk fragment.
Figure 1 shows how the segmap value in sysusages is determined:
In Figure 1, a fragment contains both the system and default segments. Therefore the bits in the first
and second positions of the bitmap are turned on, for a value of 3.
lstart The logical page number on which a database fragment begins. Logical pages are used by the
database. The first fragment of a database has an lstart value of 0.
size The fragment size is in 2K pages. To get the number of megabytes, divide the size by 512.
vstart The virtual page number on which a fragment begins. Virtual pages are used by the database
device. The vstart values for a database on several devices have no relationship to each other; they
describe the location of the fragment on the device as an offset in 2K pages.
Note
For more information about segments and placing databases on devices, see the SQL Server System
Administration Guide.
Note
See the SQL Server Error Messages manual for more information about Error 2558.
Creating the database with fragments of different sizes than the source database
Creating the fragments in a different order than the source if the fragments are of varying
sizes
Example
The following example demonstrates how loading a dump onto a database created with differently
sized fragments or fragments in a different order than the source database can increase the number
of fragments and place data and log on the same device.
Creating source_db
The source database, source_db, was created as follows:
2> go
1> sp_addsegment seg2, source_db, dev4
2> go
Finally, the system and default segments were dropped from dev3 and dev4:
segmap
, lstart,
vstart
segmap
size
----
vstart
------
--
lstart
-----
--------
3
1024
33554432
4
1024
5
2048
67108864
16
2048
1024
50331648
8
2048
83886080
4096
---
(4 rows affected)
The segmap column shows the two user-defined segments, seg1 and seg2, as values 8 and 16
respectively.
The database fragments now look like this:
Creating target_db
target_db was created differently than source_db. The database was created in a single command
on all its devices at once instead of being altered onto them later. This placed the log segment last
and resulted in a different order of fragment sizes:
2> go
1> sp_addsegment seg2, target_db, dev7
2> go
The system and default segments were dropped from dev6 and dev7:
segmap
, lstart,
vstart
segmap
lstart
size
vstart
----
-------
--
-----
-----
-------6
1024
1024
2048
16
3072
2048
5120
1024
100663296
6
117440512
6
134217728
6
150994944
(4 rows affected)
Figure 5 shows target_db's fragments as they were created:
SQL Server overwrites the segments originally added to target_db with the segment map of
source_db.
Because more than one segment is now assigned to the same fragment, SQL Server divides
the fragment into smaller fragments, each with its own segment.
segmap
, lstart,
vstart
segmap
lstart
size
------
-----
1024
1024
1024
2048
1024
3072
1024
vstart
----
----
-----6
100663296
6
117440512
6
117441536
6
134217728
16
4096
1024
16
5120
1024
134218752
6
150994944
Loading the dump of source_db onto target_db caused the following unexpected results:
Log and data are on the same physical device, although not mixed on the same fragment.
Explanation of Results
The results in the example are due to the way SQL Server handles loads and how it resolves
differences in the way databases are created. When you move a database to a new set of devices or
re-create it on the same devices, the physical and logical storage is determined as follows:
The target database determines the physical storage, that is, how much space is allocated
on which database devices.
The source database determines logical storage, that is, the logical page order and segmap
values of the fragments. This occurs whether or not you have defined segments on the new
fragments with sp_addsegment and sp_extendsegment.
Resolving Differences
When a database dump is loaded onto a database that was created differently, SQL Server resolves
the differences as follows:
The load process overwrites any segment mapping you may have created on the target
database.
If data which was on different fragments with different segmaps on separate devices is now
placed on the same device, SQL Server will create separate fragments on the device for
each fragment. In other words, it will add rows to sysusages with different segmap values.
If data which was the same segment on separate devices is placed on contiguous fragments
on the same device, SQL Server will merge the fragments. In other words, it will delete the
separate rows in sysusages and insert a single row with a larger size.
If you create a database that is too large to fit on the device, SQL Server uses all the space
available, so you get a database whose size also includes 1/2MB.
If you have such a database, you may not experience any problems until you try to move or rebuild
it. The create and alter database commands allow you to enter only whole megabytes to specify
database size. Therefore you cannot create a new database exactly the size of the old one. If you
load the old database onto the new, SQL Server has to remap the fragments, creating 1/2MB
fragments.
Similarly, if you create the target database on a device whose size includes 1/2MB, and try to load a
dump of a database whose size is in whole megabytes, you get 1/2MB fragments.
Summary
The unintended results of loading a source database onto a target database that was created
differently include:
If you have 1/2MB fragments, a database which you cannot re-create in exactly the same
way
sysdevices
sysdatabases
sysusages
syssegments
To see what segments are on the database, select from the syssegments table:
2.
3.
2> go
4.
5.
2> go
6.
segment
name
7.
------
-------------
8.
system
9.
default
10. 2
logsegment
11. 3
seg1
12. 4
seg2
13.
14. (5 rows affected)
15. Determine your database's ID number:
16. 1> use master
17. 2> go
18. 1> select name, dbid from sysdatabases
19. 2> where name = source_db
20. 3> go
21.
name
dbid
22. ---------------
----
23.
master
24.
model
25.
source_db
26.
sybsystemprocs
27.
target_db
28.
tempdb
6
2
29.
30. (6 rows affected)
31. Use the database ID to select from the sysusages table. The sysusages table shows you the
order the fragments were created, their sizes, and their segmap values:
32. 1> select dbid, segmap, lstart, size, vstart
33. 2> from sysusages where dbid = 5
34. 3> go
35. dbid
size
36. ----
----37. 5
segmap
vstart
-------
38. 5
39. 5
40. 5
1024
2048
67108864
16
2048
50331648
8
2048
-----
33554432
4
1024
--
-------3
1024
lstart
4096
83886080
41.
42. (4 rows affected)
Verify that all your size values are divisible by 512, that is, that they are whole megabytes.
WARNING!
If you have fragments whose sizes are not divisible by 512, you cannot re-create the database. If
you want to avoid 1/2MB fragments, do not try to load it onto another database. Call Technical
Support if you need help with this problem.
1.
To see what devices the database is on, look at the sysdevices table. Each vstart value from
sysusages will fit between the high and low virtual page numbers of one of the devices.
2.
3.
2> go
4.
low
high
name
5.
--------
--------
---6.
33554432
33556479
dev1
7.
50331648
50333695
dev2
8.
67108864
67110911
dev3
9.
83886080
83888127
dev4
10. 100663296
100665343
dev5
11. 117440512
117442559
dev6
12. 134217728
134219775
dev7
13. 150994944
150996991
dev8
Figure 7 shows the relationship between the system tables that give you information about your
databases.
2.
3.
4.
3> vdevno = 6,
5.
6.
Check the sysdevices table in the master database for the page ranges of the device:
7.
8.
2> go
9.
high
name
13. --------
--------
---14. 100663296
100665343
dev5
15. Use this equation to see if the size of the device is a whole megabyte:
16.
(high
low + 1) / 512
If the device size is not a whole megabyte, drop the device with sp_dropdevice. See the
System Administration Guide for more information on initializing devices.
Refer to the output from the sysusages table for the source database as you create the new
database.
2.
If you have the script which created the source database, run it now, then go to step 5.
3.
Create your database with the fragments in exactly the same order as the source database.
The database in the long example must be created in two steps if you use the log on
option. This is necessary because log is on the second fragment, but the log on option
cannot be used as the second phrase of a create statement.
Depending on where your log segment is, use commands like the following:
Check the messages from the create and alter commands. Verify that the pages allocated
are divisible by 512.
WARNING!
If the number of allocated pages is not divisible by 512, drop the database. Check the sysdevices
table in the master database for the page ranges of the device. Use this equation to see if the size of
the device is a whole megabyte:
(high low + 1) / 512
If the device size is not a whole megabyte, drop the device with sp_dropdevice. See the System
Administration Guide for more information on initializing devices.
1.
2.
3.
4.
3> go
5.
dbid
size
6.
lstart
------
-----
vstart
-------
segmap
------
7.
3
1024
8.
4
1024
9.
100663296
1024
117440512
3
2048
10. 6
2048
134217728
3
2048
4096
150994944
11.
12. (4 rows affected)
Verify that the target database has the same number of fragments as the source and that
the size and lstart values are the same as those of the source database.
Note
dbid and vstart values are normally different than those of the source database. This doesn't matter.
1.
Load the database dump from the source database with a command like the following.
2.
3.
4.
5.
6.
3> go
7.
dbid
size
8.
9.
lstart
------
-----
vstart
-------
------
3
1024
segmap
100663296
10. 6
4
1024
11. 6
8
2048
12. 6
2048
134217728
16
2048
1024
117440512
4096
150994944
13.
14. (4 rows affected)
Verify that the target database now has the same segmap values as the source database.
Refer to the output of the sysusages table for the source database and create your database
with a command like the following, specifying only devices and sizes:
2.
3.
2> on dev5 = 2,
4.
3> dev6 = 2,
5.
4> dev7 = 4,
6.
5> dev8 = 4
7.
8.
7> go
Do not use the log on option and do not add any segments. Segment mapping will come
from the source database when it is loaded.
9.
12. 3> go
13. dbid
size
15. 6
lstart
------
-----
vstart
14. ----
----
segmap
-----7
1024
16. 6
100663296
7
1024
17. 6
1024
117440512
7
2048
18. 6
2048
134217728
7
2048
4096
150994944
19.
20. (4 rows affected)
Verify that the number of fragments is correct and the size and lstart values are the same as
those of the source database.
Note
dbid and vstart values usually differ from those of the source database. This is not a problem.
The segmap values default to 7. These values will change after the load.
1.
2.
3.
4.
5.
6.
3> go
7.
dbid
size
segmap
vstart
lstart
8.
9.
----
------
----
------
3
1024
10. 6
100663296
4
1024
11. 6
1024
117440512
8
2048
12. 6
2048
134217728
16
2048
-----
4096
150994944
13.
14. (4 rows affected)
Verify that the target database now has the same segmap values as the source database.
References
See the SQL Server Reference Manual for command and stored procedure syntax. See the System
Administration Guide for a discussion of segments.
Case
10509968
Number:
1999-04-01
Open Date:
23:58:38.0
Version/EBF: 1151/0
Adaptive Server
Enterprise
OS: AIX
IBM RISC System
Platform:
6000
Product:
Case Description
All segmap fields were set to 7 by mistake after which
transaction processing took place. There is no recent dump of
the master database and there is no copy of original sysusages
available. Dump transaction fails with 4205 error due to mixed
log and data fragments.
Tip or Workaround
Msg 4205, Level 16, State 1:
Line 1:
Syslogs does not exist in its own segment in database '7' with
segmap '7' with logical start page number of '0'. You may not
use DUMP TRAN in this case, use DUMP DATABASE instead.
dbcc findstrandec(dbid):
Finds the number of non-syslogs extents that reside on a
logsegment and toggles the status bit DBT2_STRANDED
(0x8000) in the dbtable structure field dbt_stat2.
Case
10924978
Number:
2003-04-21
Open Date:
16:34:59.7
Version/EBF: 1200/0
Adaptive Server
Enterprise
OS: Windows NT
Platform: PC Windows NT
Product:
Case Description
Customer lost hard disk where sql server resides. They have a
7) drop the database and recreate it again with the right order
For details, refer to http://my.sybase.com/detail?id=1324
Resolution
create database as large as the dump file to get the correct
sysusages.
Resolutions: recovery
10431140
Case Number:
1998-08-10
21:47:44.0
Version/EBF: 1191/0
Open Date:
Adaptive Server
Enterprise
OS: HP-UX
Platform: HP-UX PA-RISC
Case Description
Attempting to load a db. The source db has one data device
300 meg and one log device 200 meg, target one has one
datadevice 700 meg and one log device at 200 meg, result
after load shows 3 devices. Why are target devices converted?
Product:
Tip or Workaround
Read the doc id 1324 from the technical library for details.
Resolution
Create database on data_device = 500 log on log_device = 200
then alter the database by 200MB.
Other Sources Related to Issue
TechNote
DocID 1324 in Tech-info Library
Solved Cases
Case
10447969
Number:
1998-09-28
Open Date:
16:47:12.0
Version/EBF: 1103/0
Adaptive Server
Enterprise
OS: Solaris
Platform: Sun Solaris SPARC
Product:
Case Description
After a load from a dump the number of rows for the source
database in sysusages has more rows than the target's.
Tip or Workaround
1> select * from sysusages where dbid=5
2> go
dbid segmap lstart size vstart pad unreservedpgs
------ ----------- ----------- ----------- ----------- -----------------5 3 0 1024 33554432 NULL 640
5 3 1024 1536 33555456 NULL 1536
5 4 2560 1536 50331648 NULL 1528
5 3 4096 512 83886080 NULL 512
5 3 4608 512 83886592 NULL 512
(5 rows affected)
1> drop database test
2> go
1> create database test on datadev1=2, datadev1=3,
logdev1=3,datadev2=1,datadev2=1
2> go
10391255
Case Number:
1998-05-01
05:22:29.0
Version/EBF: 1102/0
Open Date:
Adaptive Server
Enterprise
OS: UNIX
Platform: HP Tru64
Case Description
Current database is on two 2GB devices. Can the database be
moved to a database on a 4GB device (device was successfully
created on 64bit OS)?
Product:
Tip or Workaround
Not Available
Resolution
The database can be dumped and loaded into the 4GB device if
the segments from the original database both have the same
segmap value and are contiguous.
Other Sources Related to Issue
TechNote
10391815
Case Number:
1998-05-01
22:10:05.0
Version/EBF: 1150/0
Open Date:
Adaptive Server
Enterprise
OS: Solaris
Platform: Sun Solaris SPARC
Case Description
Problems loading a System 10 database from a dump. When
doing a create database with the data and log on seperate
devices, the space that the previous database dump took is
breaking into segments. Data is being put on a log segment.
Product:
Tip or Workaround
Please Refer to Technote # 1324: Segment Remapping with
load database
Resolution
Drop the database and recreate it according to the sysusages
rows for this database in system 10.