标签: eager-loading

如何避免 Rails 依赖销毁中的 N+1 查询?

Class User < ActiveRecord::Base
  has_many :posts, dependent: :destroy
end

Class Post < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

当拥有 N 个帖子的用户被销毁时,将运行 N+1 个查询来销毁关联的帖子和用户。在这种情况下如何避免急切加载?

performance ruby-on-rails associations eager-loading destroy

5
推荐指数
1
解决办法
1217
查看次数

SQLAlchemy仅加载集合,而不会在加载时反向引用

例如(eagerload / joinedload做同样的事情):

session = Session()    
parents = session.query(Parent).options(joinedload(Parent.children)).all()
session.close()

print parents[0].children  # This works
print parents[0].children[0].parent  # This gives a lazy loading error
Run Code Online (Sandbox Code Playgroud)

在关闭会话之前可以添加以下循环(并且不会影响数据库):

for p in parents:
  for c in p.children:
    c.parent
Run Code Online (Sandbox Code Playgroud)

真是愚蠢。有没有一种方法可以更改原始查询,以便在不增加输出SQL中添加更多联接的情况下加载关系的两端?

更新(如果相关);这是映射

class Parent(Entity):
  __tablename__ = "parent"

  id = Column(Integer, primary_key=True)
  children = relation("Child", backref="parent")

class Child(Entity):
  __tablename__ = "child"

  id = Column(Integer, primary_key=True)
  parentId = Column(Integer, ForeignKey("parent.id"), index=True)
Run Code Online (Sandbox Code Playgroud)

sqlalchemy eager-loading

4
推荐指数
1
解决办法
1522
查看次数

我可以使用HQL来加载属性吗?

我正在尝试解决如何在以下HQL查询中急切加载客户:

select order.Customer
from Order as order
where order.Id in
(
  select itemId
  from BadItem as badItem
  where (badItemType = :itemType) and (badItem.Date >= :yesterday)
)
Run Code Online (Sandbox Code Playgroud)

订单和客户之间通常有多对一的关系.

我想这样做是在查询,而不是在映射,如果可能的话 - 因为在"连接抓取......"

也许查询会被重构为一个连接,我有一个心理障碍.

有任何想法吗?

nhibernate select hql eager-loading

4
推荐指数
1
解决办法
5390
查看次数

如何首先急切地加载与实体框架代码的多对多关系?

为了清楚起见,我将给出我能想到的最基本的例子.

可以说我有两个以下形式的实体:

public class Student
{
    public int Id {get;set}
    public string FullName {get;set;}
    public virtual ICollection<Course> Courses {get;set;}
}

public class Courses
{
    public int Id {get;set;}
    public string FullName {get;set;}
    public virtual ICollection<Student> Students {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

这两个实体映射到三个表,第三个表是连接的表.

当我像这样查询学生时

var allStudents = context.Students;
Run Code Online (Sandbox Code Playgroud)

然后遍历结果以显示学生及其课程列表

foreach (var student in allStudents) 
{
    display(student.FullName);
    foreach (var course in student.Courses) 
    {
        display(course.FullName);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到第一个查询返回的每个学生的课程查询.

如何通过一个查询告诉实体框架急切地将课程加载到学生中?

database many-to-many entity-framework eager-loading ef-code-first

4
推荐指数
1
解决办法
3110
查看次数

如何将IQueryable <T>转换为ObjectQuery <T>?

我正在尝试在这篇文章中应用建议:提示22 - 如何使Include真正包括在内

它建议在实体框架(4.2)中确保急切加载的解决方法.该解决方法涉及将IQueryable转换为ObjectQuery.

但是,当我尝试这个时,如帖子所示,查询返回null.

我的查询是(ctx是DbContext):

IEnumerable<Coupon> coupons =
    from x in ctx.Coupons
    where x.LanguageCode.Equals("EN", StringComparison.InvariantCultureIgnoreCase) && x.CategoryId == MainCategoryId && x.Date >= fromDate && x.Date <= toDate
    select x;
Run Code Online (Sandbox Code Playgroud)

这按预期工作.

但是,当我使用时,

IEnumerable<Coupon> coupons =
    (from x in ctx.Coupons
     where x.LanguageCode.Equals("EN", StringComparison.InvariantCultureIgnoreCase) && x.CategoryId == MainCategoryId && x.Date >= fromDate && x.Date <= toDate
     select x) as ObjectQuery<Coupon>;
Run Code Online (Sandbox Code Playgroud)

它为"优惠券"指定了一个空值.

知道我做错了什么吗?

.net c# iqueryable objectquery eager-loading

4
推荐指数
1
解决办法
8201
查看次数

如何始终包含急切加载的模型

我使用bullet gem让我知道N + 1个查询.

我想避免偶尔添加include.

我有一个comment属于user模型的模型

有没有办法告诉模型任何时候访问评论模型以包括用户?(而不是Comment.include(:user)每次都做)

activerecord eager-loading ruby-on-rails-3

4
推荐指数
1
解决办法
1100
查看次数

如何在NHibernate中没有重复的情况下加载加载关联?

我需要加载一个包含很多孩子和孩子孩子的非常大的物品清单.什么是最好的方法?

我正在使用Oracle 11g数据库,我编写了以下方法,但它产生了笛卡尔积(重复结果):

 public IList<ARNomination> GetByEventId(long eventId)
        {
            var session = this._sessionFactory.Session;

            var nominationQuery = session.Query<ARNomination>().Where(n => n.Event.Id == eventId);

            using (var trans = session.Transaction)
            {
                trans.Begin();

                // this will load the Contacts in one statement
                nominationQuery
                    .FetchMany(n => n.Contacts)
                    .ToFuture();

                // this will load the CustomAttributes in one statement
                nominationQuery
                    .FetchMany(n => n.CustomAttributes)
                    .ToFuture();

                // this will load the nominations but joins those two tables in one statement which results in cartesian product
                nominationQuery
                    .FetchMany(n => n.CustomAttributes)
                    .FetchMany(n => …
Run Code Online (Sandbox Code Playgroud)

nhibernate orm future cartesian-product eager-loading

4
推荐指数
1
解决办法
4257
查看次数

我们如何在基于Dot Net的Azure移动服务中加载相关对象(Eager Loading)?

如果我有以下模型结构

public class QuestionItem: EntityData
{
    public string Content { get; set; }
    public bool IsAnswered { get; set; }
    public int NumberOfAnswers
    {
        //todo: make it computable
        get;
        set;
    }
    public UserItem By { get; set; }
    public string ById { get; set; }
    public string AtLocation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

&parent实体为

public class UserItem:EntityData
{

    public string UserName { get; set; }

    public string Gender { get; set; }

    public string BaseLocation { get; set; }

    public …
Run Code Online (Sandbox Code Playgroud)

.net azure eager-loading azure-mobile-services

4
推荐指数
1
解决办法
1115
查看次数

laravel eloquent渴望加载嵌套条件

我有2个表正在使用预先加载,然后在那个急切的加载中使用嵌套条件:

//migration for lead table
public function up()
{
    Schema::create('leads', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('first_name',255);
        $table->string('surname',255);
    });

    Schema::table('leads', function($table)
    {
        $table->foreign('create_by')->references('id')->on('employees')->onDelete('cascade');
    });
}

//lead for lead detail emails
public function up()
{
    Schema::create('lead_detail_emails', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('lead_id')->unsigned();
        $table->string('email',255);
    });

    Schema::table('lead_detail_emails',function($table)
    {
        $table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
    });
}

//leads model
class LeadsModel extends Eloquent
{
    protected $table = 'leads';

    public function emails()
    {
        return $this->hasMany('LeadDetailEmailsModel','lead_id','id');
    }
}

//lead detail emails
class LeadDetailEmail extends Eloquent
{
    protected $table …
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel

4
推荐指数
1
解决办法
5199
查看次数

PHP:Laravel如何急于加​​载查找方法

我有一个模型Users有很多Pages,我想急切加载下面的方法,以便它只返回一个用户所有页面渴望加载,我该如何去做.

$user = User::find(1);
$pages = $user->pages();
foreach($pages as $page) {
  var_dump($page->name);
}
Run Code Online (Sandbox Code Playgroud)

我尝试过但不起作用,它会加载所有内容:

$user = User::with('Pages')->get();
$pages = $user->pages();
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel

4
推荐指数
1
解决办法
2193
查看次数