While running your application on SQL CE v3.1 databases you may observe slowness in the performance of some of the queries especially those which involve querying the primary key.
In Sql server databases when you create a Primary Key on a column of a table, it will automatically create a unique index on the column which prevent duplicate entries to be inserted on the primary key column. This Unique index which is generated by Sql server engine automatically are called as the system generated indexes.
Ideally the statistics should be created automatically on the system generated indexes but we have observed that on SQL CE v3.1 databases statistics are not automatically created on the system generated indexes.
Due to the absence of statistics optimizer may select wrong execution which can ultimately lead to poor performance of the sql queries.
To improve the performance we need to manually create the statistics on system generated indexes using following query
CREATE STATISTICS ON <table-name> WITH FULLSCAN,NORECOMPUTE
FULLSCAN ensure that entire table is scanned to generate the statistics and we have not used any sampling.
NORECOMPUTE is used to indicate that Statistics will be updated manually and should not be triggered automatically by optimizer.
Once the statistics are created we need to manually update the statistics periodically using the following command
UPDATE STATISTICS ON <table-name> WITH FULLSCAN,NORECOMPUTE.
The above issue does not occur in SSCE v3.5 in which the statistics are automatically created on system generated indexes and hence the same query might appear to run fast on SSCE v3.5 while it appears to run slow on SSCE v3.1
If after creating and updating the statistics on the system generated indexes manually you still observe slowness in performance you might considering using other query tuning option
SE, Microsoft SQL Server