开发者

What columns to index on to make this query faster?

开发者 https://www.devze.com 2023-01-23 04:22 出处:网络
SELECT t.compname, min(t2.version) FROM archdetails t INNER JOIN svn3 b ON t.compname = b.compname INNER JOIN archdetails t2 ON t.compname = t2.compname
SELECT t.compname, min(t2.version) FROM archdetails t
INNER JOIN svn3 b ON t.compname = b.compname
INNER JOIN archdetails t2 ON t.compname = t2.compname
WHERE ((b.revno = '270294' OR b.revno = 'r275869' OR b.revno = 'r393316'))
  AND t.version = '1.6'
GROUP BY t.compname`

Table archdetails:

Field                    | Type         | Null | Key | Default | Extra  
-------------------------+--------------+------+-----+---------+-------  
name                     | varchar(15)  | NO   |     | NULL    |  
compname                 | varchar(500) | NO   | MUL | NULL    |  
sno                      | int(11)      | NO   |     | NULL    |  
count                    | int(11)      | NO   |     | NULL    |  
fdindex                  | int(11)      | NO   |     | NULL    |  
version                  | varchar(10)  | NO   |     | NULL    |  
sdate                    | date         | NO   |     | NULL    |  
edate                    | date         | NO   |     | NULL    |  
inFlat                   | int(11)      | NO   |     | NULL    |  
inStar               开发者_C百科    | int(11)      | NO   |     | NULL    |  
inNostar                 | int(11)      | NO   |     | NULL    |  
inReducedStar            | int(11)      | NO   |     | NULL    |

Table svn3:

Field    | Type          | Null | Key | Default | Extra  
---------+---------------+------+-----+---------+------  
name     | varchar(20)   | NO   | MUL | NULL    |  
revno    | varchar(10)   | NO   | MUL | NULL    |  
comp     | varchar(1000) | NO   | MUL | NULL    |  
compname | varchar(1000) | NO   |     | NA      |  

I have 1 index on compname version in archdetails and 4 indexes on svn3 on revno; revno, comp, compname; comp, compname; and name, revno, comp, compname.

The indexes on compname is of length 100.

The query still takes 0.16 sec to execute which is very expensive for my purpose. I don't have much experience with indexes and the above indexes have been created with variables most often used. Please advise on how to go about indexes.


A quick answer: Include fields that are in your where clause.

In your case, consider indexing svn3.revno and archdetails.version. Then look at the columns in your join. Index archdetails.compname is one to consider, too.

Of course you don't want to add too many indexes. They make your inserts and deletes slower, and make your DB take more space.


Try this

SELECT DISTINCT
t.compname,
t.version
FROM archdetails t
INNER JOIN svn3 b 
        ON t.compname 
         = b.compname 
WHERE b.revno in ('270294','r275869','r393316')
AND t.version = '1.6'
0

精彩评论

暂无评论...
验证码 换一张
取 消