我正在从具有60万条记录的多个表中获取数据。但是要花很长时间才能拿到它。请告诉我怎样才能缩短取货的时间。我也使用了极限情况,但仍然没有改进。
我的问题是:
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秒内得到了结果,那么会有什么问题呢?当记录较少时,第一个查询运行顺利。
发布于 2012-12-31 02:02:07
使用索引可以解决你的问题。
发布于 2012-12-17 17:49:05
最后,通过使用索引,最终得到结果
发布于 2012-12-04 16:47:27
将子选择列移动到联接中
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_idhttps://stackoverflow.com/questions/13697141
复制相似问题