LoginSignup
3
0

More than 5 years have passed since last update.

PerlのAnikiでselectする時に使える便利なoption

Posted at

概要

Anikiでselectするときの、便利でよく使うoption一覧。

There are the options of SELECT query. See also SQL::Maker.

と書いてある様にSQL::Makerのoptが使えたりもする。

columns

指定したcolumnsだけselectしてくれるようになる。
1万件selectする時とかは、欲しいcolumnsだけちゃんと指定してあげると良い。

例: user_idだけselectする。

my rows = $db->select(user => +{}, +{
    columns => [qw/user_id/],
})->all;

suppress_row_objects

返り値がrow objectではなくて hashrefで返ってくるようになる。
これをつけると、内部的にclassに加工する処理をする前に早期returnするから、処理が減るみたい。
1万件selectする時とかは、つけると良い。

my @rows = $db->select(user => +{}, +{
    suppress_row_objects => 1,
})->all;

order_by

並び替えできる。

例: user_idで昇順に並び替える。

my @rows = $db->select(user => +{}, +{
    order_by => +{ user_id => 'ASC' },
})->all;

prefix

SELECT statement の prefixを指定できる。
例えば重複削除をしてくれる 'SELECT DISTINCT 'などを指定できる。
defaultは 'SELECT ' になっている。
※ 文字列の一番うしろにspaceが必要

例: user_idの重複削除をするとき。

my @rows = $db->select(user => +{}, +{
    columns => [qw/user_id/],
    prefix  => 'SELECT DISTINCT ',
})->all;

その他

他には、prefetchfor_updatelimitとかをよく使うと思う。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0