Mysql 长SQL查询-如何优化?

Mysql 长SQL查询-如何优化?,mysql,optimization,Mysql,Optimization,我有一个运行大约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有约2000000行,定义为: Field Type Collation Null Key Default Extra ----- -------- ------

我有一个运行大约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有约2000000行,定义为:

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)                
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
表t有1200000行,定义为:

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)                
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
c,d2,e2,f2
t
表键:

c,d1,e1,f1
c,d2,e2,f2

b1
列的
r
表中是否需要一个键?

您的结构似乎很好。 200万排,不多。您的行可能很大(带有
blob
),但您所做的比较仅限于整数值。它应该更快

尝试在表上运行
分析
优化
检查
修复
命令,以确保索引构造正确

完成后,您应该尝试在系统中进行更深入的调查。 是什么减缓了查询的速度?它可以是:

  • 磁盘I/O
  • 内存限制(请尝试调整my.cnf,请参阅优秀)
  • CPU(似乎不太可能)
  • 网络问题
使用监控获取有关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中建立索引


我有一个运行大约24小时的sql查询
:运行需要24小时吗-/是的:/这就是为什么我在寻找一种方法来优化它。如果需要这么长时间,很明显有些事情是非常错误的。你能从查询中输入explain语句吗?你能从我想查看索引的表中提供实际的create语句吗。