Tuesday, June 2, 2015

SQL CE Index is not populated during a batch insert

Discovered a strange behavior with SQL CE (version 3.0) in a handheld mobile application the other day. One of the tables had a million+ rows with an index on the correct column for the query, but it was taking a few minutes to return the results.

The table was used as a template table with 0 rows to start with and then the million+ rows inserted into the table as part of the application logic. Though the index exists, it was discovered to be empty while running the command

                 sp_show_statistics 'table_name', 'index_name'


TABLE    INDEX                     ROWS    ROWS_SAMPLED    STEPS     DENSITY
Item      UQ_Item_Index       0           0                          0            0


The solution was to drop the index and create it after the rows are inserted in the application logic.
Once this change was made, the result returned in less than 1 second. There are a lot of peculiarities with the SQL CE database because of its limitations and I guess this is one of them.
Apparently the index is not populated if it exists already on an empty CE table, unless it is created after some data is inserted in the table.

TABLE    INDEX                    ROWS        ROWS_SAMPLED    STEPS    DENSITY
Item      UQ_Item_Index    1129454      1129454                200        8.853835E-07

No comments:

Post a Comment