代码之家  ›  专栏  ›  技术社区  ›  Miklos Jakab

使用ForeignCollection查询或创建对象时,ormLite有什么开销?

  •  1
  • Miklos Jakab  · 技术社区  · 10 年前

    我有一张桌子有玩家,另一张桌子上有游戏。它们之间存在玩家----(1..n)[游戏]关系(字段定义可能不完全正确):

    // Player
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField
    public String name;
    
    @ForeignCollectionField(
        eager = true,
        maxEagerLevel = 3)
    public ForeignCollection<Game> games;
    
    
    // Game
    @DatabaseField
    String title;
    
    @DatabaseField
    String playerName;
    

    我想获取并返回所有游戏的列表。让ormLite选择ForeignCollection的开销是什么时候?还是这样做更好:

        final List<Game> allGames = daoGames.getAllGroupedByName();
        final List<Player> allPlayers = gameDao.getAll();
    
        final HashMap<String, List<Game>> games = new HashMap<String, List<Game>>();
        for (Game currentGame : allGames) {
            final String player = currentGame.playerName;
            if (games.get(player) == null) {
                games.put(player, new ArrayList<Game>());
            }
            final List<Game> gamesOfPlayer = games.get(player);
            gamesOfPlayer.add(currentGame);
        }
        for (Player player : allPlayers) {
            player.games = games.get(player.name);
        }
    

    我猜ormLite会对每个玩家进行查询。与一个daoGames.getAllGroupedByName()相比,这是一个很大的开销吗(尽管甚至不需要groupBy)?

    1 回复  |  直到 10 年前
        1
  •  0
  •   Gray droiddeveloper    10 年前

    我猜ormLite会对每个玩家进行查询。与一个daoGames.getAllGroupedByName()相比,这是一个很大的开销吗(尽管甚至不需要groupBy)?

    这是正确的。ORMLite对每一个单独的查询并不能很有效地做到这一点 Player 后果你的机制 getAll() 并且手动匹配它们很可能会更快。

    我会用 ArrayList 除非你真的需要 LinkedList 能力。