0

我的 iPhone 应用程序的以下查询有问题。当应用程序运行查询时,需要相当长的时间来处理结果,可能大约一秒钟左右......我想知道查询是否可以优化?我正在使用 FMDB 框架来处理我的所有 SQL。

select pd.discounttypeid, pd.productdiscountid, pd.quantity, pd.value, p.name, p.price, pi.path 
from productdeals as pd, product as p, productimages as pi 
where pd.productid = 53252 
and pd.discounttypeid == 8769 
and pd.productdiscountid = p.parentproductid 
and pd.productdiscountid = pi.productid 
and pi.type = 362 
order by pd.id 
limit 1

我对表格的陈述如下:

CREATE TABLE "ProductImages" (
    "ProductID" INTEGER,
    "Type" INTEGER,
    "Path" TEXT
)

CREATE TABLE "Product" (
    "ProductID" INTEGER PRIMARY KEY,
    "ParentProductID" INTEGER,
    "levelType" INTEGER,
    "SKU" TEXT,
    "Name" TEXT,
    "BrandID" INTEGER,
    "Option1" INTEGER,
    "Option2" INTEGER,
    "Option3" INTEGER,
    "Option4" INTEGER,
    "Option5" INTEGER,
    "Price" NUMERIC,
    "RRP" NUMERIC,
    "averageRating" INTEGER,
    "publishedDate" DateTime,
    "salesLastWeek" INTEGER
)

CREATE TABLE "ProductDeals" (
    "ID" INTEGER,
    "ProductID" INTEGER,
    "DiscountTypeID" INTEGER,
    "ProductDiscountID" INTEGER,
    "Quantity" INTEGER,
    "Value" INTEGER
)
4

2 回答 2

1

您是否在外键列(productimages.productid 和 product.parentproductid)以及用于查找正确产品交易的列(productdeals.productid 和 productdeals.discounttypeid)上有索引?如果不是,那可能是性能不佳的原因。

您可以像这样创建它们:

CREATE INDEX idx_images_productid ON productimages(productid);
CREATE INDEX idx_products_parentid ON products(parentproductid);
CREATE INDEX idx_deals ON productdeals(productid, discounttypeid);
于 2011-10-24T14:05:35.660 回答
0

下面的查询可以帮助您减少执行时间,而且尝试正确创建字段索引以加快您的查询。

    select pd.discounttypeid, pd.productdiscountid, pd.quantity, pd.value, p.name,
    p.price, pi.path from productdeals pd join product p on pd.productdiscountid = 
    p.parentproductid join productimages pi on pd.productdiscountid = pi.productid where 
    pd.productid = 53252 and pd.discounttypeid = 8769 and pi.type = 362 order by pd.id
    limit 1

谢谢

于 2011-10-24T14:20:49.783 回答