SQL Compact 4.0身份密钥生成和EF插入问题 [英] SQL Compact 4.0 identity keys generation and EF insert problem

查看:71
本文介绍了SQL Compact 4.0身份密钥生成和EF插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在插入大约1500条记录时使用实体框架和CE数据库4.0遇到了严重问题,这里是问题描述:


我有一个由EF映射的表,它使用标识使用int主键。我的问题是,当我创建几个新对象并且保存它们时,这个操作花费了太多时间,但有趣的是插入这些
记录所需的时间正在快速增长,记录数量保存在目标表。


以下是每个插入CE数据库的报告。在插入少量数据时开始没有问题,但是当数量达到1500时,那么时间将从几秒钟移动到几分钟,这似乎是一些n ^ 2算法在那里进行
和我的问题如果这可能是由CE数据库生成的身份密钥引起的,或者是否有人可以帮助如何在CE 4.0中生成密钥。

生成  PersonLiving 月份  1.1.2010 
生成地址...
0 实例  地址  ;类 保存 在:  2ms的 (4455ticks)
PersonLiving 对于 月:  2010年1月1日 产生 在  474ms (924518ticks)
生成  PersonLiving 对于 一个月&NBSP ; 1.2.2010
生成地址...
145   地址 类 已保存在:  1649ms (3213547ticks)
P ersonLiving 为 月:  2010年2月1日 产生 以  1841ms (3588574ticks)
生成  PersonLiving 为 当月  2010年3月1日
生成 地址...
的&NBSP;螺纹&NBSP; '<否&NBSP;名称>' &NBSP;(0x1b10)&NBSP;具有&NBSP; EXITED&NBSP;与&NBSP;代码&NBSP; 0&NBSP;(为0x0)。
  '< No  Name>'   (0x1910) 已退出 代码  0 (0x0)。
280&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&NBSP; 2720ms&NBSP;(5300321ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP; 2010年3月1日&NBSP;产生&NBSP;在&NBSP; 2949ms&NBSP;(5747549ticks)
生成&NBSP; PersonLiving&NBSP;为&NBSP;当月&NBSP; 1.4.2010
生成&NBSP;地址...
中的&NBSP;螺纹&NBSP; '<不&NBSP;名称>' &NBSP;(0x1ef8)&NBSP;具有&NBSP;退出&NBSP;与&NBSP;代码&NBSP; 0&NBSP;(为0x0)。
310&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&NBSP; 3117ms&NBSP;(6074759ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP; 1.4.2010&NBSP;产生&NBSP;在&NBSP; 3309ms&NBSP;(6448577ticks)
生成&NBSP; PersonLiving&NBSP;为&NBSP;当月&NBSP; 2010年5月1日
生成&NBSP;地址...
385&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&NBSP; 4362ms&NBSP;(8500084ticks)
PersonLiving&NBSP;为&NBSP;月:&NBSP; 2010年5月1日&NBSP;产生&NBSP;以  4542ms&NBSP;(8851156ticks)
生成&NBSP; PersonLiving&NBSP;为&NBSP;当月&NBSP; 2010年6月1日
生成&NBSP;地址...
490&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&NBSP; 5756ms&NBSP;(11216108ticks)
PersonLiving&NBSP;为&NBSP;月:&NBSP; 2010年1月6日&NBSP;产生&NBSP;以  5946ms&NBSP;(11585805ticks)
生成  PersonLiving 月份  1.7.2010
生成地址...
    地址&NBSP;类&NBSP;保存&NBSP;在:&的 8037ms&NBSP; (15659668ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP; 2010年7月1日&NBSP;产生&NBSP;在&NBSP; 8246ms&NBSP;(16066057ticks)
生成&NBSP; PersonLiving&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在&NBSP;当月&NBSP; 2010年8月1日
生成&NBSP;地址...
920&NBSP;实例&NBSP:&NBSP; 13471ms&NBSP; (26246415ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP; 2010年8月1日&NBSP;产生&NBSP;在&NBSP; 13671ms&NBSP;(26636372ticks)
生成&NBSP; PersonLiving&NBSP;对于&NBSP;当月&NBSP; 2010年9月1日
生成&NBSP;地址...
965&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&NBSP; 16742ms&NBSP; (32619246ticks)
PersonLiving&NBSP;为&NBSP;月:&NBSP; 1.9。 2010&NBSP;产生&NBSP;以  16939ms&NBSP;(33003541ticks)
生成&NBSP; PersonLiving&NBSP;为&NBSP;当月&NBSP; 2010年1月10日
生成&NBSP;地址...
1375&NBSP;实例与国家统计局磷;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&的 28896ms&NBSP; (56299830ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP;二○一○年十月一日&NBSP;产生&NBSP;在&NBSP; 29132ms&NBSP;(56759196ticks )
生成&NBSP; PersonLiving&NBSP;为&NBSP;当月&NBSP; 2010年11月1日
生成&NBSP;地址...
中的&NBSP;螺纹&NBSP; '<不&NBSP;名称>' &NBSP;(0x1b44)&NBSP;具有&NBSP ;退出&NBSP;与&NBSP;代码&NBSP; 0&NBSP;(为0x0)。
1550&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在:&的 36878ms&NBSP; (71852250ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP; 2010年1月11日&NBSP;产生&NBSP;以  37099ms&NBSP;(72282885ticks)
生成&NBSP; PersonLiving&NBSP;为&NBSP;当月&NBSP; 2010年1月12日
生成&NBSP;地址...
1500&NBSP;实例&NBSP;的&NBSP;地址&NBSP;类&NBSP;保存&NBSP;在: &的 42285ms&NBSP; (82386665ticks)
PersonLiving&NBSP;对于&NBSP;月:&NBSP; 2010年1月12日&NBSP;产生&NBSP;在&NBSP; 42527ms&NBSP;(82856871ticks)


zDN @

解决方案

AFAIK为每个表存储当前标识值,因此生成下一个值应该如下所示添加一个。


我注意到结束此系列的1500个实例测试仍然比之前的1550实例测试慢。


如果所有这些测试在同一批次中运行我会尝试测试se相同数量的实体上的veral时间或每次运行只运行一次测试(您的代码可能会做一些会降低性能的事情)。例如,如果在100次测试之后你将
写入2个实体但保留了上下文中的所有先前写入的实体,则可能是EF很难在先前测试中保留的数千个未更改实体中找到这2个实体等...)


Hi,

I hit a serious problem using Entity Framework with CE database 4.0 when inserting for about 1500 of records, here is problem description:

I have a table mapped by EF which is using int primary key using identity. My problem is, that when I create several new objects and attemt to save them this operation tooks too much time, but what is interesting the time needed to insert this amount of records is rapidly growing up with the records count saved in the target table.

Below is report of each insert into CE database. On the beggining when inserting smaller amounts of data there is no problem, but when the amount reaches 1500, then the time will move from seconds to minutes which seems, that some n^2 algorithm is going out there and my question is if that can be caused by identities key generation by CE database or if anyone can help how are the keys generated in CE 4.0.

Generating PersonLiving for month 1.1.2010
Generating Addresses...
0 instances of Address class saved in: 2ms (4455ticks)
PersonLiving for month: 1.1.2010 generated in 474ms (924518ticks)
Generating PersonLiving for month 1.2.2010
Generating Addresses...
145 instances of Address class saved in: 1649ms (3213547ticks)
PersonLiving for month: 1.2.2010 generated in 1841ms (3588574ticks)
Generating PersonLiving for month 1.3.2010
Generating Addresses...
The thread '<No Name>' (0x1b10) has exited with code 0 (0x0).
The thread '<No Name>' (0x1910) has exited with code 0 (0x0).
280 instances of Address class saved in: 2720ms (5300321ticks)
PersonLiving for month: 1.3.2010 generated in 2949ms (5747549ticks)
Generating PersonLiving for month 1.4.2010
Generating Addresses...
The thread '<No Name>' (0x1ef8) has exited with code 0 (0x0).
310 instances of Address class saved in: 3117ms (6074759ticks)
PersonLiving for month: 1.4.2010 generated in 3309ms (6448577ticks)
Generating PersonLiving for month 1.5.2010
Generating Addresses...
385 instances of Address class saved in: 4362ms (8500084ticks)
PersonLiving for month: 1.5.2010 generated in 4542ms (8851156ticks)
Generating PersonLiving for month 1.6.2010
Generating Addresses...
490 instances of Address class saved in: 5756ms (11216108ticks)
PersonLiving for month: 1.6.2010 generated in 5946ms (11585805ticks)
Generating PersonLiving for month 1.7.2010
Generating Addresses...
600 instances of Address class saved in: 8037ms (15659668ticks)
PersonLiving for month: 1.7.2010 generated in 8246ms (16066057ticks)
Generating PersonLiving for month 1.8.2010
Generating Addresses...
920 instances of Address class saved in: 13471ms (26246415ticks)
PersonLiving for month: 1.8.2010 generated in 13671ms (26636372ticks)
Generating PersonLiving for month 1.9.2010
Generating Addresses...
965 instances of Address class saved in: 16742ms (32619246ticks)
PersonLiving for month: 1.9.2010 generated in 16939ms (33003541ticks)
Generating PersonLiving for month 1.10.2010
Generating Addresses...
1375 instances of Address class saved in: 28896ms (56299830ticks)
PersonLiving for month: 1.10.2010 generated in 29132ms (56759196ticks)
Generating PersonLiving for month 1.11.2010
Generating Addresses...
The thread '<No Name>' (0x1b44) has exited with code 0 (0x0).
1550 instances of Address class saved in: 36878ms (71852250ticks)
PersonLiving for month: 1.11.2010 generated in 37099ms (72282885ticks)
Generating PersonLiving for month 1.12.2010
Generating Addresses...
1500 instances of Address class saved in: 42285ms (82386665ticks)
PersonLiving for month: 1.12.2010 generated in 42527ms (82856871ticks)


zDN@

解决方案

AFAIK the current identity value is stored for each table so generating the next value should be as simple as adding one.

I noticed that the 1500 instances test that ends this series is still slower than the previous 1550 instances test.

If all those tests runs in the same batch I would try either to test several times on the same number of entities or to run only a single test per run (your code could do something that degrades performance over times). For example if after 100 tests you write 2 entities but have kept all the previous written entities in your context, it could be that EF has a hard time to locate those 2 entities inside thousands of unchanged entities you kept from previous tests etc...)


这篇关于SQL Compact 4.0身份密钥生成和EF插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆