SQL Server Compact - May unexpectedly rebuild table indexes when database first opened

You Can Take it With You

Syndication

News

  • Don't miss the next Windows Mobile Webcast... Unit Testing for Mobile Devices: http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032382824&EventCategory=4&culture=en-US&CountryCode=US.

One of the nice features of SQL Server Compact (SSC) is portability. Each database is fully self-contained in a single file and that file can be freely moved between Windows Mobile Devices, and desktop or server computers. It turns out that this portability may result in some unexpected, and potentially painful, behavior.

You may find that a SSC database you create on a Windows Vista computer results in a long-delay when you attempt to open the SSC database on a Windows Mobile device. However, if you create the SSC database on a Windows XP computer, the database opens on a Windows Mobile device with no delay.

The problem occurs because Windows Vista and Windows Server 2008 use a different sort order than that used by earlier Windows versions - As we all know, indexes are based on sort order.

SSC indexing needs to be consistent with the sort-order of the host platform. As a result, when you open a SSC database, the SSC engine checks to see if the sort order used to create the database indexes is the same as the sort-order of the host platform. If the sort orders are different, SSC rebuilds all of indexes in the database. Fortunately, once the new indexes are built, all future attempts to open the database on the current platform will occur without delay.

This is certainly something to keep in mind if you're thinking of upgrading from Windows XP to Windows Vista or Windows Server 2003 to Windows Server 2008 if you have a process that creates or exchanges SSC database files with Windows Mobile devices.


Posted Jan 23 2008, 11:54 AM by jim-wilson

Comments

Denis wrote re: SQL Server Compact - May unexpectedly rebuild table indexes when database first opened
on 01-23-2008 10:44 AM
Actually it does not have to match. Full version of SQL carries its version of sort order along and thus full SQL Server behaves identically on all platforms. Sort order may change from version to version though (e.g. SQL 2000 to SQL 2005).

SQL Compact exhibits poor design and lack of consideration for the user. It burdens the user with something that should never be user's concern. Boo.

Jim Wilson wrote re: SQL Server Compact - May unexpectedly rebuild table indexes when database first opened
on 01-23-2008 12:26 PM
Denis;

In the full version, you're right, sort-order doesn't have to match the host. That's because Full SQL Server is a self-contained server allowed to occupy many, MANY megabytes of disk-storage as well as many megabytes of RAM. Full SQL Server can take up pretty much as much memory as it wants; as a result, providing a selectable sort-order that's internally implemented is a good idea.

In terms of calling SSC index management "poor design", I really think it's a matter of trade-offs. The entire SSC installation takes up less than 4MB. Maintaining such a small footprint requires one to make trade-offs. I think using the host sort-order APIs rather than incur the overhead of implementing and managing custom sort-oriented APIs is a reasonable one. Remember, SSC is intended to provide an application with locally accessible, reliable storage; it’s not intended to be a little version of the great-big SQL Server engine.

Another key point to consider; if you’re using a database system that runs in-process to your application, wouldn’t you expect the database to order data in the same order as other sorted data you store in your application? Take this scenario...
- You have an array of randomly entered strings
- You write the array contents to a database table that is indexed on those values
- You sort the array in memory using Array.Sort or similar

Wouldn’t you expect the array and database table to return the contents in the same order? If SSC didn’t use the host sort-oriented APIs, in some cases the database table order would differ from the array order. I think most of us would consider that an error.

I think we can agree that it’s a matter of choosing the right tool for the job.

If one needs the flexibility to control various aspects of the data storage such as sort-order consistency across platforms, along with a boat-load of other tunable features, then one should choose Full SQL Server – and pay the memory cost and overhead that goes with that.

If one needs a database that’s highly portable, works well on both a mobile device and a desktop computer, does not require any software to be installed other than the app using it, does not require network connectivity to access it, and consumes minimal resources then one should choose SQL Server Compact – and accept that they may not have every possible feature available.

Neither way is right or wrong, just solving different problems.

Jim :-)
Denis wrote re: SQL Server Compact - May unexpectedly rebuild table indexes when database first opened
on 02-01-2008 9:45 AM
Well, those are some very convincing arguments.
I admit I did not think it trough.

It still pains me to see the user impact being of what is essentially an optimization being so large.
Jim Wilson wrote re: SQL Server Compact - May unexpectedly rebuild table indexes when database first opened
on 02-01-2008 10:55 AM
Denise;

"It still pains me to see the user impact ... being so large"

I couldn't agree more.

Jim :-)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?