首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql查询需要花费大量时间来获取数据

Mysql查询需要花费大量时间来获取数据
EN

Stack Overflow用户
提问于 2012-12-04 14:15:25
回答 3查看 511关注 0票数 2

我正在从具有60万条记录的多个表中获取数据。但是要花很长时间才能拿到它。请告诉我怎样才能缩短取货的时间。我也使用了极限情况,但仍然没有改进。

我的问题是:

代码语言:javascript
复制
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

我的表格:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
SELECT * from tf_history

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-31 02:02:07

使用索引可以解决你的问题。

票数 0
EN

Stack Overflow用户

发布于 2012-12-17 17:49:05

最后,通过使用索引,最终得到结果

票数 1
EN

Stack Overflow用户

发布于 2012-12-04 16:47:27

将子选择列移动到联接中

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13697141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档