[英]How can I speed up a query that use twice the same table against a MySQL using MyISAM table?
I use a query like the following that use twice the same table utdat in order to recover the data. 我使用如下查询,该查询使用两次相同的表utdat来恢复数据。
The key that link together the 2 table is tableA.utprog = tableB.utprog. 将2个表链接在一起的键是tableA.utprog = tableB.utprog。
SELECT tableA.upd, tableB.utdata FROM utdat as tableA, utdat AS tableB
WHERE tableA.uttdat = 'I' AND tableA.utctip = '01' AND
substring(tableA.utreco, 1, 1) = '3'
AND tableB.uttdat = 'I' AND tableB.utctip = '01' AND
substring(tableB.utreco, 1, 1) = '1'
AND tableA.utprog = tableB.utprog AND TRIM(substr(tableB.utreco, 65,
12)) = '20000190';
There is a way to speed up this query on MyISAM db ? 有一种方法可以加快对MyISAM db的查询速度?
Create a multi-column index on the 3 columns used in the WHERE
clause: 在WHERE
子句中使用的3列上创建一个多列索引:
ALTER TABLE utdat ADD INDEX (uttdat, utctip, itreco);
And change SUBSTRING(tableA.utreco, 1, 1) = 3
to tableA.utreco LIKE '3%', as MySQL knows how to optimize prefix matching in a
LIKE expression, but it may not realize that
SUBSTR` is extracting a prefix. 然后将SUBSTRING(tableA.utreco, 1, 1) = 3
更改为tableA.utreco LIKE '3%', as MySQL knows how to optimize prefix matching in a
LIKE expression, but it may not realize that
tableA.utreco LIKE '3%', as MySQL knows how to optimize prefix matching in a
expression, but it may not realize that
SUBSTR`正在提取前缀。
it won't affect performance, but you should also learn ANSI JOIN syntax. 它不会影响性能,但是您还应该学习ANSI JOIN语法。
SELECT tableA.upd, tableB.utdata
FROM utdat as tableA
JOIN utdat AS tableB ON tableA.utprog = tableB.utprog
WHERE tableA.uttdat = 'I' AND tableA.utctip = '01' AND tableA.utreco LIKE '3%'
AND tableB.uttdat = 'I' AND tableB.utctip = '01' AND tableB.utreco LIKE '1%'
AND TRIM(substr(tableB.utreco, 65, 12)) = '20000190';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.