Mysql查询需要花费大量时间来获取数据
我正在从有60万条记录的多个表中提取数据。但是要花很多很多时间才能拿到。 请告诉我如何缩短取回的时间。 我也使用了极限情况,但仍然没有改进 我的问题是:Mysql查询需要花费大量时间来获取数据,mysql,c#-4.0,Mysql,C# 4.0,我正在从有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.im
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.641s内得到结果,那么会有什么问题?
当记录较少时,第一个查询运行平稳。将subselect列移动到联接中
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
最终得到了结果,通过使用索引使用索引将解决您的问题。表的定义是什么?你有什么指数?你在用什么样的硬件?什么是“很多很多时间”?秒?分钟?小时?我的硬件是双核的,你的磁盘IO系统是。。。软盘?萨塔?突袭NAS?与数据量相比,有多少内存?所有这些都很重要。谢谢@EricJ。对于询问索引,我记得我忘记了使用索引:)