开发者

Database design: bigger table vs query the entity for every row in dataTable

I have a table of NewsFeed.

NewsFeed开发者_如何学编程

+ id

+ fromUserId

+ targetId

+ type

so the news is like this: Tom Wakefield commented on Peter Smith's profile.

So either I add two more fields into my NewsFeed call fromUserName and targetUserName

OR

For every row I display in my dataTable I would query from the Entity.

<p:dataTable value="#{myBean.news}" var="item">
    <p:column>
         <h:outputText value="#{myBean.getName(item.fromUserId)} " />
         <h:outputText value="commented on " />
         <h:outputText value="#{myBean.getName(item.targetId)}" />
    </p:column>
</p:dataTable>

then inside myBean.java

myBean.java

public String getName(Long userId){
    User u = mySessionBean.findUserById(userId);
    return u.getFName() + " " + u.getLName();
}

Which way is better? Does making query at every row really hurting me a lot in term of performance

Note: the database expect to have lot of users. News are display very often.


"normalize until it hurts, denormalize until it works"

IOW: record only the IDs and use JOINs to get the whole data.


Does making query at every row really hurting me a lot in term of performance

No.

Duplicating data causes more problems than it solves.

Duplicated relationships make updates nearly impossible to process correctly.

Object-Relational Managers can -- and often do -- generate proper SQL joins to optimize the fetches. Also ORM's have cache. And the database has cache.

The 2nd and 3rd normal forms amount to the following rule: Do Not Duplicate Relationships.

In rare cases you can "denormalize" and improve performance without creating complex update logic. In most other cases, the duplicated data leads to updates and inserts that are either either complex or slow or both.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜