繁体   English   中英

长SQL查询-如何优化?

[英]Long SQL query - how to optimize?

我有一个大约24小时运行的sql查询。 我想知道是否有一种优化方法。

查询是:

update r, t  set r.a1 = t.a2, r.b1 = t.b1  where r.c = t.c and r.d1 = t.d2 and r.e1 = t.e2 and r.f1 = t.f2;

表r具有〜2,000,000行,并定义为:

Field  Type      Collation  Null  Key  Default Extra         
-----  --------  ---------  ----  ---  ------  --------------

id     int(11)   (NULL)     NO    PRI  (NULL)  auto_increment

a1     blob      (NULL)     YES        (NULL)                

e1     tinyblob  (NULL)     YES   MUL  (NULL)                

f1     int(11)   (NULL)     YES   MUL  (NULL)                

c      int(11)   (NULL)     YES   MUL  (NULL)                

b1     int(11)   (NULL)     YES   MUL  (NULL)                

d1     int(11)   (NULL)     YES   MUL  (NULL)                

表t具有约1,200,000行,并定义为:

Field  Type      Collation  Null  Key     Default  Extra         

-----  --------  ---------  ----  ------  -------  --------------

c      int(11)   (NULL)     NO    MUL     0                      

d2     int(11)   (NULL)     NO    MUL     0                      

e2     tinyblob  (NULL)     YES   MUL     (NULL)                 

f2     int(2)    (NULL)     NO    MUL     (NULL)                 

a2     blob      (NULL)     YES           (NULL)                 

b1     int(11)   (NULL)     YES           0                      

id     int(11)   (NULL)     NO    PRI     (NULL)   auto_increment

我想知道是否有一种优化查询的方法?

谢谢!

索引键的顺序应与where语句的顺序相同。 r表键:

c,d1,e1,f1

t表键:

c,d2,e2,f2

您是否需要在r表中的b1列上输入密钥?

您的结构似乎还不错。 2M行不是很多。 您的行可能很大(带有blobs ),但是您所做的比较仅针对整数值。 它应该更快。

尝试在表上运行ANALYZEOPTIMIZECHECKREPAIR命令,以确保正确构造了索引。

完成此操作后,您应该尝试更深入地研究系统。 是什么让查询变慢了? 有可能 :

使用监视来获取有关您的sql缓存,内存使用情况等的数据。它将帮助您诊断问题。

尝试这个

update r left join t on r.c = t.c and r.d1 = t.d2 and r.e1 = t.e2 and r.f1 = t.f2 set r.a1 = t.a2, r.b1 = t.b1

也做一些改变

  • 使c,d1,e1列在表r中建立索引

  • 使c,d2,e2列在表t中建立索引

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM