首页 新闻 会员 周边

Sql Server数据库表设计问题

0
悬赏园豆:100 [待解决问题]

小项目使用Spring和Sql Server接收并存储数据。

表间的关系和逻辑简化如下,有3个表:

Table: Item:
--itemId(unique,primary-key,auto-increase)
--name
--category
--description

Table: Recipient:
--recipientId(unique,primary-key,auto-increase)
--first name
--last name
--address
--email

Table: Order:
--id(unique,primary-key,auto-increase)
--price
--date
--itemId(foreign-key)
--recipientId(foreign-key)


逻辑可以简化为近似的下单系统,有三个表分表代表物品(item),收件人(recipient)和订单(Order)。现在Spring没接收一个订单,就必须将这个订单数据插入数据库:

先找物品,如果表内有这个物品则返回itemId,没有的话就插入这个新物品然后返回itemId。
再找收件人,如果表内有这个收件人则返回recipentId,没有的话就插入这个新收件人然后返回recipentId。
最后根据上面得到的itemId和recipentId,插入订单数据。

上面的逻辑,我都在Sql Server 2008 R2里使用存储过程完成,代码如下:

 

CREATE procedere spInsert

--list of parameters

set @tmpItemId = (select itemId from item where name=@name and category=@category and description=@description)

if @tmpItemId is NULL

   begin 
       insert to item values(@name,@category,@description)
       set @tmpItemId=@@IDENTITY
   end

set @tmpRecipentId = (select recipentId from recipient where firstname=@firstname and lastname=@lastname and address=@address and email=@email )

if @tmpRecipentId is NULL

   begin 
       insert to recipient values(@firstname ,@lastname ,@address,@email )
       set @tmpRecipentId =@@IDENTITY
   end

insert into order values(@price,@date,@tmpItemId,@tmpRecipentId )

 

对于每一条入来的订单,Spring直接调用如下代码:

 

JdbcTemplate jt = getJdbcTemplate();
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jt);
simpleJdbcCall.withProcedureName("spInsert").execute(paraMap);

 

但是由于订单量太大,数据库插入的速度跟不上,经测试,评价10-30秒插入100条信息。

请各位高手高人帮忙看看,这里有什么可以优化的地方,包括数据库表的设计等等。

谢谢

macemers的主页 macemers | 初学一级 | 园豆:102
提问于:2014-03-31 11:19
< >
分享
所有回答(1)
0

项目小不小,不是以老板给你多少工资计算,而是以项目处理的数据有关系。如果你处理的订单多到这种程度,

怎么说一年也有上亿的销售额不止了。

就好比淘宝,实现功能很简单,如果只有全中国两个人使用的话,淘宝这个系统不到百万估计就开发下来了。

可是一考虑到双十一,这个系统就要几十亿开发费用了。

爱编程的大叔 | 园豆:30839 (高人七级) | 2014-03-31 12:34

你就当这是压力测试吧。我估计是select那里拖慢了速度,将itemId和recipientId改为UUID作主键,避免每次都select,会不会有改善?

支持(0) 反对(0) macemers | 园豆:102 (初学一级) | 2014-03-31 13:14

@macemers: 

通常产品表,除了ItemID主关键字外,多半ArtNo(货号)也应该是唯一的。

改善当然会有啦。你这样传递的参数也可以少了,只要传ID就行。

 

你要当压力测试我是没有问题啦,只想说不同数据量的系统,数据库的设计本身就有很大的不同。

支持(0) 反对(0) 爱编程的大叔 | 园豆:30839 (高人七级) | 2014-03-31 14:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册