Web ページには、PostgreSql データベースから特定の製品カテゴリの製品イメージを 1 つ表示する必要があります。この画像は、25 秒ごとに別の画像に自動的に変更されます。返品される製品は、ランダムまたはある順序である場合があります。一部の製品が欠落しており、一部が繰り返されている可能性がありますが、基準内のほとんどの製品は返品されます。利用可能な画像の合計数は、サンプル取得ごとにわずかに異なる場合があります
現在、25 秒ごとに実行される以下のコードが使用されています。これには、データベースへの 2 つのクエリが必要です。句が重複している両方のケースで、句が非常に大きく、それを変更する実際のアプリケーションでは、2 つの場所で変更が必要です。
単一のクエリが sample を返すようにこれを改善するにはどうすればよいですか? 列の型は変更できません。自然主キーが使用されます。これが役立つ場合は、追加の列、トリガー、インデックス、シーケンスを追加できます。
ASP.NET/Mono MVC3 、 npgsql が使用されます。
$count = select count(*)
from products
where prodtype=$sometype and productid in (select productid from images);
$random = next random integer between 0 .. $count-1;
-- $productsample is result: desired sample product
$productsample = select product
from products
where prodtype=$sometype and productid in (select productid from images)
offset $random
limit 1;
create table products ( productid char(20) primary key,
prodtype char(10) references producttype
);
create table images(
id serial primary key,
productid char(20) references products,
mainimage bool
);