Search documentation of Metastatic

# Metastatic.Analysis.BusinessLogic.MissingPreload (Metastatic v0.13.2)

View Source

Detects database queries without eager loading (N+1 query potential).

Universal pattern: accessing related data without preloading/prefetching.

## Examples

Python (Django ORM without select_related):

    users = User.objects.all()  # Should use select_related('profile')
    for user in users:
        print(user.profile.bio)  # N+1 query - profile accessed in loop

JavaScript (Sequelize without include):

    const users = await User.findAll();  # Should include: [{ model: Profile }]
    users.forEach(user => {
        console.log(user.profile.bio);  # N+1 - profile fetched per user
    });

Elixir (Ecto without preload):

    users = Repo.all(User)  # Should use preload(:profile)
    Enum.each(users, fn user ->
        IO.puts(user.profile.bio)  # N+1 - profile loaded per user
    end)

C# (Entity Framework without Include):

    var users = context.Users.ToList();  # Should use Include(u => u.Profile)
    foreach (var user in users) {
        Console.WriteLine(user.Profile.Bio);  # Lazy loading - N+1 queries
    }

Go (GORM without Preload):

    var users []User
    db.Find(&users)  # Should use Preload("Profile")
    for _, user := range users {
        fmt.Println(user.Profile.Bio)  # N+1 - separate query per user
    }

Java (Hibernate without JOIN FETCH):

    List<User> users = session.createQuery("FROM User").list();  # Should use JOIN FETCH
    for (User user : users) {
        System.out.println(user.getProfile().getBio());  # Lazy loading triggers N queries
    }

Ruby (ActiveRecord without includes):

    users = User.all  # Should use User.includes(:profile)
    users.each do |user|
        puts user.profile.bio  # N+1 - profile query per user
    end

## Detection Modes

The analyzer supports two detection modes:

1. Semantic (preferred): Uses `op_kind` metadata from semantic enrichment. If a function_call has `op_kind: [domain: :db, operation: :retrieve_all]`, it's definitively a database collection query.

2. Heuristic (fallback): Uses pattern matching on function names when semantic metadata is not available. May produce false positives.