首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提高monster数据库方法速度的方法

问提高monster数据库方法速度的方法
EN

Stack Overflow用户
提问于 2014-03-18 04:44:40
回答 1查看 40关注 0票数 0

我正在尝试实现一个服务器端缓存,以避免所有时间都去数据库进行查询。

我已经建立了我的缓存,它可以很好地处理数百个用户,但是当这个数字达到数千个时,它需要很长时间才能建立起来。为了获得一些性能提升,我可以对此做些什么优化。

谢谢

代码语言:javascript
复制
/**
 * 
 * Get a user profile
 * 
 * @param userid
 *            the userid of the profile to get
 * 
 * @param serialized
 *            true if you want the profile string serialized
 * 
 * @return the user profile as a string or an Object
 */
private static ConcurrentMap<String, Profile> getUserProfiles() {

    final String select = "SELECT ua.userid, ua.displayname, ua.major, ua.minor,ua.profileimage,ua.genderpreference,"
            + "ad.devicetoken,pf.optionalImageOne, pf.optionalImageTwo,  pf.optionalImageThree, "
            + "pf.optionalImagefour,pp.created,hon.numhots,hon.numnots "
            + "FROM UserAccount ua "
            + "LEFT OUTER JOIN ApnsDevices ad ON ad.userid = ua.userid "
            + "LEFT OUTER JOIN ProfileImages pf ON pf.userid = ua.userid "
            + "LEFT OUTER JOIN PendingProfile pp ON pp.userid = ua.userid "
            + "LEFT OUTER JOIN HotOrNot hon ON hon.userid = ua.userid";

    final String friendsList = "SELECT * FROM Shortlist WHERE userid = ?";
    String displayName = "";
    String userid = null;
    String genderpreference = null;

    Profile userProfile;
    int majorid = 0;
    int minorid = 0;
    int hots = 0;
    int nots = 0;

    byte[][] images = new byte[4][];
    byte[] image = null;

    Connection conn = null;
    PreparedStatement st = null;
    ResultSet rs = null;
    ResultSet rs2 = null;

    Timestamp onDemandTimeSent = null;
    long origianlStartTime = System.nanoTime();
    ConcurrentMap<String, Profile> results = new ConcurrentHashMap<String, Profile>();

    try {
        conn = source.getConnection();
        st = conn.prepareStatement(select);
        rs = st.executeQuery(select);
        while (rs.next()) {

            byte[] imageOne = null;
            byte[] imageTwo = null;
            byte[] imageThree = null;
            byte[] imageFour = null;

            long startTime = System.nanoTime();
            userid = rs.getString("userid");
            genderpreference = rs.getString("genderpreference");
            majorid = rs.getInt("major");
            minorid = rs.getInt("minor");
            image = rs.getBytes("profileimage");
            displayName = rs.getString("displayname");
            hots = rs.getInt("numhots");
            nots = rs.getInt("numnots");
            onDemandTimeSent = rs.getTimestamp("created");

            if (rs.getBlob("optionalImageOne") != null){
                imageOne = rs.getBlob("optionalImageOne").getBytes(1,(int) rs.getBlob("optionalImageOne").length());
            }
            if (rs.getBlob("optionalImageTwo") != null){
                imageTwo = rs.getBlob("optionalImageTwo").getBytes(1,(int) rs.getBlob("optionalImageTwo").length());
            }
            if (rs.getBlob("optionalImageThree") != null){
                imageThree = rs.getBlob("optionalImageThree").getBytes(1,(int) rs.getBlob("optionalImageThree").length());
            }
            if (rs.getBlob("optionalImageFour") != null){
                imageFour = rs.getBlob("optionalImageFour").getBytes(1,(int) rs.getBlob("optionalImageFour").length());
            }

            images[0] = imageOne;
            images[1] = imageTwo;
            images[2] = imageThree;
            images[3] = imageFour;
            String deviceToken = rs.getString("devicetoken");

            // get friends list
            List<String> friends = new ArrayList<String>();
            st = conn.prepareStatement(friendsList);
            st.setString(1, userid);
            rs2 = st.executeQuery();
            String friend;
            while (rs2.next()) {
                friend = rs2.getString("friend");
                if (friend != null) {
                    friends.add(friend);
                }
            }

            if (onDemandTimeSent != null) {
                userProfile = new Profile(image, onDemandTimeSent.toString(), userid, displayName, hots, nots, majorid, minorid, deviceToken, genderpreference, friends);
            } else {
                userProfile = new Profile(image, null, userid, displayName, hots, nots, majorid, minorid, deviceToken, genderpreference, friends);
            }

            userProfile.setAlternativeImages(images);
            long endTime = System.nanoTime();
            double totalTime = TimeUnit.SECONDS.convert((endTime - startTime), TimeUnit.NANOSECONDS);
            System.out.println("Total time to generate user profile for "+ userid + " was : " + totalTime + " seconds : " + userProfile);
            logger.info("Generated user profile for " + userid);
            results.put(userid, userProfile);
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.warn("An error occured when getting user profile for "+ userid, e);
    } finally {
        closeConnection(conn);
        closeResultSet(rs);
        closeResultSet(rs2);
        closePreparedStatement(st);
    }
    long endTime = System.nanoTime();
    double totalTime = TimeUnit.SECONDS.convert((endTime - origianlStartTime), TimeUnit.NANOSECONDS);
    System.out.println("Total time to generate all user profiles for " + results.size() + " users was " + totalTime + " seconds");
    return results;
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-18 05:00:09

我不会重复发明轮子:See memcached

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22464974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档