2012-06-18 40 views
5

在下面這個例子:DBIx ::類例如

my $rs = $schema->resultset('CD')->search(
{ 
    'artist.name' => 'Bob Marley' 
    'liner_notes.notes' => { 'like', '%some text%' }, 
}, 
{ 
    join  => [qw/ artist liner_notes /], 
    order_by => [qw/ artist.name /], 
} 
); 

的DBIx cookbook說,這是將要生成的SQL:

# Equivalent SQL: 
# SELECT cd.*, artist.*, liner_notes.* FROM cd 
# JOIN artist ON cd.artist = artist.id 
# JOIN liner_notes ON cd.id = liner_notes.cd 
# WHERE artist.name = 'Bob Marley' 
# ORDER BY artist.name 

但是從菜譜的休息,我已經導致相信查詢只會選擇cd。*,除非當然使用預取像這樣:

my $rs = $schema->resultset('CD')->search(
{ 
    'artist.name' => 'Bob Marley' 
    'liner_notes.notes' => { 'like', '%some text%' }, 
}, 
{ 
    join  => [qw/ artist liner_notes /], 
    order_by => [qw/ artist.name /], 
    prefetch => [qw/ artist liner_notes/], 
} 
); 

下面是導致我相信這一點的說法:

[Prefetch] allows you to fetch results from related tables in advance 

任何人都可以向我解釋我在這裏失蹤嗎?或不?非常感謝!

回答

4

Equivalent SQL與烹飪書的previous section相矛盾,看起來像一個錯誤。

執行查詢並應用過濾器和排序條件時,聯接將使用來自聯接表的列,但不會返回聯接表的列。這意味着,如果你做$cd->artist->name那麼它將需要它做額外的SELECT artist.* FROM artist WHERE artist.id = ?來獲得藝術家的名字每次您調用該聲明

預取也用於從預取表中選擇所有列。在實際需要這些列時使用預取更有效率,例如,所以你可以做$cd->artist->name而不需要做額外的查詢。但是,如果你不需要這些列,那麼你就不必要地加載這些數據。

+0

所以在上面的例子中,sql將被改爲「SELECT cd。*」? – srchulo

+1

等效的sql - 沒錯 – stevenl