我正在研究公司的遗产项目。有一个巨大的对象是由超长查询构造的。查询不知何故看起来像这样。
SELECT *
FROM item i
JOIN item_product prod on prod.item = i.id
LEFT JOIN product_shippingaddress ps on pa.product = prod.id
LEFT JOIN product_packageinfo pp on pp.product = prod.id
.
.
.
(80 lines of query)
WHERE item.id = @itemId
这是一个非常长的查询涉及有关此产品的许多信息。
它构造了一个巨大的对象'Item',它提供了项目的所有类型的信息。以下是事物如何运作的例子
int itemId = createItem(); //creates a record in item table
associateAddressToItem(item); // Add a shipping address to item
for(int productId in productsToAdd){
addProduct(itemId); // insert info into item_product table
}
Item item = getItem(item); // This function invokes the huge query to collect information of a single item
for(Product product in item.products){
UpdateItemPrice(product.Id,itemId);
if (product.shippable())
{
addItemTax(itemId, product.Id); // add tax based on address and product attribute
}
}
item = getItem(itemId); // calls the query to update information of the object
charge(item); //charge based on item's price and tax
这种情况两次调用函数getItem。我认为这根本不高效,只要它运行两次大量查询。但是,我认为目前需要它,因为它需要获取存储在数据库中的信息来更新对象。
有没有更好的方法来处理这种情况?我觉得它没有优化,但我可以想出一种方法来改进它。