views:

59

answers:

3

Greetings friends,

In my MySQL database, I have 'MAB' table which contains information about Gene Ids (GI_ID),and some other gene related information.

'MAB_CAN' table can contains the Gene Ids (GI_ID) only relevant to Cancer.

I use following SQL query to get cancer related information from MAB table :

SELECT * FROM MAB WHERE `GI_ID` IN (SELECT `GI ID` FROM `MAB_CAN`)

It takes about 14 Seconds for this query, which is too long.(1605 records).

But the following independent queries it takes short time.

SELECT GI_ID FROM MAB_CAN WHERE 1

  • 0.0005 secs (1,605 records)

SELECT * FROM MAB WHERE 1

  • 0.0007 sec (31,043 records)

Any tips of optimizing my first query ?

+2  A: 

Try this

SELECT * FROM MAB
   INNER JOIN MAB_CAN ON MAB.GI_ID = MAB_CAN.GI_ID

You then need to look at what columns you need to return you should never really use SELECT * instead return a distinct list of required column names e.g. SELECT Col1, Col2, Col3 FROM... that way you minimize the returned data.

As the other answers have indicated if the query is still slow you need to look at placing an index on the GI_ID field in the MAB table.

Simon Mark Smith
+1  A: 

You can start with creating an index on GI_ID column in the MAB table.

Svetlozar Angelov
+1  A: 
  1. replace * with column names
  2. create non-clustered index on GI_ID
lakhlaniprashant.blogspot.com