2

我正在从具有 60 万条记录的多个表中获取数据。但是要花很多时间来获取它。请让我知道如何缩短获取它的时间。我也使用过 LIMIT 案例,但仍然没有改善。

我的查询是:

SELECT DISTINCT 
tf_history.thefind_id, 
tf_product.product_id, 
tf_product.`name`, 
tf_product.product_url, 
tf_product.image_tpm, 
tf_product.image_thefind, 
tf_product.image_accuracy, 
(SELECT MIN(tf_h.price)
 FROM tf_history AS tf_h
 WHERE tf_h.thefind_id = tf_history.thefind_id) as price, 
oc_product.price AS priceTPM
FROM tf_product
LEFT JOIN tf_history ON tf_product.product_id = tf_history.product_id 
                     AND tf_product.thefind_id = tf_history.thefind_id
LEFT JOIN oc_product ON tf_product.product_id = oc_product.product_id 
WHERE  tf_product.product_id = @product_id

我的表:

tf_history

history_id  int(11) NO  PRI     auto_increment
thefind_id  int(11) NO          
product_id  int(11) NO          
price   decimal(15,4)   NO          
date    datetime    NO          

AND
tf_product

thefind_id  int(11) NO  PRI     auto_increment
product_id  int(11) NO          
name    varchar(255)    NO          
store_id    int(11) NO          
product_url varchar(255)    NO          
image_tpm   varchar(255)    NO          
image_thefind   varchar(255)    NO          
image_accuracy  int(3)  NO          
date    datetime    NO

但是当我使用这个查询时:

SELECT * from tf_history

我在 0.641 秒内得到了结果,那可能是什么问题?当记录较少时,第一个查询运行顺利。

4

3 回答 3

1

终于得到了结果,通过使用索引

于 2012-12-17T09:49:04.727 回答
0

将子选择列移动到连接中

SELECT DISTINCT 
th.thefind_id, 
tp.product_id, 
tp.`name`, 
tp.product_url, 
tp.image_tpm, 
tp.image_thefind, 
tp.image_accuracy, 
th.price, 
op.price AS priceTPM
FROM tf_product tp
LEFT JOIN (SELECT thefind_id, product_id, MIN(price) as price
           FROM tf_history
           group by thefind_id, product_id) th ON tp.product_id = th.product_id
                                               AND tp.thefind_id = th.thefind_id
LEFT JOIN oc_product op ON tp.product_id = op.product_id 
WHERE  tp.product_id = @product_id
于 2012-12-04T08:47:27.207 回答
0

使用索引将解决您的问题。

于 2012-12-30T18:02:07.423 回答