简体   繁体   中英

Optimizing simple SQL query?

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 ?

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 eg 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.

  1. replace * with column names
  2. create non-clustered index on GI_ID

您可以从在MAB表的GI_ID列上创建索引开始。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM