[英]Long SQL query - how to optimize?
I have an sql query that run about 24 hours. 我有一个大约24小时运行的sql查询。 I would like to know if there is a way to optimize it. 我想知道是否有一种优化方法。
The Query is: 查询是:
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;
Table r have ~2,000,000 rows and defined as: 表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)
Table t have ~1,200,000 rows and defined as: 表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
I would like to know if there is a way to optimize the query? 我想知道是否有一种优化查询的方法?
Thanks! 谢谢!
Your index key should be ordered the same as your where statement. 索引键的顺序应与where语句的顺序相同。 r
table key: r
表键:
c,d1,e1,f1
and t
table key: 和t
表键:
c,d2,e2,f2
Do you need a key in r
table on column b1
? 您是否需要在r
表中的b1
列上输入密钥?
Your structure seems quite OK. 您的结构似乎还不错。 2M rows in not that much. 2M行不是很多。 Your rows may be large (with the blobs
), but the comparison you do are only on integer values. 您的行可能很大(带有blobs
),但是您所做的比较仅针对整数值。 It should be faster. 它应该更快。
Try to run ANALYZE
, OPTIMIZE
, CHECK
, REPAIR
commands on your tables to make sure your indexes are correctly constructed. 尝试在表上运行ANALYZE
, OPTIMIZE
, CHECK
, REPAIR
命令,以确保正确构造了索引。
Once this is done, you should try investigate deeper in the system. 完成此操作后,您应该尝试更深入地研究系统。 What is slowing down the query ? 是什么让查询变慢了? It can be : 有可能 :
Use monitoring to have data about your sql cache, memory usage etc. It will help you diagnose the issue. 使用监视来获取有关您的sql缓存,内存使用情况等的数据。它将帮助您诊断问题。
Try this 尝试这个
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
Also Do some changes 也做一些改变
make c,d1,e1 column indexed in table r 使c,d1,e1列在表r中建立索引
make c,d2,e2 column indexed in table t 使c,d2,e2列在表t中建立索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.